@diplodoc/transform 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.diplodoc.md +38 -0
- package/CHANGELOG.md +404 -0
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/README.ru.md +36 -0
- package/dist/css/yfm.css +736 -0
- package/dist/css/yfm.css.map +7 -0
- package/dist/css/yfm.min.css +2 -0
- package/dist/css/yfm.min.css.map +7 -0
- package/dist/js/code.d.ts +1 -0
- package/dist/js/cut.d.ts +1 -0
- package/dist/js/index.d.ts +5 -0
- package/dist/js/polyfill.d.ts +1 -0
- package/dist/js/tabs.d.ts +1 -0
- package/dist/js/term/index.d.ts +1 -0
- package/dist/js/term/utils.d.ts +10 -0
- package/dist/js/utils.d.ts +2 -0
- package/dist/js/yfm.js +432 -0
- package/dist/js/yfm.js.map +7 -0
- package/dist/js/yfm.min.js +2 -0
- package/dist/js/yfm.min.js.map +7 -0
- package/lib/getObject.d.ts +2 -0
- package/lib/getObject.js +11 -0
- package/lib/getObject.js.map +1 -0
- package/lib/headings.d.ts +4 -0
- package/lib/headings.js +73 -0
- package/lib/headings.js.map +1 -0
- package/lib/highlight.d.ts +3 -0
- package/lib/highlight.js +29 -0
- package/lib/highlight.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +35 -0
- package/lib/index.js.map +1 -0
- package/lib/liquid/conditions.d.ts +4 -0
- package/lib/liquid/conditions.js +181 -0
- package/lib/liquid/conditions.js.map +1 -0
- package/lib/liquid/cycles.d.ts +4 -0
- package/lib/liquid/cycles.js +160 -0
- package/lib/liquid/cycles.js.map +1 -0
- package/lib/liquid/errors.d.ts +3 -0
- package/lib/liquid/errors.js +13 -0
- package/lib/liquid/errors.js.map +1 -0
- package/lib/liquid/evaluation.d.ts +3 -0
- package/lib/liquid/evaluation.js +138 -0
- package/lib/liquid/evaluation.js.map +1 -0
- package/lib/liquid/filters.d.ts +9 -0
- package/lib/liquid/filters.js +22 -0
- package/lib/liquid/filters.js.map +1 -0
- package/lib/liquid/index.d.ts +9 -0
- package/lib/liquid/index.js +87 -0
- package/lib/liquid/index.js.map +1 -0
- package/lib/liquid/lexical.d.ts +14 -0
- package/lib/liquid/lexical.js +79 -0
- package/lib/liquid/lexical.js.map +1 -0
- package/lib/liquid/services/argv.d.ts +15 -0
- package/lib/liquid/services/argv.js +17 -0
- package/lib/liquid/services/argv.js.map +1 -0
- package/lib/liquid/sourceMap.d.ts +24 -0
- package/lib/liquid/sourceMap.js +56 -0
- package/lib/liquid/sourceMap.js.map +1 -0
- package/lib/liquid/substitutions.d.ts +2 -0
- package/lib/liquid/substitutions.js +36 -0
- package/lib/liquid/substitutions.js.map +1 -0
- package/lib/liquid/utils.d.ts +8 -0
- package/lib/liquid/utils.js +24 -0
- package/lib/liquid/utils.js.map +1 -0
- package/lib/log.d.ts +18 -0
- package/lib/log.js +54 -0
- package/lib/log.js.map +1 -0
- package/lib/md.d.ts +14 -0
- package/lib/md.js +96 -0
- package/lib/md.js.map +1 -0
- package/lib/plugins/anchors/collect.d.ts +5 -0
- package/lib/plugins/anchors/collect.js +81 -0
- package/lib/plugins/anchors/collect.js.map +1 -0
- package/lib/plugins/anchors/constants.d.ts +2 -0
- package/lib/plugins/anchors/constants.js +6 -0
- package/lib/plugins/anchors/constants.js.map +1 -0
- package/lib/plugins/anchors/index.d.ts +7 -0
- package/lib/plugins/anchors/index.js +134 -0
- package/lib/plugins/anchors/index.js.map +1 -0
- package/lib/plugins/changelog/collect.d.ts +8 -0
- package/lib/plugins/changelog/collect.js +50 -0
- package/lib/plugins/changelog/collect.js.map +1 -0
- package/lib/plugins/changelog/index.d.ts +6 -0
- package/lib/plugins/changelog/index.js +131 -0
- package/lib/plugins/changelog/index.js.map +1 -0
- package/lib/plugins/changelog/types.d.ts +10 -0
- package/lib/plugins/changelog/types.js +3 -0
- package/lib/plugins/changelog/types.js.map +1 -0
- package/lib/plugins/checkbox/checkbox.d.ts +7 -0
- package/lib/plugins/checkbox/checkbox.js +91 -0
- package/lib/plugins/checkbox/checkbox.js.map +1 -0
- package/lib/plugins/checkbox/index.d.ts +8 -0
- package/lib/plugins/checkbox/index.js +12 -0
- package/lib/plugins/checkbox/index.js.map +1 -0
- package/lib/plugins/code.d.ts +3 -0
- package/lib/plugins/code.js +61 -0
- package/lib/plugins/code.js.map +1 -0
- package/lib/plugins/cut.d.ts +3 -0
- package/lib/plugins/cut.js +70 -0
- package/lib/plugins/cut.js.map +1 -0
- package/lib/plugins/deflist.d.ts +3 -0
- package/lib/plugins/deflist.js +8 -0
- package/lib/plugins/deflist.js.map +1 -0
- package/lib/plugins/file/const.d.ts +27 -0
- package/lib/plugins/file/const.js +47 -0
- package/lib/plugins/file/const.js.map +1 -0
- package/lib/plugins/file/file.d.ts +8 -0
- package/lib/plugins/file/file.js +63 -0
- package/lib/plugins/file/file.js.map +1 -0
- package/lib/plugins/file/index.d.ts +4 -0
- package/lib/plugins/file/index.js +9 -0
- package/lib/plugins/file/index.js.map +1 -0
- package/lib/plugins/images/collect.d.ts +8 -0
- package/lib/plugins/images/collect.js +43 -0
- package/lib/plugins/images/collect.js.map +1 -0
- package/lib/plugins/images/index.d.ts +10 -0
- package/lib/plugins/images/index.js +81 -0
- package/lib/plugins/images/index.js.map +1 -0
- package/lib/plugins/imsize/const.d.ts +7 -0
- package/lib/plugins/imsize/const.js +12 -0
- package/lib/plugins/imsize/const.js.map +1 -0
- package/lib/plugins/imsize/helpers.d.ts +6 -0
- package/lib/plugins/imsize/helpers.js +64 -0
- package/lib/plugins/imsize/helpers.js.map +1 -0
- package/lib/plugins/imsize/index.d.ts +8 -0
- package/lib/plugins/imsize/index.js +12 -0
- package/lib/plugins/imsize/index.js.map +1 -0
- package/lib/plugins/imsize/plugin.d.ts +3 -0
- package/lib/plugins/imsize/plugin.js +188 -0
- package/lib/plugins/imsize/plugin.js.map +1 -0
- package/lib/plugins/includes/collect.d.ts +8 -0
- package/lib/plugins/includes/collect.js +49 -0
- package/lib/plugins/includes/collect.js.map +1 -0
- package/lib/plugins/includes/index.d.ts +8 -0
- package/lib/plugins/includes/index.js +98 -0
- package/lib/plugins/includes/index.js.map +1 -0
- package/lib/plugins/links/collect.d.ts +7 -0
- package/lib/plugins/links/collect.js +74 -0
- package/lib/plugins/links/collect.js.map +1 -0
- package/lib/plugins/links/constants.d.ts +1 -0
- package/lib/plugins/links/constants.js +5 -0
- package/lib/plugins/links/constants.js.map +1 -0
- package/lib/plugins/links/index.d.ts +24 -0
- package/lib/plugins/links/index.js +178 -0
- package/lib/plugins/links/index.js.map +1 -0
- package/lib/plugins/meta.d.ts +3 -0
- package/lib/plugins/meta.js +8 -0
- package/lib/plugins/meta.js.map +1 -0
- package/lib/plugins/monospace.d.ts +3 -0
- package/lib/plugins/monospace.js +109 -0
- package/lib/plugins/monospace.js.map +1 -0
- package/lib/plugins/notes.d.ts +3 -0
- package/lib/plugins/notes.js +103 -0
- package/lib/plugins/notes.js.map +1 -0
- package/lib/plugins/sup.d.ts +3 -0
- package/lib/plugins/sup.js +8 -0
- package/lib/plugins/sup.js.map +1 -0
- package/lib/plugins/table/index.d.ts +3 -0
- package/lib/plugins/table/index.js +215 -0
- package/lib/plugins/table/index.js.map +1 -0
- package/lib/plugins/tabs.d.ts +4 -0
- package/lib/plugins/tabs.js +4 -0
- package/lib/plugins/tabs.js.map +1 -0
- package/lib/plugins/term/constants.d.ts +1 -0
- package/lib/plugins/term/constants.js +5 -0
- package/lib/plugins/term/constants.js.map +1 -0
- package/lib/plugins/term/index.d.ts +3 -0
- package/lib/plugins/term/index.js +108 -0
- package/lib/plugins/term/index.js.map +1 -0
- package/lib/plugins/term/termDefinitions.d.ts +4 -0
- package/lib/plugins/term/termDefinitions.js +125 -0
- package/lib/plugins/term/termDefinitions.js.map +1 -0
- package/lib/plugins/typings.d.ts +12 -0
- package/lib/plugins/typings.js +3 -0
- package/lib/plugins/typings.js.map +1 -0
- package/lib/plugins/utils.d.ts +6 -0
- package/lib/plugins/utils.js +30 -0
- package/lib/plugins/utils.js.map +1 -0
- package/lib/plugins/video/const.d.ts +10 -0
- package/lib/plugins/video/const.js +22 -0
- package/lib/plugins/video/const.js.map +1 -0
- package/lib/plugins/video/index.d.ts +8 -0
- package/lib/plugins/video/index.js +104 -0
- package/lib/plugins/video/index.js.map +1 -0
- package/lib/plugins/video/parsers.d.ts +6 -0
- package/lib/plugins/video/parsers.js +64 -0
- package/lib/plugins/video/parsers.js.map +1 -0
- package/lib/plugins/video/types.d.ts +34 -0
- package/lib/plugins/video/types.js +3 -0
- package/lib/plugins/video/types.js.map +1 -0
- package/lib/plugins/video/utils.d.ts +2 -0
- package/lib/plugins/video/utils.js +25 -0
- package/lib/plugins/video/utils.js.map +1 -0
- package/lib/plugins.d.ts +3 -0
- package/lib/plugins.js +36 -0
- package/lib/plugins.js.map +1 -0
- package/lib/sanitize.d.ts +12 -0
- package/lib/sanitize.js +572 -0
- package/lib/sanitize.js.map +1 -0
- package/lib/title.d.ts +7 -0
- package/lib/title.js +18 -0
- package/lib/title.js.map +1 -0
- package/lib/typings.d.ts +57 -0
- package/lib/typings.js +3 -0
- package/lib/typings.js.map +1 -0
- package/lib/utils.d.ts +13 -0
- package/lib/utils.js +79 -0
- package/lib/utils.js.map +1 -0
- package/lib/utilsFS.d.ts +27 -0
- package/lib/utilsFS.js +89 -0
- package/lib/utilsFS.js.map +1 -0
- package/lib/yfmlint/index.d.ts +26 -0
- package/lib/yfmlint/index.js +72 -0
- package/lib/yfmlint/index.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/index.d.ts +9 -0
- package/lib/yfmlint/markdownlint-custom-rule/index.js +22 -0
- package/lib/yfmlint/markdownlint-custom-rule/index.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm001.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm001.js +28 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm001.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm002.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm002.js +33 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm002.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm003.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm003.js +33 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm003.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm004.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm004.js +28 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm004.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm005.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm005.js +28 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm005.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm006.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm006.js +28 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm006.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm007.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm007.js +29 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm007.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm008.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm008.js +28 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm008.js.map +1 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm009.d.ts +2 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm009.js +47 -0
- package/lib/yfmlint/markdownlint-custom-rule/yfm009.js.map +1 -0
- package/lib/yfmlint/typings.d.ts +12 -0
- package/lib/yfmlint/typings.js +3 -0
- package/lib/yfmlint/typings.js.map +1 -0
- package/lib/yfmlint/utils.d.ts +9 -0
- package/lib/yfmlint/utils.js +22 -0
- package/lib/yfmlint/utils.js.map +1 -0
- package/lib/yfmlint/yfmlint.d.ts +3 -0
- package/lib/yfmlint/yfmlint.js +69 -0
- package/lib/yfmlint/yfmlint.js.map +1 -0
- package/package.json +106 -0
- package/src/.eslintrc +10 -0
- package/src/js/code.ts +65 -0
- package/src/js/cut.ts +57 -0
- package/src/js/index.ts +6 -0
- package/src/js/polyfill.js +27 -0
- package/src/js/term/index.ts +79 -0
- package/src/js/term/utils.ts +151 -0
- package/src/js/utils.ts +9 -0
- package/src/scss/_anchor.scss +33 -0
- package/src/scss/_code.scss +23 -0
- package/src/scss/_common.scss +407 -0
- package/src/scss/_cut.scss +42 -0
- package/src/scss/_file.scss +35 -0
- package/src/scss/_highlight.scss +96 -0
- package/src/scss/_note.scss +73 -0
- package/src/scss/_print.scss +20 -0
- package/src/scss/_term.scss +79 -0
- package/src/scss/_variables.scss +16 -0
- package/src/scss/yfm.scss +11 -0
- package/src/transform/getObject.ts +10 -0
- package/src/transform/headings.ts +88 -0
- package/src/transform/highlight.ts +33 -0
- package/src/transform/index.ts +50 -0
- package/src/transform/liquid/conditions.ts +259 -0
- package/src/transform/liquid/cycles.ts +244 -0
- package/src/transform/liquid/errors.ts +10 -0
- package/src/transform/liquid/evaluation.ts +145 -0
- package/src/transform/liquid/filters.ts +19 -0
- package/src/transform/liquid/index.ts +141 -0
- package/src/transform/liquid/lexical.ts +86 -0
- package/src/transform/liquid/services/argv.ts +23 -0
- package/src/transform/liquid/sourceMap.ts +72 -0
- package/src/transform/liquid/substitutions.ts +40 -0
- package/src/transform/liquid/utils.ts +30 -0
- package/src/transform/log.ts +60 -0
- package/src/transform/md.ts +123 -0
- package/src/transform/plugins/anchors/collect.ts +101 -0
- package/src/transform/plugins/anchors/constants.ts +2 -0
- package/src/transform/plugins/anchors/index.ts +166 -0
- package/src/transform/plugins/changelog/collect.ts +66 -0
- package/src/transform/plugins/changelog/index.ts +164 -0
- package/src/transform/plugins/changelog/types.ts +10 -0
- package/src/transform/plugins/checkbox/checkbox.ts +107 -0
- package/src/transform/plugins/checkbox/index.ts +15 -0
- package/src/transform/plugins/code.ts +79 -0
- package/src/transform/plugins/cut.ts +98 -0
- package/src/transform/plugins/deflist.ts +5 -0
- package/src/transform/plugins/file/README.md +34 -0
- package/src/transform/plugins/file/const.ts +46 -0
- package/src/transform/plugins/file/file.ts +82 -0
- package/src/transform/plugins/file/index.ts +10 -0
- package/src/transform/plugins/images/collect.ts +60 -0
- package/src/transform/plugins/images/index.ts +119 -0
- package/src/transform/plugins/imsize/const.ts +7 -0
- package/src/transform/plugins/imsize/helpers.ts +75 -0
- package/src/transform/plugins/imsize/index.ts +14 -0
- package/src/transform/plugins/imsize/plugin.ts +214 -0
- package/src/transform/plugins/includes/collect.ts +73 -0
- package/src/transform/plugins/includes/index.ts +127 -0
- package/src/transform/plugins/links/collect.ts +92 -0
- package/src/transform/plugins/links/constants.ts +1 -0
- package/src/transform/plugins/links/index.ts +218 -0
- package/src/transform/plugins/meta.ts +5 -0
- package/src/transform/plugins/monospace.ts +141 -0
- package/src/transform/plugins/notes.ts +134 -0
- package/src/transform/plugins/sup.ts +5 -0
- package/src/transform/plugins/table/index.ts +290 -0
- package/src/transform/plugins/tabs.ts +3 -0
- package/src/transform/plugins/term/constants.ts +1 -0
- package/src/transform/plugins/term/index.ts +139 -0
- package/src/transform/plugins/term/termDefinitions.ts +177 -0
- package/src/transform/plugins/typings.ts +14 -0
- package/src/transform/plugins/utils.ts +38 -0
- package/src/transform/plugins/video/const.ts +20 -0
- package/src/transform/plugins/video/index.ts +134 -0
- package/src/transform/plugins/video/parsers.ts +65 -0
- package/src/transform/plugins/video/types.ts +27 -0
- package/src/transform/plugins/video/utils.ts +24 -0
- package/src/transform/plugins.ts +35 -0
- package/src/transform/sanitize.ts +596 -0
- package/src/transform/title.ts +22 -0
- package/src/transform/typings.ts +64 -0
- package/src/transform/utils.ts +91 -0
- package/src/transform/utilsFS.ts +128 -0
- package/src/transform/yfmlint/README.md +48 -0
- package/src/transform/yfmlint/index.ts +124 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/index.ts +9 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm001.ts +36 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm002.ts +33 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm003.ts +33 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm004.ts +27 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm005.ts +27 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm006.ts +26 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm007.ts +28 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm008.ts +26 -0
- package/src/transform/yfmlint/markdownlint-custom-rule/yfm009.ts +54 -0
- package/src/transform/yfmlint/typings.ts +13 -0
- package/src/transform/yfmlint/utils.ts +31 -0
- package/src/transform/yfmlint/yfmlint.ts +73 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {bold} from 'chalk';
|
|
2
|
+
import GithubSlugger from 'github-slugger';
|
|
3
|
+
|
|
4
|
+
import {headingInfo} from '../../utils';
|
|
5
|
+
import {CUSTOM_ID_REGEXP, CUSTOM_ID_EXCEPTION} from './constants';
|
|
6
|
+
import StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
7
|
+
import Token from 'markdown-it/lib/token';
|
|
8
|
+
import {escapeHtml} from 'markdown-it/lib/common/utils';
|
|
9
|
+
import {MarkdownItPluginCb} from '../typings';
|
|
10
|
+
|
|
11
|
+
const slugify: (str: string, opts: {}) => string = require('slugify');
|
|
12
|
+
|
|
13
|
+
function createLinkTokens(state: StateCore, id: string, title: string, setId = false) {
|
|
14
|
+
const open = new state.Token('link_open', 'a', 1);
|
|
15
|
+
const close = new state.Token('link_close', 'a', -1);
|
|
16
|
+
|
|
17
|
+
if (setId) {
|
|
18
|
+
open.attrSet('id', id);
|
|
19
|
+
}
|
|
20
|
+
open.attrSet('href', '#' + id);
|
|
21
|
+
open.attrSet('class', 'yfm-anchor');
|
|
22
|
+
open.attrSet('aria-hidden', 'true');
|
|
23
|
+
|
|
24
|
+
// SEO: render invisible heading title because link must have text content.
|
|
25
|
+
const hiddenDesc = new state.Token('anchor_hidden_desc', '', 0);
|
|
26
|
+
|
|
27
|
+
hiddenDesc.content = title;
|
|
28
|
+
|
|
29
|
+
return [open, hiddenDesc, close];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const getCustomIds = (content: string) => {
|
|
33
|
+
const ids: string[] = [];
|
|
34
|
+
|
|
35
|
+
content.replace(CUSTOM_ID_REGEXP, (match, customId) => {
|
|
36
|
+
if (match !== CUSTOM_ID_EXCEPTION) {
|
|
37
|
+
ids.push(customId);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return '';
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return ids.length ? ids : null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const removeCustomId = (content: string) => {
|
|
47
|
+
if (CUSTOM_ID_REGEXP.test(content)) {
|
|
48
|
+
return content
|
|
49
|
+
.replace(CUSTOM_ID_REGEXP, (match) => {
|
|
50
|
+
if (match === CUSTOM_ID_EXCEPTION) {
|
|
51
|
+
return match;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return '';
|
|
55
|
+
})
|
|
56
|
+
.trim();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return content;
|
|
60
|
+
};
|
|
61
|
+
const removeCustomIds = (token: Token) => {
|
|
62
|
+
token.content = removeCustomId(token.content);
|
|
63
|
+
token.children?.forEach((child) => {
|
|
64
|
+
child.content = removeCustomId(child.content);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
interface Options {
|
|
69
|
+
extractTitle?: boolean;
|
|
70
|
+
supportGithubAnchors?: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const index: MarkdownItPluginCb<Options> = (
|
|
74
|
+
md,
|
|
75
|
+
{extractTitle, path, log, supportGithubAnchors},
|
|
76
|
+
) => {
|
|
77
|
+
const plugin = (state: StateCore) => {
|
|
78
|
+
/* Do not use the plugin if it is included in the file */
|
|
79
|
+
if (state.env.includes && state.env.includes.length) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const ids: Record<string, number> = {};
|
|
84
|
+
const tokens = state.tokens;
|
|
85
|
+
let i = 0;
|
|
86
|
+
const slugger = new GithubSlugger();
|
|
87
|
+
|
|
88
|
+
while (i < tokens.length) {
|
|
89
|
+
const token = tokens[i];
|
|
90
|
+
const isHeading = token.type === 'heading_open';
|
|
91
|
+
|
|
92
|
+
if (isHeading) {
|
|
93
|
+
const {title, level} = headingInfo(tokens, i);
|
|
94
|
+
|
|
95
|
+
const inlineToken = tokens[i + 1];
|
|
96
|
+
let id = token.attrGet('id');
|
|
97
|
+
let ghId: string;
|
|
98
|
+
|
|
99
|
+
if (!title) {
|
|
100
|
+
log.warn(`Header without title${path ? ` in ${bold(path)}` : ''}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (level < 2 && extractTitle) {
|
|
104
|
+
i += 3;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const customIds = getCustomIds(inlineToken.content);
|
|
109
|
+
if (customIds) {
|
|
110
|
+
id = customIds[0];
|
|
111
|
+
removeCustomIds(tokens[i + 1]);
|
|
112
|
+
} else {
|
|
113
|
+
id = slugify(title, {
|
|
114
|
+
lower: true,
|
|
115
|
+
remove: /[*+~.()'"!:@`ь]/g,
|
|
116
|
+
});
|
|
117
|
+
ghId = slugger.slug(title);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
token.attrSet('id', id);
|
|
121
|
+
|
|
122
|
+
if (ids[id]) {
|
|
123
|
+
id = id + ids[id]++;
|
|
124
|
+
token.attrSet('id', id);
|
|
125
|
+
} else {
|
|
126
|
+
ids[id] = 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const allAnchorIds = customIds ? customIds : [id];
|
|
130
|
+
const anchorTitle = removeCustomId(title).replace(/`/g, '');
|
|
131
|
+
allAnchorIds.forEach((customId) => {
|
|
132
|
+
const setId = id !== customId;
|
|
133
|
+
const linkTokens = createLinkTokens(state, customId, anchorTitle, setId);
|
|
134
|
+
|
|
135
|
+
inlineToken.children?.unshift(...linkTokens);
|
|
136
|
+
|
|
137
|
+
if (supportGithubAnchors) {
|
|
138
|
+
const ghLinkTokens = createLinkTokens(state, ghId, anchorTitle, true);
|
|
139
|
+
inlineToken.children?.unshift(...ghLinkTokens);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
i += 3;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
i++;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
md.core.ruler.after('includes', 'anchors', plugin);
|
|
153
|
+
} catch {
|
|
154
|
+
try {
|
|
155
|
+
md.core.ruler.after('curly_attributes', 'anchors', plugin);
|
|
156
|
+
} catch {
|
|
157
|
+
md.core.ruler.push('anchors', plugin);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
md.renderer.rules.anchor_hidden_desc = function (tokens, index) {
|
|
162
|
+
return '<span class="visually-hidden">' + escapeHtml(tokens[index].content) + '</span>';
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export = index;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {bold} from 'chalk';
|
|
2
|
+
import {ChangelogItem} from './types';
|
|
3
|
+
import initMarkdownit from '../../md';
|
|
4
|
+
import changelogPlugin from './index';
|
|
5
|
+
import imsize from '../imsize';
|
|
6
|
+
import {MarkdownItPluginOpts} from '../typings';
|
|
7
|
+
|
|
8
|
+
const BLOCK_START = '{% changelog %}';
|
|
9
|
+
const BLOCK_END = '{% endchangelog %}\n';
|
|
10
|
+
|
|
11
|
+
function parseChangelogs(str: string, path?: string) {
|
|
12
|
+
const {parse, compile, env} = initMarkdownit({
|
|
13
|
+
plugins: [changelogPlugin, imsize],
|
|
14
|
+
extractChangelogs: true,
|
|
15
|
+
path,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
compile(parse(str));
|
|
19
|
+
|
|
20
|
+
return env.changelogs || [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type Options = Pick<MarkdownItPluginOpts, 'path' | 'log'> & {
|
|
24
|
+
changelogs?: ChangelogItem[];
|
|
25
|
+
extractChangelogs?: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const collect = (input: string, {path: filepath, log, changelogs, extractChangelogs}: Options) => {
|
|
29
|
+
let result = input;
|
|
30
|
+
let lastPos = 0;
|
|
31
|
+
const rawChanges = [];
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line no-constant-condition
|
|
34
|
+
while (true) {
|
|
35
|
+
const pos = result.indexOf(BLOCK_START, lastPos);
|
|
36
|
+
lastPos = pos;
|
|
37
|
+
if (pos === -1) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
const endPos = result.indexOf(BLOCK_END, pos + BLOCK_START.length);
|
|
41
|
+
if (endPos === -1) {
|
|
42
|
+
log.error(`Changelog block must be closed${filepath ? ` in ${bold(filepath)}` : ''}`);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const change = result.slice(pos, endPos + BLOCK_END.length);
|
|
47
|
+
|
|
48
|
+
rawChanges.push(change);
|
|
49
|
+
|
|
50
|
+
result = result.slice(0, pos) + result.slice(endPos + BLOCK_END.length);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (rawChanges.length && changelogs && extractChangelogs) {
|
|
54
|
+
const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath);
|
|
55
|
+
if (parsedChangelogs.length !== rawChanges.length) {
|
|
56
|
+
log.error(
|
|
57
|
+
`Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
changelogs.push(...parsedChangelogs);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return result;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export = collect;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {MarkdownItPluginCb} from '../typings';
|
|
2
|
+
import Core from 'markdown-it/lib/parser_core';
|
|
3
|
+
import Token from 'markdown-it/lib/token';
|
|
4
|
+
import {bold} from 'chalk';
|
|
5
|
+
import yaml from 'js-yaml';
|
|
6
|
+
import StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
7
|
+
|
|
8
|
+
interface Options {
|
|
9
|
+
extractChangelogs?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const CHANGELOG_OPEN_RE = /^\{% changelog %}/;
|
|
13
|
+
const CHANGELOG_CLOSE_RE = /^\{% endchangelog %}/;
|
|
14
|
+
|
|
15
|
+
function isOpenToken(tokens: Token[], i: number) {
|
|
16
|
+
return (
|
|
17
|
+
tokens[i].type === 'paragraph_open' &&
|
|
18
|
+
tokens[i + 1].type === 'inline' &&
|
|
19
|
+
tokens[i + 2].type === 'paragraph_close' &&
|
|
20
|
+
CHANGELOG_OPEN_RE.test(tokens[i + 1].content)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isCloseToken(tokens: Token[], i: number) {
|
|
25
|
+
return (
|
|
26
|
+
tokens[i]?.type === 'paragraph_open' &&
|
|
27
|
+
tokens[i + 1].type === 'inline' &&
|
|
28
|
+
tokens[i + 2].type === 'paragraph_close' &&
|
|
29
|
+
CHANGELOG_CLOSE_RE.test(tokens[i + 1].content)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isTitle(tokens: Token[], i = 0) {
|
|
34
|
+
return (
|
|
35
|
+
tokens[i].type === 'heading_open' &&
|
|
36
|
+
tokens[i + 1].type === 'inline' &&
|
|
37
|
+
tokens[i + 2].type === 'heading_close'
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isImageParagraph(tokens: Token[], i = 0) {
|
|
42
|
+
return (
|
|
43
|
+
tokens[i].type === 'paragraph_open' &&
|
|
44
|
+
tokens[i + 1].type === 'inline' &&
|
|
45
|
+
tokens[i + 2].type === 'paragraph_close' &&
|
|
46
|
+
tokens[i + 1].children?.some((t) => t.type === 'image')
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseBody(tokens: Token[], state: StateCore) {
|
|
51
|
+
const {md, env} = state;
|
|
52
|
+
|
|
53
|
+
const metadataToken = tokens.shift();
|
|
54
|
+
if (metadataToken?.type !== 'fence') {
|
|
55
|
+
throw new Error('Metadata tag not found');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let metadata: object = {};
|
|
59
|
+
const rawMetadata = yaml.load(metadataToken.content, {
|
|
60
|
+
schema: yaml.JSON_SCHEMA,
|
|
61
|
+
});
|
|
62
|
+
if (rawMetadata && typeof rawMetadata === 'object') {
|
|
63
|
+
metadata = rawMetadata;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!isTitle(tokens)) {
|
|
67
|
+
throw new Error('Title tag not found');
|
|
68
|
+
}
|
|
69
|
+
const title = tokens.splice(0, 3)[1].content;
|
|
70
|
+
|
|
71
|
+
let image;
|
|
72
|
+
if (isImageParagraph(tokens)) {
|
|
73
|
+
const paragraphTokens = tokens.splice(0, 3);
|
|
74
|
+
const imageToken = paragraphTokens[1]?.children?.find((token) => token.type === 'image');
|
|
75
|
+
if (imageToken) {
|
|
76
|
+
const width = Number(imageToken.attrGet('width'));
|
|
77
|
+
const height = Number(imageToken.attrGet('height'));
|
|
78
|
+
let ratio;
|
|
79
|
+
if (Number.isFinite(width) && Number.isFinite(height)) {
|
|
80
|
+
ratio = height / width;
|
|
81
|
+
}
|
|
82
|
+
let alt = imageToken.attrGet('title') || '';
|
|
83
|
+
if (!alt && imageToken.children) {
|
|
84
|
+
alt = md.renderer.renderInlineAsText(imageToken.children, md.options, env);
|
|
85
|
+
}
|
|
86
|
+
image = {
|
|
87
|
+
src: imageToken.attrGet('src'),
|
|
88
|
+
alt,
|
|
89
|
+
ratio,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const description = md.renderer.render(tokens, md.options, env);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
...metadata,
|
|
98
|
+
title,
|
|
99
|
+
image,
|
|
100
|
+
description,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const changelog: MarkdownItPluginCb<Options> = function (md, {extractChangelogs, log, path}) {
|
|
105
|
+
const plugin: Core.RuleCore = (state) => {
|
|
106
|
+
const {tokens, env} = state;
|
|
107
|
+
|
|
108
|
+
for (let i = 0, len = tokens.length; i < len; i++) {
|
|
109
|
+
const isOpen = isOpenToken(tokens, i);
|
|
110
|
+
if (!isOpen) continue;
|
|
111
|
+
|
|
112
|
+
const openAt = i;
|
|
113
|
+
let isCloseFound = false;
|
|
114
|
+
while (i < len) {
|
|
115
|
+
i++;
|
|
116
|
+
if (isCloseToken(tokens, i)) {
|
|
117
|
+
isCloseFound = true;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!isCloseFound) {
|
|
123
|
+
log.error(`Changelog close tag in not found: ${bold(path)}`);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const closeAt = i + 2;
|
|
128
|
+
|
|
129
|
+
if (env && extractChangelogs) {
|
|
130
|
+
const content = tokens.slice(openAt, closeAt + 1);
|
|
131
|
+
|
|
132
|
+
// cut open
|
|
133
|
+
content.splice(0, 3);
|
|
134
|
+
// cut close
|
|
135
|
+
content.splice(-3);
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const change = parseBody(content, state);
|
|
139
|
+
|
|
140
|
+
if (!env.changelogs) {
|
|
141
|
+
env.changelogs = [];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
env.changelogs.push(change);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
log.error(`Changelog error: ${(err as Error).message} in ${bold(path)}`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
tokens.splice(openAt, closeAt + 1 - openAt);
|
|
152
|
+
len = tokens.length;
|
|
153
|
+
i = openAt - 1;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
md.core.ruler.before('curly_attributes', 'changelog', plugin);
|
|
159
|
+
} catch (e) {
|
|
160
|
+
md.core.ruler.push('changelog', plugin);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export = changelog;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type MarkdownIt from 'markdown-it';
|
|
2
|
+
import type StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
3
|
+
import type Token from 'markdown-it/lib/token';
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line no-useless-escape
|
|
6
|
+
const pattern = /^\[(X|\s|\_|\-)\]\s(.*)/i;
|
|
7
|
+
|
|
8
|
+
function matchOpenToken(tokens: Token[], i: number) {
|
|
9
|
+
return (
|
|
10
|
+
tokens[i].type === 'paragraph_open' &&
|
|
11
|
+
tokens[i + 1].type === 'inline' &&
|
|
12
|
+
tokens[i + 1].content.match(pattern)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type CheckboxOptions = {
|
|
17
|
+
idPrefix?: string;
|
|
18
|
+
divClass?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions) {
|
|
22
|
+
let lastId = 0;
|
|
23
|
+
const defaults = {
|
|
24
|
+
divClass: 'checkbox',
|
|
25
|
+
idPrefix: 'checkbox',
|
|
26
|
+
};
|
|
27
|
+
const options = Object.assign(defaults, opts);
|
|
28
|
+
|
|
29
|
+
const createTokens = function (state: StateCore, checked: boolean, label: string, i: number) {
|
|
30
|
+
let token: Token;
|
|
31
|
+
const nodes = [];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* <div class="checkbox">
|
|
35
|
+
*/
|
|
36
|
+
token = new state.Token('checkbox_open', 'div', 1);
|
|
37
|
+
token.block = true;
|
|
38
|
+
token.map = state.tokens[i].map;
|
|
39
|
+
token.attrs = [['class', options.divClass]];
|
|
40
|
+
nodes.push(token);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* <input type="checkbox" id="checkbox{n}" checked="true">
|
|
44
|
+
*/
|
|
45
|
+
const id = options.idPrefix + lastId;
|
|
46
|
+
lastId += 1;
|
|
47
|
+
token = new state.Token('checkbox_input', 'input', 0);
|
|
48
|
+
token.block = true;
|
|
49
|
+
token.map = state.tokens[i].map;
|
|
50
|
+
token.attrs = [
|
|
51
|
+
['type', 'checkbox'],
|
|
52
|
+
['id', id],
|
|
53
|
+
['disabled', ''],
|
|
54
|
+
];
|
|
55
|
+
if (checked === true) {
|
|
56
|
+
token.attrs.push(['checked', 'true']);
|
|
57
|
+
}
|
|
58
|
+
nodes.push(token);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* <label for="checkbox{n}">
|
|
62
|
+
*/
|
|
63
|
+
token = new state.Token('checkbox_label_open', 'label', 1);
|
|
64
|
+
token.attrs = [['for', id]];
|
|
65
|
+
nodes.push(token);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* content of label tag
|
|
69
|
+
*/
|
|
70
|
+
token = state.md.parseInline(label, state.env)[0];
|
|
71
|
+
nodes.push(token);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* closing tags
|
|
75
|
+
*/
|
|
76
|
+
token = new state.Token('checkbox_label_close', 'label', -1);
|
|
77
|
+
token.block = true;
|
|
78
|
+
token.map = state.tokens[i].map;
|
|
79
|
+
nodes.push(token);
|
|
80
|
+
token = new state.Token('checkbox_close', 'div', -1);
|
|
81
|
+
token.block = true;
|
|
82
|
+
token.map = state.tokens[i].map;
|
|
83
|
+
nodes.push(token);
|
|
84
|
+
|
|
85
|
+
return nodes;
|
|
86
|
+
};
|
|
87
|
+
const splitTextToken = function (state: StateCore, matches: RegExpMatchArray, i: number) {
|
|
88
|
+
let checked = false;
|
|
89
|
+
const value = matches[1];
|
|
90
|
+
const label = matches[2];
|
|
91
|
+
if (value === 'X' || value === 'x') {
|
|
92
|
+
checked = true;
|
|
93
|
+
}
|
|
94
|
+
return createTokens(state, checked, label, i);
|
|
95
|
+
};
|
|
96
|
+
return function (state: StateCore) {
|
|
97
|
+
const blockTokens = state.tokens;
|
|
98
|
+
for (let i = 0; i < blockTokens.length; i++) {
|
|
99
|
+
const match = matchOpenToken(blockTokens, i);
|
|
100
|
+
if (!match) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
blockTokens.splice(i, 3, ...splitTextToken(state, match, i));
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import {CheckboxOptions, checkboxReplace} from './checkbox';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checkbox plugin for markdown-it.
|
|
6
|
+
* Forked from https://github.com/mcecot/markdown-it-checkbox
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const checkbox = (md: MarkdownIt, options: CheckboxOptions) => {
|
|
10
|
+
md.core.ruler.push('checkbox', checkboxReplace(md, options));
|
|
11
|
+
|
|
12
|
+
return md;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export = checkbox;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
|
|
3
|
+
import {MarkdownItPluginCb} from './typings';
|
|
4
|
+
import {generateID} from './utils';
|
|
5
|
+
|
|
6
|
+
const wrapInClipboard = (element: string | undefined, id: number) => {
|
|
7
|
+
return `
|
|
8
|
+
<div class="yfm-clipboard">
|
|
9
|
+
${element}
|
|
10
|
+
<svg width="16" height="16" viewBox="0 0 24 24" class="yfm-clipboard-button" data-animation="${id}">
|
|
11
|
+
<path
|
|
12
|
+
fill="currentColor"
|
|
13
|
+
d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"
|
|
14
|
+
/>
|
|
15
|
+
<path
|
|
16
|
+
stroke="currentColor"
|
|
17
|
+
fill="transparent"
|
|
18
|
+
stroke-width="1.5"
|
|
19
|
+
d="M9.5 13l3 3l5 -5"
|
|
20
|
+
visibility="hidden"
|
|
21
|
+
>
|
|
22
|
+
<animate
|
|
23
|
+
id="visibileAnimation-${id}"
|
|
24
|
+
attributeName="visibility"
|
|
25
|
+
from="hidden"
|
|
26
|
+
to="visible"
|
|
27
|
+
dur="0.2s"
|
|
28
|
+
fill="freeze"
|
|
29
|
+
begin=""
|
|
30
|
+
/>
|
|
31
|
+
<animate
|
|
32
|
+
id="hideAnimation-${id}"
|
|
33
|
+
attributeName="visibility"
|
|
34
|
+
from="visible"
|
|
35
|
+
to="hidden"
|
|
36
|
+
dur="1s"
|
|
37
|
+
begin="visibileAnimation-${id}.end+1"
|
|
38
|
+
fill="freeze"
|
|
39
|
+
/>
|
|
40
|
+
</path>
|
|
41
|
+
</svg>
|
|
42
|
+
</div>
|
|
43
|
+
`;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
interface EnvTerm {
|
|
47
|
+
terms: {
|
|
48
|
+
[keys: string]: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function termReplace(str: string, env: EnvTerm): string {
|
|
53
|
+
const regTerms = Object.keys(env.terms)
|
|
54
|
+
.map((el) => el.substr(1))
|
|
55
|
+
.join('|');
|
|
56
|
+
const regText = '\\[([^\\[]+)\\](\\(\\*(' + regTerms + ')\\))';
|
|
57
|
+
const reg = new RegExp(regText, 'g');
|
|
58
|
+
|
|
59
|
+
const termCode = str.replace(
|
|
60
|
+
reg,
|
|
61
|
+
(_match: string, p1: string, _p2: string, p3: string) =>
|
|
62
|
+
`<i class="yfm yfm-term_title" term-key=":${p3}" id="${generateID()}">${p1}</i>`,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return termCode || str;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const code: MarkdownItPluginCb = (md) => {
|
|
69
|
+
const superCodeRenderer = md.renderer.rules.fence;
|
|
70
|
+
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
|
71
|
+
const superCode = superCodeRenderer?.(tokens, idx, options, env, self);
|
|
72
|
+
const superCodeWithTerms =
|
|
73
|
+
superCode && env?.terms ? termReplace(superCode, env) : superCode;
|
|
74
|
+
|
|
75
|
+
return wrapInClipboard(superCodeWithTerms, idx);
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export = code;
|