节点- html- 标记
NHM是一个 快点 HTML用于标记下转换器,兼容节点和浏览器.
它的建立铭记了以下两个目标:
1. 速度
我们每天都需要转换HTML的千兆字节。 所有我们发现的图书馆的节点都太慢了. 我们考虑使用一种低级语言,但决定尝试写一些会挤出每一点的东西 表现出JIT 我们可以。 最终结果已经足够快了!
2. 人的可读性
我们测试过的其他图书馆产出将在许多条件下破解,产出与许多 重复线条等。 一般来说,在打分取景器之外,结果不容易被读取.
我们采取的方法是: 干净、简洁 得出一致的间隔规则.
安装
<yarn|npm|pnpm> add node-html-markdown
基准
-----------------------------------------------------------------------------
Estimated processing times (fastest to slowest):
[node-html-markdown (reused instance)]
100 kB: 17ms
1 MB: 176ms
50 MB: 8.80sec
1 GB: 3min, 0sec
50 GB: 2hr, 30min, 14sec
[turndown (reused instance)]
100 kB: 27ms
1 MB: 280ms
50 MB: 13.98sec
1 GB: 4min, 46sec
50 GB: 3hr, 58min, 35sec
-----------------------------------------------------------------------------
Speed comparison - node-html-markdown (reused instance) is:
1.02 times as fast as node-html-markdown
1.57 times as fast as turndown
1.59 times as fast as turndown (reused instance)
-----------------------------------------------------------------------------
使用量
import { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } from 'node-html-markdown'
/* ********************************************************* *
* Single use
* If using it once, you can use the static method
* ********************************************************* */
// Single file
NodeHtmlMarkdown.translate(
/* html */ `<b>hello</b>`,
/* options (optional) */ {},
/* customTranslators (optional) */ undefined,
/* customCodeBlockTranslators (optional) */ undefined
);
// Multiple files
NodeHtmlMarkdown.translate(
/* FileCollection */ {
'file1.html': `<b>hello</b>`,
'file2.html': `<b>goodbye</b>`
},
/* options (optional) */ {},
/* customTranslators (optional) */ undefined,
/* customCodeBlockTranslators (optional) */ undefined
);
/* ********************************************************* *
* Re-use
* If using it several times, creating an instance saves time
* ********************************************************* */
const nhm = new NodeHtmlMarkdown(
/* options (optional) */ {},
/* customTransformers (optional) */ undefined,
/* customCodeBlockTranslators (optional) */ undefined
);
// Single file
nhm.translate(/* html */ `<b>hello</b>`);
// Multiple Files
nhm.translate(
/* FileCollection */ {
'file1.html': `<b>hello</b>`,
'file2.html': `<b>goodbye</b>`
},
);
选项
export interface NodeHtmlMarkdownOptions {
/**
* Use native window DOMParser when available
* @default false
*/
preferNativeParser: boolean,
/**
* Code block fence
* @default ```
*/
codeFence: string,
/**
* Bullet marker
* @default *
*/
bulletMarker: string,
/**
* Style for code block
* @default fence
*/
codeBlockStyle: 'indented' | 'fenced',
/**
* Emphasis delimiter
* @default _
*/
emDelimiter: string,
/**
* Strong delimiter
* @default **
*/
strongDelimiter: string,
/**
* Strike delimiter
* @default ~~
*/
strikeDelimiter: string,
/**
* Supplied elements will be ignored (ignores inner text does not parse children)
*/
ignore?: string[],
/**
* Supplied elements will be treated as blocks (surrounded with blank lines)
*/
blockElements?: string[],
/**
* Max consecutive new lines allowed
* @default 3
*/
maxConsecutiveNewlines: number,
/**
* Line Start Escape pattern
* (Note: Setting this will override the default escape settings, you might want to use textReplace option instead)
*/
lineStartEscape: [ pattern: RegExp, replacement: string ]
/**
* Global escape pattern
* (Note: Setting this will override the default escape settings, you might want to use textReplace option instead)
*/
globalEscape: [ pattern: RegExp, replacement: string ]
/**
* User-defined text replacement pattern (Replaces matching text retrieved from nodes)
*/
textReplace?: [ pattern: RegExp, replacement: string ][]
/**
* Keep images with data: URI (Note: These can be up to 1MB each)
* @example
* <img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSK......0o/">
* @default false
*/
keepDataImages?: boolean
/**
* Place URLS at the bottom and format links using link reference definitions
*
* @example
* Click <a href="/url1">here</a>. Or <a href="/url2">here</a>. Or <a href="/url1">this link</a>.
*
* Becomes:
* Click [here][1]. Or [here][2]. Or [this link][1].
*
* [1]: /url
* [2]: /url2
*/
useLinkReferenceDefinitions?: boolean
/**
* Wrap URL text in < > instead of []() syntax.
*
* @example
* The input <a href="https://google.com">https://google.com</a>
* becomes <https://google.com>
* instead of [https://google.com](https://google.com)
*
* @default true
*/
useInlineLinks?: boolean
}
自定义翻译
自定义翻译是允许以特定方式处理某些元素的高级选项.
可通过 NodeHtmlMarkdown#translators 属性,或创建时添加.
关于如何使用它们的细节,见数字 :
这个 NodeHtmlMarkdown#codeBlockTranslators 属性是处理 a 内元素的翻译器集合 <pre><code> 块.
进一步改进
作为一个以表演为中心的图书馆,我们总是对进一步改进感兴趣. 有几种可能的途径,我们可以通过这些途径取得比目前模式更大的业绩.
这些方法包括:
- 写入自定义解析器
- 整合一个基于合成工字的多线程模型
- 完全替换任何剩余的正则克
执行这些将很有趣;然而,目前,目前的图书馆对我来说已经足够快了。 那个 说,我欢迎讨论 和任何公关 努力进一步改善业绩, 我最终可能做更多的 将来要以此身份工作!
帮助通缉犯!
想做贡献吗? 看看我们 需要帮助 列出一个良好的起始位置 !