# Snipsync Snipsync makes sure your documented code snippets are always in sync with your Github repo source files. ## Prerequisites This tool requires [Node](https://nodejs.org/) v15.0.0 or above (recommended 15.2.1) and [Yarn](https://yarnpkg.com/). ## Install **Yarn**: ```bash yarn add snipsync ``` ## Configure Create a file called "snipsync.config.yaml" in the project root. This file specifies the following: - `origins`: The Github repositories or local files where the tool will look for source code snippets. - `targets`: The local directories that contain the files to be spliced with the code snippets. The `origins` property is a list of objects that have one of the following 2 formats: 1. `owner`, `repo`, and optionally `ref`: Pull snippets from a GitHub repo 2. `files`: a set of strings: - `pattern`: Relative path to load snippets from. Supports [glob](https://www.npmjs.com/package/glob) syntax. - `owner`: GitHub repo owner name, to be used in the source snippets links - `repo`: Name of the repo snipsync is being used in, to link to the source snippets - `ref`: (Optional, defaults to main) Used for writing source snippet links. If the `ref` key is left blank or not specified, then the most recent commit from the main branch will be used. If the `enable_source_link` key in `features` is not specified, then it will default to `true`. If the `enable_code_block` key in `features` is not specified, then it will default to `true`. The `allowed_target_extensions` key in `features` lets you set a list of extensions to scan. Specify extensions like `[.md,.txt]`. If the `allowed_target_extensions` key in `features` is not specified, then it defaults to an empty array (`[]`) and all files are scanned. The `enable_code_dedenting` key in `features` lets you remove leading spaces from indented code snippets. This is handy when you're including a snippet of code within a class or function and don't want to include the leading indentation. This is `false` by default. The `require_named_nested_ends` key in `features` makes an unnamed `@@@SNIPEND` inside [nested snippets](#nested-snippets) a hard error instead of a warning, so the run fails before any files are written. This is `false` by default. Example of a complete `snipsync.config.yaml`: ```yaml origins: - owner: temporalio repo: go-samples ref: 6880b0d09ddb6edf150e3095c90522602022578f - owner: temporalio repo: java-samples - files: pattern: ./sample-apps/typescript/*.ts owner: temporalio repo: documentation ref: main targets: - docs - blog features: enable_source_link: false enable_code_block: false allowed_target_extensions: [.md] enable_code_dedenting: false require_named_nested_ends: true ``` Example of a bare minimum snipsync.config.yaml: ```yaml origins: - owner: temporalio repo: go-samples targets: - docs ``` ## Comment wrappers Use comments to identify code snippets and the locations where they should be merged. ### Source code In the source repo, wrap the code snippets in comments with a unique snippet identifier like this: ```go // @@@SNIPSTART hellouniverse func HelloUniverse() { fmt.Println("Hello Universe!") } // @@@SNIPEND ``` In the example above, "hellouniverse" is the unique identifier for the code snippet. Unique identifiers can contain letters, numbers, hyphens, and underscores. #### Nested snippets Snippets can be nested, so one page can show a whole sample and another page can show just part of it. Each snippet gets every line between its own start and end markers. Marker lines are never included, so the outer snippet doesn't show the inner snippet's comments. ```go // @@@SNIPSTART hellouniverse-full func HelloUniverse() { // @@@SNIPSTART hellouniverse-print fmt.Println("Hello Universe!") // @@@SNIPEND hellouniverse-print } // @@@SNIPEND hellouniverse-full ``` Here `hellouniverse-full` is the whole function and `hellouniverse-print` is only the `fmt.Println` line. You can put the snippet's identifier after `@@@SNIPEND`. It's optional, but use it whenever snippets nest. A named end closes the snippet with that identifier, which also lets snippets overlap without being strictly nested. An unnamed `@@@SNIPEND` closes the most recently opened snippet. If other snippets are still open at that point, snipsync logs a warning because the end is ambiguous; set `require_named_nested_ends` to make it an error instead. ### Target files In the target files wrap the location with comments that reference the identifier of the code snippet that will be placed there: ```md ``` In the example above, the "hellouniverse" code snippet will be spliced between the comments. Any text inside of the placeholders will be replaced by the code snippet when the tool runs. The tool will automatically specify the code type for markdown rendering. For example, if the source file ends in ".go" then the code section will be written like this: ` ```go ` #### Per-snip features To customize how a single snip is rendered, add a JSON feature configuration in the snip start line. ```md ``` **Selected lines** A single source code snippet may be used in multiple places. If so, you may wish to customize which lines are rendered. Add a "selected" configuration to the snip start line. ``` ``` The line numbers are **relative** to the snippet, not the source file. The feature supports multiple selections as either a single line or a range. **Highlighed lines** Some frameworks support highlighting code lines in code blocks. If so, you can add a "highlights" configuration to the snip start line. ``` ``` The line numbers are relative to the published snippet. That means that if selectedLines is used, the line numbers to highlight are relative to the pared down selection that is merged into the Markdown file. If you use Docusuarus, you just need to add some additional CSS: https://docusaurus.io/docs/markdown-features/code-blocks#line-highlighting **Regex snipping** Instead of specifying a set of line numbers to snip, you can use regular expression patterns to mark the start and end of a snip. Specify a `startPattern` and an `endPattern`: ``` ``` **Specifying a source file** If the named snippet you want to extract exists in multiple source repositories, provide the path to only that source file after the snippet name, followed by an `@`: ``` ``` ## Run From the root directory of your project run the following command: ```bash yarn snipsync ``` ### Sync only some files Pass `--target` with a [glob](https://www.npmjs.com/package/glob) to splice a subset of your target files: ```bash yarn snipsync --target "docs/develop/dotnet/index.mdx" yarn snipsync --target "docs/develop/dotnet/**/*.mdx" ``` Origins are still downloaded and all snippets extracted, so only the splice step gets faster. Scoping to a single page in the Temporal documentation repository takes a run from about 90 seconds to under 20. Notes on the glob: - It resolves from the directory you run the command in. - It replaces `targets` rather than filtering it, so it can match files outside your configured target directories. - `allowed_target_extensions` still applies. - Quote it and pass it as a separate argument, so your shell does not expand it. ### Remove snippets In some cases, you may want to remove the snippets from your target files. Use the `--clear` flag to do that: ``` yarn snipsync --clear ``` `--clear` takes `--target` too: ```bash yarn snipsync --clear --target "docs/develop/dotnet/index.mdx" ``` ## Development The snipsync tool is set up to run its own functionality during development. Git ignores the `snipsync.config.yaml` file and the `/docs` directory within the package itself. While developing, you can add files to `/docs` define the `snipsync.config.yaml` file, and run `yarn dev` to run snipsync from the root of the repo. To clear the snippets run `yarn dev --clear` ### Testing Run `yarn test` from the root of the repo to run the testing suites. Many tests download the sample repos from GitHub, which allows 60 unauthenticated API requests per hour. If you hit that limit, set `GITHUB_TOKEN` to a GitHub token and snipsync will use it to authenticate: ```bash GITHUB_TOKEN=$(gh auth token) yarn test ```