Markdown AST Injection Utility

2.0.0 · active · verified Sun Apr 19

md-node-inject is a utility for programmatically injecting Markdown Abstract Syntax Tree (AST) nodes into specific sections of another Markdown AST. Currently at stable version 2.0.0, this package is designed to work with parsed Markdown content, allowing for precise content integration without string manipulation. It differentiates itself by operating directly on ASTs, which prevents common issues associated with regex-based text replacement and ensures syntactically correct output. The library is ESM-only and requires Node.js v14 or higher for execution. It's often used in conjunction with other AST parsing and serialization libraries like `markdown-to-ast` and `ast-to-markdown` to form a complete Markdown processing pipeline.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing two Markdown strings into ASTs, injecting one AST (representing API documentation) into a specific section ('API') of the other, and then converting the resulting merged AST back into a Markdown string.

import ast from 'markdown-to-ast';
import toMarkdown from 'ast-to-markdown';
import inject from 'md-node-inject';

const mdContent = `
  # Sample
  Description

  # API

  # License
  MIT
`;

const mdApiContent = `
  ## method()
  Method description
`;

const mdContentAst = ast.parse(mdContent);
const mdApiContentAst = ast.parse(mdApiContent);

const injectionSection = 'API';
const mergedContentAst = inject(injectionSection, mdContentAst, mdApiContentAst);

const mergedContent = toMarkdown(mergedContentAst);

console.log(mergedContent);

view raw JSON →