rewrite.md
v1.0.0
Published
A programmable Markdown transformer for safe rewriting, dynamic injection, and front-matter processing
Maintainers
Readme
rewrite.md
A programmable Markdown transformer for safe rewriting, dynamic injection, and front-matter processing
Installation
npm install rewrite.md
# or
pnpm add rewrite.md
# or
bun add rewrite.mdUsage
rewriteMdComment
Basic Example
import { rewriteMdComment } from 'rewrite.md';
const md = `
# Project
<!-- version:start -->
0.0.0
<!-- version:end -->
## License
MIT
`;
const result = rewriteMdComment(md, {
version: '1.2.3',
});
console.log(result);Output:
# Project
<!-- version:start -->
1.2.3
<!-- version:end -->
## License
MITMultiple Blocks
You can rewrite multiple sections at once:
const md = `
<!-- version:start -->0.0.0<!-- version:end -->
<!-- author:start -->unknown<!-- author:end -->
`;
const result = rewriteMdComment(md, {
version: '2.0.0',
author: 'TenE',
});Custom Markers
Change the default start / end markers:
const md = `
<!-- stats:begin -->
old value
<!-- stats:finish -->
`;
const result = rewriteMdComment(
md,
{ stats: '⭐ 120 stars' },
{
startMarker: 'begin',
endMarker: 'finish',
},
);Buffer Support
If you pass a Buffer, the return type depends on preserveType.
const buffer = Buffer.from(`
<!-- build:start -->
pending
<!-- build:end -->
`);
const result = rewriteMdComment(buffer, { build: 'success' }, { preserveType: true });
console.log(Buffer.isBuffer(result)); // trueReal-World Example (Sync Monorepo Package List)
import fs from 'node:fs';
import { rewriteMdComment } from 'rewrite.md';
const packages = ['core', 'cli', 'plugin'];
const table = packages.map((p) => `- @myorg/${p}`).join('\n');
const readme = fs.readFileSync('README.md', 'utf8');
const updated = rewriteMdComment(readme, {
packages: table,
});
fs.writeFileSync('README.md', updated);Marker Format
Blocks must follow this structure:
<!-- key:start -->
content to replace
<!-- key:end -->keycorresponds to a property in thedataobjectstartandendare configurable via options
