Coder Social home page Coder Social logo

markdownsnippets's Introduction

MarkdownSnippets

Build status NuGet Status NuGet Status NuGet Status

A dotnet tool that extract snippets from code files and merges them into markdown documents.

See Milestones for release notes.

.net 8 or higher is required to run this tool.

Value Proposition

Automatically extract snippets from code and injecting them into markdown documents has several benefits:

  • Snippets can be verified by a compiler or parser.
  • Tests can be run on snippets, or snippets can be pulled from existing tests.
  • Changes in code are automatically reflected in documentation.
  • Snippets are less likely to get out of sync with the main code-base.
  • Snippets in markdown is easier to create and maintain since any preferred editor can be used to edit them.

Behavior

  • Recursively scan the target directory for code files containing snippets. (See exclusion).
  • Recursively scan the target directory for markdown (.md or mdx) files. (See Document Scanning).
  • Merge the snippets into those markdown files.

Installation

Ensure dotnet CLI is installed.

Install MarkdownSnippets.Tool

dotnet tool install -g MarkdownSnippets.Tool

Update

dotnet tool update -g MarkdownSnippets.Tool

Usage

mdsnippets C:\Code\TargetDirectory

If no directory is passed the current directory will be used, but only if it exists with a git repository directory tree. If not an error is returned.

Document Convention

There are two approaches scanning and modifying markdown files.

SourceTransform

This is the default.

source.md file

The file convention recursively scans the target directory for all *.source.md files. Once snippets are merged the .source.md to produce .md files. So for example readme.source.md would be merged with snippets to produce readme.md. Note that this process will overwrite any existing .md files that have matching .source.md files.

mdsource directory

There is a secondary convention that leverages the use of a directory named mdsource. Where .source.md files are placed in a mdsource sub-directory, the mdsource part of the file path will be removed when calculating the target path. This allows the .source.md to be grouped in a sub directory and avoid cluttering up the main documentation directory.

When using the mdsource convention, all references to other files, such as links and images, should specify the full path from the root of the repository. This will allow those links to work correctly in both the source and generated markdown files. Relative paths cannot work for both the source and the target file.

InPlaceOverwrite

Recursively scans the target directory for all *.md files and merges snippets into those files.

Command line
mdsnippets -c InPlaceOverwrite
mdsnippets --convention InPlaceOverwrite
Config file

Can be enabled in mdsnippets.json config file.

{
  "Convention": "InPlaceOverwrite"
}

Moving from SourceTransform to InPlaceOverwrite

  • Ensure "WriteHeader": false is used in mdsnippets.json.
  • Ensure "ReadOnly": false is used in mdsnippets.json.
  • Ensure using the current stable version and a docs generation has run.
  • Delete all .source.md files.
  • Modify mdsnippets.json to add "Convention": "InPlaceOverwrite".
  • Run the docs generation.

Mark resulting files as read only

To mark the resulting documents files as read only use -r or --read-only.

This can be helpful in preventing incorrectly editing the documents file instead of the .source. file conventions.

mdsnippets -r true
mdsnippets --read-only true

Defining Snippets

Any code wrapped in a convention based comment will be picked up. The comment needs to start with begin-snippet: which is followed by the key. The snippet is then terminated by end-snippet.

// begin-snippet: MySnippetName
My Snippet Code
// end-snippet

Named C# regions will also be picked up, with the name of the region used as the key.

To stop regions collapsing in Visual Studio disable 'enter outlining mode when files open'. See Visual Studio outlining.

UrlsAsSnippets

Urls to files to be included as snippets. Space separated for multiple values.

Each url will be accessible using the file name as a key. Any snippets within the files will be extracted and accessible as individual keyed snippets.

mdsnippets --urls-as-snippets "https://github.com/SimonCropp/MarkdownSnippets/snippet.cs"
mdsnippets -u "https://github.com/SimonCropp/MarkdownSnippets/snippet.cs"

Using Snippets

The keyed snippets can be used in any documentation .md file by adding the text snippet: KEY.

Then snippets with that key.

For example

Some blurb about the below snippet
snippet: MySnippetName

