Skip to content

kuankuan2007/k-markdown-parser

Repository files navigation

k-markdown-parser

This module is an independent development and maintenance project by Kuankuan, a Markdown parser designed to run on various platforms without relying on any runtime environment.

Features

  • No dependencies
  • Extensible and customizable
  • Heavily relies on regular expressions

Usage

import KMarkdownParser from '@kuankuan/k-markdown-parser';

const parser = new KMarkdownParser({
    ...//options
});
const article = parser.parse('# Hello World');

article is the KMarkdownNode object returned by the parser, which is the root node. Specifically, it is a KMarkdownRootNode object.

Each KMarkdownNode has the following properties:

  • content: The content of the node, which is an array of (string|KMarkdownNode<Record<string, any>>)[]
  • args: The arguments of the node, the type of this property depends on the type of the node
  • _canParseSubContent: Used to mark whether the current node can parse sub-nodes

For KMarkdownParser, it has two other methods:

  • toPlant: Converts the text in KMarkdownNode that has been replaced and escaped by the parser to plain text
  • toMarkdown: Converts the text in KMarkdownNode that has been replaced and escaped by the parser to Markdown format

Configuration & Customization

type Option = Readonly<{
  syntaxes?: KMarkdownSyntax[][];
  replacerTagStart?: string;
  replacerTagMap?: {
    '\\': string;
    [key: string]: string;
  };
  nodeMap?: {
    [key: string]: KMarkdownNode<Record<string, any>>;
}>;
  • syntaxes: Syntax definitions

This is a two-dimensional array of KMarkdownSyntax, used to define the syntax supported by the Markdown parser.

You can see the default settings in defaultSyntaxes exported from @kuankuan/k-markdown-parser/options.js.

It is set as a two-dimensional array to facilitate various levels of syntax nesting. That is, we assign levels to syntax. If a node generated by a certain level of syntax needs to be recursively parsed, it will only apply syntaxes of equal or lower levels than the syntax that generated the node. For example:

syntaxes = [
    [code block matching syntax, XML block matching syntax],
    [ordered list syntax, unordered list syntax],
    [bold syntax, strikethrough syntax],
]

In this setting, the content of a node generated by an unordered list syntax will be parsed in the order of ordered list => unordered list => bold => strikethrough.

  • replacerTagStart & replacerTagMap: These determine how escape characters are handled,

KMarkdownParser will replace special characters or escape characters in Markdown with the format replacerTagStart + replacerTagMap[character] to facilitate parsing. For example:

replacerTagStart = '¨';
replacerTagMap = {
  '\\': 'AA',
  '(': 'BB',
  ')': 'CC',
};

In this configuration, the \ character will be replaced with ¨AA, ( with ¨BB, and ) with ¨CC.

  • nodeMap: This is a mapping relationship used to map different Markdown node types to custom node types. You can modify this mapping to implement custom Markdown node types. You can see the default settings in defaultNodeMap exported from @kuankuan/k-markdown-parser/options.js.

License

This project is licensed under the MulanPSL-2.0.

About

a Markdown parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published