MarkdownSnippets

repository·main·Indexed 42 days ago

https://github.com/simoncropp/markdownsnippets

.NET tool and MSBuild task that extracts code snippets from source files and merges them into Markdown documents. It supports C# regions and comment conventions for snippet definition, remote content inclusion via URLs, and integration with Expressive Code syntax. Available as a global dotnet tool (requires .NET 10 or higher) or a NuGet package.

Tokens
10.3K
Snippets
62
Records
73
Agent score
53%

What's inside MarkdownSnippets

  1. How code indentation trimming works in snippets

    main

    MarkdownSnippets performs 'smart trimming' of indentation within snippets. It identifies the common leading whitespace (either spaces or tabs) used across all lines within the snippet boundaries and removes that amount from every line. This allows you to indent snippet definitions within your source files without affecting the actual content of the snippet when it is rendered.

    ```markdown
    ··// begin-snippet MySnippetName
    ··Line one of the snippet
    ····Line two of the snippet
    ··// end-snippet

    // Resulting output: Line one of the snippet ··Line two of the snippet

  2. Define snippets in code files

    main

    MarkdownSnippets extracts code blocks based on specific comment conventions or C# regions. The name used in the code becomes the snippet key.

    Using convention-based comments

    Wrap your code with begin-snippet: <KeyName> and end-snippet.

    Using C# regions

    Named C# regions are also supported. The region name is used as the snippet key.

    Behavior: The tool recursively scans the target directory for code files containing these markers and markdown (.md or .mdx) files to merge them into.

    // begin-snippet: MySnippetName
    My Snippet Code
    // end-snippet
    
    #region MySnippetName
    My Snippet Code
    #endregion
  3. Enable content validation with ValidateContent

    main

    Setting ValidateContent: true runs technical-writing style checks on the processed markdown. It flags:

    • Exclamation marks (! ).
    • Discouraged words (e.g., you, your, we, our, please, just, simply, easy, aforementioned, henceforth).
    • Wordy phrases (e.g., in order to should be to).

    Note: Block quotes (>), fenced code blocks, and inline code spans are skipped. An unclosed fenced code block will be reported as an error.

  4. How code indentation trimming works

    main

    MarkdownSnippets performs 'smart trimming' of snippet indentation. It identifies the common leading whitespace (either spaces or tabs) used across all lines within a snippet block and removes that specific amount from every line. This allows you to indent your snippet definitions within your source file without affecting the actual content of the snippet when it is rendered.

    // Source snippet with 2 leading spaces:
      // begin-snippet MySnippetName
      Line one of the snippet
        Line two of the snippet
      // end-snippet
    
    // Resulting output:
    Line one of the snippet
      Line two of the snippet
  5. Understand Document Scanning Conventions

    main

    MarkdownSnippets uses different conventions to determine how markdown files are scanned and modified.

    SourceTransform (Default)

    This convention scans for *.source.md files. The tool extracts snippets and merges them into these files to produce the final *.md files (e.g., readme.source.md produces readme.md). This process overwrites existing .md files that match a .source.md file.

    The mdsource directory pattern

    You can place .source.md files in a sub-directory named mdsource. When calculating the target path, the mdsource part of the path is removed. This allows you to group source files in a sub-directory without cluttering the main documentation directory. Warning: When using this pattern, all links and images must use full paths from the repository root because relative paths will break between the source and the generated target files.

    InPlaceOverwrite

    This convention scans for all *.md files and merges snippets directly into them, modifying them in place.

    {
      "Convention": "InPlaceOverwrite"
    }
  6. Default directory exclusions

    main

    MarkdownSnippets automatically ignores several types of directories by default to avoid processing unnecessary or temporary files. These include:

    • Source Control: .git
    • IDE Temp Files: .vs, .vscode, .idea
    • Package Caches: packages, node_modules
    • Build Output: dist, .angular, bin, obj
    • Hidden Directories: Any directory with the Hidden attribute.
  7. Default file exclusions (Binary and Non-comment files)

    main

    MarkdownSnippets automatically excludes certain file types from processing:

    1. Binary Files: A wide range of binary extensions (e.g., .exe, .zip, .png, .pdf, .mp3, etc.) are excluded to prevent errors during text processing.
    2. Files that cannot contain comments: Files that do not support the comment syntax required for snippet markers are excluded. These include:
      • DotSettings
      • csv
      • json
      • geojson
      • sln
  8. Enable IDE autocomplete for mdsnippets.json

    main

    To enable editor help, code completion, and validation in IDEs like JetBrains Rider, include the $schema field in your mdsnippets.json file pointing to the official schema.

    {
      "$schema": "https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/schema.json"
    }
  9. Include a full file as a snippet

    main
    If a snippet key does not match any defined snippet, MarkdownSnippets searches the target directory for a file matching that name. For example, snippet: license.txt will render the entire contents of license.txt as a code block.
    snippet: license.txt