Scan a Markdown file for code blocks prepended with a local file link and write the code block’s content to the file defined in the link, the file / path of the link are created if not existing yet.
Basic use case is e.g. to validate code examples in a README.md, use them directly for automated tests and reuse them for demos, i.e. to avoid the effort and notorious errors with copy and paste of code and divergence of intentionally identical code at different places.
Also possible is e.g. extracting examples from the README.md of another project to reuse them in the current project or pushing examples of the current project to a centralized storage.
The basic idea and code is forked from the ingenious markdown-code-extractor (v1.4.0).
hh lohmann <hh.lohmann@gmail.com>
import { markdownCodeExtract } from 'markdown-code-extract'
[await] markdownCodeExtract()
[await] markdownCodeExtract(mdFile)
[await] markdownCodeExtract(mdFile,outputRoot)
[await] markdownCodeExtract('examples.md')
[await] markdownCodeExtract('../examples/js.md')
[await] markdownCodeExtract('#','../examples/js.md')
Optional: Name / path of Markdown file to extract code blocks from
./README.md# for ‘use default’ if you only want to specify other parametersOptional: Path of an existing directory to write code files to
./# for ‘use default’ if you only want to specify other parametersOptional: Log process details
0 = no logging, 1 = some logging0# for ‘use default’ if you only want to specify other parametersPromise
markdownCodeExtract mainly acts as a batch with error throwing you may need only .catch but not await / .thenSee demos
Pick for your preferred package manager:
npm i markdown-code-extract
pnpm i markdown-code-extract
bun i markdown-code-extract
# For Yarn you should double check docs for your and / or
# current Yarn version, newer versions do not treat `i package_name`
# as an alias for `add ...` and exclude global installations
yarn add markdown-code-extract