mdast to nlcst Transformer

7.0.1 · active · verified Sun Apr 19

mdast-util-to-nlcst is a utility in the unifiedjs ecosystem that transforms an mdast (markdown abstract syntax tree) into an nlcst (natural language concrete syntax tree). Currently at stable version 7.0.1, the package follows a semver release cadence with notable breaking changes often accompanying major version bumps. Its core function is to allow inspection of the natural language content within markdown documents by providing a structured NLCST representation. Unlike other utilities like `mdast-util-to-hast` which converts markdown to HTML, this package focuses specifically on language processing. It is frequently used in conjunction with natural language parsers like `parse-english` or `parse-latin` and is wrapped by the `remark-retext` plugin for a higher-level abstraction. A key limitation is that it does not provide functionality to apply changes from the NLCST back into the original mdast tree.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting markdown content from a VFile into an mdast tree, then transforming it into an nlcst tree using `ParseEnglish`, and inspecting the resulting natural language AST.

import { fromMarkdown } from 'mdast-util-from-markdown'
import { toNlcst } from 'mdast-util-to-nlcst'
import { ParseEnglish } from 'parse-english'
import { read } from 'to-vfile'
import { inspect } from 'unist-util-inspect'
import { VFile } from 'vfile'

async function processMarkdown() {
  // Simulate reading a markdown file
  const markdownContent = 'Some *foo*sball.\n\nA second sentence.'
  const file = new VFile({
    path: 'example.md',
    value: markdownContent
  })

  // Convert markdown string to mdast tree
  const mdast = fromMarkdown(file.value.toString(), { 
    // Extensions might be needed for full markdown features, but not for basic text.
  })

  // Ensure the mdast tree has positional information, which `fromMarkdown` usually provides.
  // The `file` object is crucial here as `toNlcst` expects it.
  const nlcst = toNlcst(mdast, file, ParseEnglish)

  // Inspect the resulting nlcst tree
  console.log(inspect(nlcst))
}

processMarkdown().catch(console.error)

view raw JSON →