The resulting markdown will be:

<!-- snippet: MySnippetName -->
Some blurb about the below snippet
<a id='snippet-MySnippetName'></a>
```
My Snippet Code
```
<sup><a href='/relativeUrlToFile#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-MySnippetName' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Notes:

Including a snippet from the web

Snippets that start with http will be downloaded and the contents rendered. For example:

snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt

Will render:

<!-- snippet: https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt -->
<a id='snippet-https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt'></a>
```txt
The MIT License (MIT)
...
```
<sup><a href='#snippet-https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/license.txt' title='Snippet source file'>anchor</a></sup>
<!-- endSnippet -->

Files are downloaded to %temp%MarkdownSnippets with a maximum of 100 files kept.

Including a full file

If no snippet is found matching the defined name. The target directory will be searched for a file matching that name. For example:

snippet: license.txt

Will render:

<!-- snippet: license.txt -->
<a id='snippet-license.txt'></a>
```txt
The MIT License (MIT)
...
```
<sup><a href='#snippet-license.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Files are downloaded to %temp%MarkdownSnippets with a maximum of 100 files kept.

LinkFormat

Defines the format of snippet source links that appear under each snippet.

Command line

mdsnippets --link-format Bitbucket
mdsnippets -l Bitbucket

Values

namespace MarkdownSnippets;

public enum LinkFormat
{
    GitHub,
    Tfs,
    Bitbucket,
    GitLab,
    None
}

snippet source | anchor

Link format None will omit the source link but still keep the snippet anchor.

OmitSnippetLinks

The links below a snippet can be omitted.

Command line

mdsnippets --omit-snippet-links true

Config file

{
  "OmitSnippetLinks": true
}

How links are constructed

if (linkFormat == LinkFormat.GitHub)
{
    Polyfill.Append(builder, $"{path}#L{snippet.StartLine}-L{snippet.EndLine}");
    return;
}

if (linkFormat == LinkFormat.Tfs)
{
    Polyfill.Append(builder, $"{path}&line={snippet.StartLine}&lineEnd={snippet.EndLine}");
    return;
}

if (linkFormat == LinkFormat.Bitbucket)
{
    Polyfill.Append(builder, $"{path}#lines={snippet.StartLine}:{snippet.EndLine}");
    return;
}

if (linkFormat == LinkFormat.GitLab)
{
    Polyfill.Append(builder, $"{path}#L{snippet.StartLine}-{snippet.EndLine}");
    return;
}

snippet source | anchor

UrlPrefix

UrlPrefix allows a string to be defined that will prefix all snippet links. This is helpful when the markdown file are being hosted on a site that is not co-located with the source code files. It can be defined in the config file, the MsBuild task, and the dotnet tool.

Command line

mdsnippets --url-prefix "TheUrlPrefix"

Config file

{
  "UrlPrefix": "TheUrlPrefix"
}

Add to Windows Explorer

Use src/context-menu.reg to add MarkdownSnippets to the Windows Explorer context menu.

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Shell]
@="none"
[HKEY_CLASSES_ROOT\Directory\shell\mdsnippets]
"MUIVerb"="run mdsnippets"
"Position"="bottom"
[HKEY_CLASSES_ROOT\Directory\Background\shell\mdsnippets]
"MUIVerb"="run mdsnippets"
"Position"="bottom"
[HKEY_CLASSES_ROOT\Directory\shell\mdsnippets\command]
@="cmd.exe /c mdsnippets \"%V\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\mdsnippets\command]
@="cmd.exe /c mdsnippets \"%V\""

snippet source | anchor

More Documentation

Credits

Loosely based on some code from https://github.com/shiftkey/scribble.

Icon

Down by Alfredo Creates from The Noun Project.

markdownsnippets's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

markdownsnippets's Issues

mdsnippets on Mac produces files with mixed line endings, confusing GitHub Desktop

Preamble

This has happened on both @isidore and my Macs.

Versions on my machine:

  • Tool 'markdownsnippets.tool' was reinstalled with the latest stable version (version '16.4.0').
  • .NET Core SDK 2.2.401 (x64)

