npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kuankuan/k-markdown-parser

v1.0.11

Published

a Markdown parser

Downloads

44

Readme

k-markdown-parser

此模块是宽宽独立开发、维护的 Markdown 解析器。用于在各个平台上运行,无需依赖任何运行时环境。

特点

  • 无依赖
  • 可扩展、可定制
  • 大量依赖正则表达式

使用

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

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

article 是解析器返回的 KMarkdownNode 对象,即根节点,具体的说,它是KMarkdownRootNode对象。

对于每个KMarkdownNode,他有以下属性:

  • content: 节点的内容,它是一个(string|KMarkdownNode<Record<string, any>>)[]数组
  • args: 节点的参数,这个属性的类型取决于节点的类型
  • _canParseSubContent: 用于标记当前节点是否可以解析子节点

对于KMarkdownParser,它有两个其他方法:

  • toPlant : 将 KMarkdownNode 中,被解析器替换、转义后的文本转换为普通文本
  • toMarkdown : 将 KMarkdownNode 中,被解析器替换、转义后的文本转换为 Markdown 格式

配置&定制

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

这是一个KMarkdownSyntax二维数组,用于定义 Markdown 解析器支持的语法。

你可以在@kuankuan/k-markdown-parser/options.js导出的defaultSyntaxes中看到默认设置。

之所以将其设定为二维数组,是为了便于语法的各种嵌套。也就是说,我们给语法设定了等级。从某个等级的语法产生的节点如果需要被递归解析,它将仅适用比产生该节点的语法等级相等或者更低的语法。举个例子:

syntaxes= [
    [代码块匹配语法,xml块匹配语法],
    [有序列表语法,无序列表语法],
    [加粗语法,删除线语法],
]

在这个设置中,一个无序列表语法产生的节点的内容将会按照有序列表=>无序列表=>加粗=>删除线的顺序解析。

  • replacerTagStart & replacerTagMap: 这将决定转义字符的处理方式,

KMarkdownParser 将会把 Markdown 中的特殊字符或转义字符以replacerTagStart+replacerTagMap[字符]中的格式替换,以便于解析。比如:

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

在这个配置中\字符将被¨AA替换,(将被¨BB替换,)将被¨CC替换。

  • nodeMap: 这是一个映射关系,用于将不同的 Markdown 节点类型映射到自定义的节点类型。你可以通过修改这个映射来实现自定义的 Markdown 节点类型。你可以在@kuankuan/k-markdown-parser/options.js导出的defaultNodeMap中看到默认设置。

开源

本项目使用MulanPSL-2.0开源协议。