markdown-code-extract
v1.0.2
Published
Extract dedicated code blocks from markdown files to 'real' code files for validation, application, reusing etc.
Downloads
161
Readme
Extract dedicated code blocks from markdown files to "real" code files
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 <[email protected]>
Synopsis
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')Parameters
mdFile
Optional: Name / path of Markdown file to extract code blocks from
- Default:
./README.md - Use placeholder
#for 'use default' if you only want to specify other parameters
outputRoot
Optional: Path of an existing directory to write code files to
- Default:
./ - Use placeholder
#for 'use default' if you only want to specify other parameters
logLevel
Optional: Log process details
- Possible values:
0= no logging,1= some logging - Default:
0 - Use placeholder
#for 'use default' if you only want to specify other parameters
Returns
Promise
- Since
markdownCodeExtractmainly acts as a batch with error throwing you may need only.catchbut notawait/.then
Demo
Dependencies
- npm packages:
- is-binary-file-simple: Check with minimal effort if a file is binary, e.g. before trying to apply text file methods
Installation
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-extractSource Code
License
- MIT (see LICENSE.txt)
References
is-binary-file-simple
- https://hh-lohmann.github.io/is-binary-file-simple
- in npm registry: https://npmjs.com/package/is-binary-file-simple
- at npmx.dev: https://npmx.dev/package/is-binary-file-simple