Describe the bug

Expected behaviour: The output .md files should have consistent line endings

Actual behaviour: On Mac, when the input file has Unix EOLs, the output file has the following lines with different EOLs:

<!-- toc -->
## Contents

I'll attach a screenshot diffing the source and destination files in BeyondCompare, as it makes it easy to see diffs in line endings.

When I run mdsnippets on files in https://github.com/approvals/ApprovalTests.cpp, there are always some files whose source has not changed, but whose generated files show a few lines of difference in GitHub Desktop, around the table of contents.

Minimal Repro

Example file to run it on:

https://github.com/approvals/ApprovalTests.cpp/blob/master/doc/mdsource/TemplatePage.source.md

Download that to a Mac and convert it with this command line:

mdsnippets --exclude cmake-build --toc-level 5 --readonly true --header "GENERATED FILE - DO NOT EDIT\nThis file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).\nSource File: {relativePath}\nTo change this file edit the source file and then execute ./run_markdown_templates.sh."

View the output file with an editor that shows line endings. You should see that 3 lines have different EOLs.

Add custom Header string

  • add header param to DirectoryMarkdownProcessor
  • add 'header' param to MarkdownProcessor
  • remove short header option from command line. ie -h is no longer valid. use --write-header instead
  • add header option (--header) to command line

Preserve line endings

The resulting .md should have the same line endings as the source.md file.

It sucks that this is even an issue and somehow part of our lives are dealing with line endings again. :-(

Way to pull in files from outside repo - preferably by supplying a URL to text file

Is the feature request related to a problem

No.

I would like to pull in to docs some files that, for C++-builds-reasons, cannot be in my repo: they are in another repository that is adjacent to mine.

I tried these things:

1. Add a snippet to pull the file in via relative links:

snippet: ../../../ApprovalTests.cpp.CMakeSamples/develop_approvaltests/CMakeLists.txt

Result:

Failed: Missing snippets: ../../../ApprovalTests.cpp.CMakeSamples/develop_approvaltests/CMakeLists.txt

2. Added a symlink to the adjacent repo, so it appears to be inside my repo

ln -s /Users/clare/Documents/develop/ApprovalTests/ApprovalTests.cpp.CMakeSamples .

Then put this snippet in:

snippet: develop_approvaltests/CMakeLists.txt

(t needs a directory, because there are CMakeLists.txt files in many directories)

Result:

Added 697 .source.md files
Searching 17720 files for snippets
Unhandled exception. System.IO.IOException: Too many levels of symbolic links
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.File.OpenText(String path)
   at MarkdownSnippets.FileSnippetExtractor.Read(String path, Int32 maxWidth) in C:\projects\markdownsnippets\src\MarkdownSnippets\Reading\FileSnippetExtractor.cs:line 95
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MarkdownSnippets.DirectoryMarkdownProcessor.AddSnippetsFrom(String directory) in C:\projects\markdownsnippets\src\MarkdownSnippets\Processing\DirectoryMarkdownProcessor.cs:line 101
   at MarkdownSnippets.DirectoryMarkdownProcessor..ctor(String targetDirectory, Boolean scanForMdFiles, Boolean scanForSnippets, Action`1 log, AppendSnippetGroupToMarkdown appendSnippetGroup, Boolean writeHeader, String header, DirectoryFilter directoryFilter, Boolean readOnly, LinkFormat linkFormat, Int32 tocLevel, IEnumerable`1 tocExcludes, Boolean treatMissingSnippetsAsWarnings, Int32 maxWidth, String urlPrefix) in C:\projects\markdownsnippets\src\MarkdownSnippets\Processing\DirectoryMarkdownProcessor.cs:line 71
   at Program.Inner(String targetDirectory, ConfigInput configInput) in C:\projects\markdownsnippets\src\MarkdownSnippets.Tool\Program.cs:line 28
   at CommandRunner.<>c__DisplayClass0_0.<RunCommand>b__0(Options options) in C:\projects\markdownsnippets\src\MarkdownSnippets.Tool\CommandRunner.cs:line 41
   at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
   at CommandRunner.RunCommand(Invoke invoke, String[] args) in C:\projects\markdownsnippets\src\MarkdownSnippets.Tool\CommandRunner.cs:line 42
   at Program.Main(String[] args) in C:\projects\markdownsnippets\src\MarkdownSnippets.Tool\Program.cs:line 11
./run_markdown_templates.sh: line 15: 14890 Abort trap: 6           mdsnippets --exclude cmake-build:build --toc-level 5 --readonly true --header "GENERATED FILE - DO NOT EDIT\nThis file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).\nSource File: {relativePath}\nTo change this file edit the source file and then execute ./run_markdown_templates.sh."

3. Try a URL instead

This was kind of wishful thinking - and not actually the file I wanted to include - but as an experiment:

snippet: https://github.com/approvals/ApprovalTests.cpp.Qt.StarterProject/blob/master/CMakeLists.txt

Result:

Failed: Missing snippets: https://github.com/approvals/ApprovalTests.cpp.Qt.StarterProject/blob/master/CMakeLists.txt

Describe the solution

A clear and concise proposal of how you intend to implement the feature.

Sorry, I don't know C# so won't be able to do this.

If it were possible to implement any of these:

  • Allow relative links to outside the repo
  • Allow symlinks inside the repo, to repos elsewhere
  • Allow hyperlinks and embed their content

I prefer the last one, as mdsnippets would presumably not need to work out repo URLs - it could just use the exact URL that was supplied...

Another reason for preferring the last one is that anyone else could run mdsnippets on this project without having to clone the other repo in a particular location, and possible even create a symlink to it.

Describe alternatives considered

A clear and concise description of any alternative solutions or features you've considered.

I'm going to have to copy-and-paste the source files in.

Additional context

Add any other context about the feature request here.

There is an additional problem in this case, which is that these CMakeLists.txt files will need to be formatted with the "cmake" language, which isn't obvious from the file extension...

For example:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# This version is needed for FetchContent_Declare & FetchContent_MakeAvailable

project(fetch_content_approvaltests)

enable_testing()

add_subdirectory(dependencies)
add_subdirectory(tests)

MSBuildTask does not play well with NCrunch

Describe the bug

To be honest I don't think it's a bug really but reporting this as an FYI for you and others so close immediately :)

When using the MSBuild task and NCrunch is running then one receives a compilation error from ncrunch:

..\..\..\..\..\Users\damian\.nuget\packages\markdownsnippets.msbuild\17.6.0\build\MarkdownSnippets.MsBuild.targets (21, 6): No '.git' directory found. Define a 'MdTargetDir'.

The reason is that NCrunch copies things to workspace directory that obviously won't be a git repo. I'm not sure setting the MdTargetDir will suffice as MSBuild variables will be pointing to workspace directory instead of the repo directory.

In anycase I'll switch over to the dotnet tool.

Minimal Repro

  1. Checkout [this commit].
  2. Enable ncrunch.

Mark output files readonly

Is the feature request related to a problem?

Often while iterating on documentation, we accidentally edit the output files instead of the source files. These edits then get wiped out when we rerun mdsnippets.

Describe the solution

mdsnippets could mark the output files to be writable before writing them, and set them back to readonly right after.

Describe alternatives considered

None at this time.

Additional context

We're on Windows.

Thanks,

  • Jay and Llewellyn

Remove some redundant MarkdownProcessor constructors

public MarkdownProcessor(
    ReadSnippets snippets,
    AppendSnippetGroupToMarkdown appendSnippetGroup)

public MarkdownProcessor(
    IEnumerable<Snippet> snippets,
    AppendSnippetGroupToMarkdown appendSnippetGroup,
    IReadOnlyList<string> snippetSourceFiles)

Should probably ignore regions which you cannot use, rather than exit on unhandled exception

I saw the C++ on Sea 2020 talk, and so naturally, I tried running the tool on my existing repo (no .source.md files, just to see what it would do).

It turns out it failed with an exception, because one of our .cs files has this region: "#region COM+ functions". I'm not on the system upon which I ran the test, so I cannot copy/paste the exception trace, but it fails in KeyValidaror.cs, line 19.

I don't know why that fails with that message per se, and I cannot even confirm the code is sound, but I'd suggest that optimally, the utility should probably ignore implicit snippets which cannot be parsed per expectation. That could make it easier for people to explore and/or adopt in existing codebases.

Offer to help: add some structure to ToC of project page

Hi Simon,

As the number of features has increased, I have started to struggle to see what I'm looking for in the long table of contents on the main page...

I realise that this is a nice problem to have (lots of features!) - but if you would like a PR to have a stab at adding some grouping sections, I would be happy to do this...

MdTargetDir .git error

Preamble

Using MarkdownSnippets.MSBuild 18.0.0 or 19.0.0 - same error

Describe the bug

A clear and concise description of what the bug is. Include any relevant version information.
It seems like MarkdownSnippets.MSBuild is not usable when the initial build target is dotnet test.

A clear and concise description of what you expected to happen.
A clearer error message than:

No '.git' directory found. Define a MdTargetDir.

Add any other context about the problem here.
I reviewed the last code change, here: fee9f9b#r40062164

Minimal Repro

Clone RazorLight repo. run the following commands in root directory:

dotnet --info
dotnet restore
dotnet test  tests/RazorLight.Tests/RazorLight.Tests.csproj

Table of contents links don't work for headings with commas

Describe the bug

toc

## I am a title, but it has a comma

The table of contents generated from this will not work - i.e. clicking on the link in the toc does not cause Firefox to jump to the location, when viewing the generated file on github.

Minimal Repro

As above.

Allow other files than text files

snippet: tests/approvals/ApprovalTest.testVerifyAsJson.approved.json

results in

Failed to process markdown: Missing snippets: tests/approvals/ApprovalTest.testVerifyAsJson.approved.json

This does not happen if we use a .txt file.

Thank you,
Josh and Llewellyn

Consider syntax for one-line snippets

Is the feature request related to a problem

Some of the ApprovalTests.cpp tests are becoming quite cluttered as a result of the MarkdownSnippets text.

For example (not yet updated to new start/end markers):

// startcode googletest_customize_test
TEST(TestCaseName_IgnoreThis, TestName1 )
// endcode
{
    ApprovalTestNamer namer;
// startcode googletest_customize_test_name
    auto outputFileBaseName = "GoogleFixtureNamerCustomizationTests.TestName";
// endcode
    EXPECT_EQ(namer.getOutputFileBaseName(), outputFileBaseName);
}

A couple of times, I've come to work on a test file and all my brain has seen is the comments for MarkdownSnippets, and not the code...

Describe the solution

Would it be possible to have an extra kind of markup which is an inline, end-of-line comment.

For example:

TEST(TestCaseName_IgnoreThis, TestName2) // inline-snippet googletest_customize_test
{
    ApprovalTestNamer namer;
    auto outputFileBaseName = "GoogleFixtureNamerCustomizationTests.TestName"; // inline-snippet googletest_customize_test_name
    EXPECT_EQ(namer.getOutputFileBaseName(), outputFileBaseName);
}

The idea is that the characters from // snippet-inline to the end of the line would be excluded from the output.

I realise that this is a complication for the current code, which does not need to understand how to write comments in all the different languages that someone might use this tool with...

Describe alternatives considered

One idea I can think of is for @isidore and I to add white-space around our markdown code blocks. This would change the top example to:

// startcode googletest_customize_test
TEST(TestCaseName_IgnoreThis, TestName1 )
// endcode

{
    ApprovalTestNamer namer;

// startcode googletest_customize_test_name
    auto outputFileBaseName = "GoogleFixtureNamerCustomizationTests.TestName";
// endcode

    EXPECT_EQ(namer.getOutputFileBaseName(), outputFileBaseName);
}

This works well for glance-ability of the second block - with auto outputFileBaseName - but any code-formatting tool would delete the blank line before the opening {, and I think separating the TEST and { lines with a blank line is really jarring to read, for anyone familiar with the C++ Google Test framework.

Another is for us to indent some of the comments, so that they are consistent with the surroudning code, which does help a little - but I'd still prefer the inline idea...

// startcode googletest_customize_test
TEST(TestCaseName_IgnoreThis, TestName1 )
// endcode
{
    ApprovalTestNamer namer;
    // startcode googletest_customize_test_name
    auto outputFileBaseName = "GoogleFixtureNamerCustomizationTests.TestName";
    // endcode
    EXPECT_EQ(namer.getOutputFileBaseName(), outputFileBaseName);
}

Additional context

Can't think of any.

Snippet source has all dots removed when target is "." ๐Ÿ›

There's a bug where if you run mdsnippets supplying . as the target directory to mean the current directory, like this:

$ mdsnippets .

then the snippet source link in the output Markdown files has all its dots removed!

Steps to reproduce

Create an empty directory and copy Usage.cs from this project there.

Then add a Markdown document called index.source.md with the following content:

snippet: ReadingDirectorySimple

Next, run:

mdsnippets .

and you should see the following output:

$ mdsnippets .
Searching 3 files for snippets
Found 5 snippets
Found 1 .source.md files
Processing .\index.source.md

Next, look at the content of index.md

<!--
This file was generate by MarkdownSnippets.
Source File: \indexsourcemd
To change this file edit the source file and then re-run the generation using either the dotnet global tool (https://github.com/SimonCropp/MarkdownSnippets#githubmarkdownsnippets) or using the api (https://github.com/SimonCropp/MarkdownSnippets#running-as-a-unit-test).
-->

# Welcome

<!-- snippet: ReadingDirectorySimple -->
```cs
// extract snippets from files
var snippetExtractor = new DirectorySnippetExtractor(
    // all directories except bin and obj
    directoryFilter: dirPath => !dirPath.EndsWith("bin") && !dirPath.EndsWith("obj"),
    // all js and cs files
    fileFilter: filePath => filePath.EndsWith(".js") || filePath.EndsWith(".cs"));
var snippets = snippetExtractor.ReadSnippets(@"C:\path");
```
<sup>[snippet source](/Usagecs#L34-L44)</sup>
<!-- endsnippet -->

Notice the line:

<sup>[snippet source](/Usagecs#L34-L44)</sup>

The relative URL reads /Usagecs#L34-L44 instead of /Usage.cs#L34-L44. The dot preceding the file extension disappeared!

If you run mdsnippets with the full target directory path, e.g. using mdsnippets "%cd%" on the Windows Command Prompt or mdsnippets "$pwd" in PowerShell or mdsnippets "$(pwd)" in Bash, then the bug does not occur.

Environment

I am using version 9.0.0 on Windows 10.

My dotnet --info says:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

.NET Core SDKs installed:
  1.1.8 [C:\Program Files\dotnet\sdk]
  1.1.9 [C:\Program Files\dotnet\sdk]
  1.1.10 [C:\Program Files\dotnet\sdk]
  1.1.11 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.300 [C:\Program Files\dotnet\sdk]
  2.1.400 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.1.502 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Map file extensions to user-specified language

Problem

I am using Pygments for code highlighting of fenced code blocks in Markdown. MarkdownSnippets uses the file extension as the language identifier so for C# this ends up being cs. Unfortunately, the Pygments lexers list says that the short names recognized for C# are csharp or c# and so my code blocks appear without highlighting. I imagine that other people may run into a similar problem so it would help to offer a way to map file extensions to a language identifier for custom scenarios. The use of file extension as the language identifier can be left as a default.

Solutions

Solution 1: Configuration File

A configuration file is used to list the desired mappings between source file extensions and target language identifiers. I would suggest using the INI format for simplicity as the complexity/ceremony of JSON or XML feels unwarranted. An example configuration would look like this:

[md-code-languages]
csharp=.cs

Multiple extensions mapping to a single language identifier should be allowed as separate entries:

[md-code-languages]
csharp=.cs
csharp=.cshtml

The command-line tool will look for a file called mdsnippets.ini in the target directory, parse the [md-code-languages] section (using Gini?) into a dictionary of file extension to language identifier mappings and make it available for the remainder of processing.

Solution 2: Command-Line Arguments

An alternative to solution 1 would be to allow file extension to language identifier mappings to be specified as optional command-line arguments in the form of --md-code-language:csharp=.cs. Multiple such arguments could be supplied, one for each mapping.

snippet: filename.md causes error

Describe the bug

did a snippet: with an md file. It didn't work.
Used an include: which worked beautify and was all I needed, but the snippet should work as well.

Snippet source link customization

Problem

There is no way to customize the [snippet source] link, such as:

  • omit it from the output Markdown document
  • specify an alternate URL prefix for when they are hosted elsewhere than the documentation

Solution

Add command-line arguments or configuration (not unlike the suggestions in #14) to:

  • disable the snippet source link altogether
  • specify a URL prefix such

The URL prefix should recognize a token such as $, which when present, is replaced with the commit hash. This provides a more stable link from the documentation to the snippet, in case the latter changes or moves.

Recent releases seemingly missing from github releases list

Describe the bug

The github releases page only goes up to 16.1.1 on 3 August.
https://github.com/SimonCropp/MarkdownSnippets/releases

The milestone of this issue suggests there was at least a 16.4.2 release:
#89

And this output suggests that the current version is 16.5.0

$ dotnet tool update  -g MarkdownSnippets.Tool
error NU1202: Package MarkdownSnippets.Tool 16.5.0 is not compatible with netcoreapp2.2 (.NETCoreApp,Version=v2.2) / any. Package MarkdownSnippets.Tool 16.5.0 supports: netcoreapp3.0 (.NETCoreApp,Version=v3.0) / any
Tool 'markdownsnippets.tool' failed to update due to the following:
The tool package could not be restored.
Tool 'markdownsnippets.tool' failed to install. This failure may have been caused by:

* You are attempting to install a preview release and did not use the --version option to specify the version.
* A package by this name was found, but it was not a .NET Core tool.
* The required NuGet feed cannot be accessed, perhaps because of an Internet connection problem.
* You mistyped the name of the tool.

Minimal Repro

Open https://github.com/SimonCropp/MarkdownSnippets/releases

Level 4, 5 and 6 headings in TOC have incorrect text and anchor

Preamble

version: latest stable version (version '16.4.1').

Describe the bug

Headings in levels 4, 5 and 6 are displayed in the ToC with 1, 2 and 3 extra # prefixes.

They are also not given unique anchors, if the heading text is duplicated.

Minimal Repro

Input:

# Title

toc

## Section

### Section

#### Section 

##### Section 

###### Section

Command line used

From https://github.com/approvals/ApprovalTests.cpp/blob/b802c36bf5886745f76ab7b143eb6ab1eb706c9f/run_markdown_templates.sh:

mdsnippets \
    --exclude cmake-build \
    --toc-level 5 \
    --readonly true \
    --header "GENERATED FILE -...  execute ./run_markdown_templates.sh."

Expected TOC in Output:

<!-- toc -->
## Contents

  * [Section](#section)
    * [Section](#section-1)
      * [Section](#section-2)
        * [Section](#section-3)
          * [Section](#section-4)
<!-- endtoc -->

Actual TOC in Output:

<!-- toc -->
## Contents

  * [Section](#section)
    * [Section](#section-1)
      * [# Section](#-section)
        * [## Section](#-section)
          * [### Section](#-section)
<!-- endtoc -->

Note that there are 3 sections with anchor '-section'..

PS readonly saved me at least twice, editing the repro for this ticket - thanks!

allow /source/ directories

Too many files

I am loving MarkdownSnippets and it is helping to have me create more documentation than ever before. The issue is it creates a lot of files in the directories.

For example: https://github.com/approvals/ApprovalTests.cpp/tree/master/doc

This creates a sort of decision paralysis when reading docs.

seperate /source/ subdirectory

It would be nice to have
/docs/source/Contributing.source.md
compile to
/docs/Contributing.md

The idea would be if the directory right in front of a .source.md file is source, it is also removed.

Describe alternatives considered

don't really have an alternative at the moment.

Additional context

To be clear, I still want the existing behavior as well. just the option of putting the docs in a source subdirectory when they get to numerous.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.