@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,218 @@
|
|
|
1
|
+
import url from 'url';
|
|
2
|
+
import {bold} from 'chalk';
|
|
3
|
+
import {isLocalUrl, findBlockTokens, headingInfo, getHrefTokenAttr} from '../../utils';
|
|
4
|
+
import {isFileExists, getFileTokens} from '../../utilsFS';
|
|
5
|
+
import {PAGE_LINK_REGEXP} from './constants';
|
|
6
|
+
import Token from 'markdown-it/lib/token';
|
|
7
|
+
import {Logger} from 'src/transform/log';
|
|
8
|
+
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
|
|
9
|
+
import path, {isAbsolute, parse, relative, resolve} from 'path';
|
|
10
|
+
import {StateCore} from 'src/transform/typings';
|
|
11
|
+
|
|
12
|
+
function defaultTransformLink(href: string) {
|
|
13
|
+
const parsed = url.parse(href);
|
|
14
|
+
|
|
15
|
+
return url.format({
|
|
16
|
+
...parsed,
|
|
17
|
+
pathname: parsed.pathname?.replace(PAGE_LINK_REGEXP, '.html'),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getTitleFromTokens(tokens: Token[]) {
|
|
22
|
+
let title = '';
|
|
23
|
+
|
|
24
|
+
let i = 0;
|
|
25
|
+
while (i < tokens.length) {
|
|
26
|
+
const token = tokens[i];
|
|
27
|
+
|
|
28
|
+
if (token.type === 'heading_open') {
|
|
29
|
+
({title = ''} = headingInfo(tokens, i));
|
|
30
|
+
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
i += 2;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return title;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Options = {
|
|
41
|
+
hash: string | null;
|
|
42
|
+
file: string;
|
|
43
|
+
state: StateCore;
|
|
44
|
+
opts: object;
|
|
45
|
+
isEmptyLink: boolean;
|
|
46
|
+
tokens: Token[];
|
|
47
|
+
idx: number;
|
|
48
|
+
nextToken: Token;
|
|
49
|
+
href: string;
|
|
50
|
+
currentPath: string;
|
|
51
|
+
log: Logger;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const addTitle = (options: Options) => {
|
|
55
|
+
const {hash, file, state, opts, isEmptyLink, tokens, idx, nextToken, href, currentPath, log} =
|
|
56
|
+
options;
|
|
57
|
+
|
|
58
|
+
const id = hash && hash.slice(1);
|
|
59
|
+
const fileTokens = getFileTokens(file, state, {
|
|
60
|
+
...opts,
|
|
61
|
+
disableLint: true,
|
|
62
|
+
disableTitleRefSubstitution: true,
|
|
63
|
+
disableCircularError: true,
|
|
64
|
+
inheritVars: false,
|
|
65
|
+
});
|
|
66
|
+
const sourceTokens = id ? findBlockTokens(fileTokens, id) : fileTokens;
|
|
67
|
+
const title = getTitleFromTokens(sourceTokens);
|
|
68
|
+
|
|
69
|
+
if (title) {
|
|
70
|
+
let textToken;
|
|
71
|
+
|
|
72
|
+
if (isEmptyLink) {
|
|
73
|
+
textToken = new state.Token('text', '', 0);
|
|
74
|
+
tokens.splice(idx + 1, 0, textToken);
|
|
75
|
+
} else {
|
|
76
|
+
textToken = nextToken;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
textToken.content = title;
|
|
80
|
+
} else {
|
|
81
|
+
log.warn(`Title not found: ${bold(href)} in ${bold(currentPath)}`);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
interface ProcOpts extends MarkdownItPluginOpts {
|
|
86
|
+
transformLink: (v: string) => string;
|
|
87
|
+
notFoundCb: (v: string) => void;
|
|
88
|
+
needSkipLinkFn: (v: string) => boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line complexity
|
|
92
|
+
function processLink(state: StateCore, tokens: Token[], idx: number, opts: ProcOpts) {
|
|
93
|
+
const {path: startPath, root, transformLink, notFoundCb, needSkipLinkFn, log} = opts;
|
|
94
|
+
const currentPath = state.env.path || startPath;
|
|
95
|
+
const linkToken = tokens[idx];
|
|
96
|
+
const nextToken = tokens[idx + 1];
|
|
97
|
+
|
|
98
|
+
let href = getHrefTokenAttr(linkToken);
|
|
99
|
+
|
|
100
|
+
if (!href) {
|
|
101
|
+
log.error(`Empty link in ${bold(startPath)}`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const {pathname, hash} = url.parse(href);
|
|
106
|
+
let file;
|
|
107
|
+
let fileExists;
|
|
108
|
+
let isPageFile;
|
|
109
|
+
|
|
110
|
+
if (!isLocalUrl(href)) {
|
|
111
|
+
linkToken.attrSet('target', '_blank');
|
|
112
|
+
linkToken.attrSet('rel', 'noreferrer noopener');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (pathname) {
|
|
117
|
+
file = resolve(path.parse(currentPath).dir, pathname);
|
|
118
|
+
fileExists = isFileExists(file);
|
|
119
|
+
isPageFile = PAGE_LINK_REGEXP.test(pathname);
|
|
120
|
+
|
|
121
|
+
if (isPageFile && !fileExists) {
|
|
122
|
+
let needShowError = true;
|
|
123
|
+
if (needSkipLinkFn) {
|
|
124
|
+
needShowError = !needSkipLinkFn(href);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (notFoundCb && needShowError) {
|
|
128
|
+
notFoundCb(file.replace(root, ''));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (needShowError) {
|
|
132
|
+
log.error(`Link is unreachable: ${bold(href)} in ${bold(currentPath)}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
} else if (hash) {
|
|
136
|
+
file = startPath;
|
|
137
|
+
fileExists = true;
|
|
138
|
+
isPageFile = true;
|
|
139
|
+
} else {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const isEmptyLink = nextToken.type === 'link_close';
|
|
144
|
+
const isTitleRefLink = nextToken.type === 'text' && nextToken.content === '{#T}';
|
|
145
|
+
if (
|
|
146
|
+
(isEmptyLink || isTitleRefLink) &&
|
|
147
|
+
fileExists &&
|
|
148
|
+
isPageFile &&
|
|
149
|
+
!state.env.disableTitleRefSubstitution
|
|
150
|
+
) {
|
|
151
|
+
addTitle({
|
|
152
|
+
hash,
|
|
153
|
+
file,
|
|
154
|
+
state,
|
|
155
|
+
opts,
|
|
156
|
+
isEmptyLink,
|
|
157
|
+
tokens,
|
|
158
|
+
idx,
|
|
159
|
+
nextToken,
|
|
160
|
+
href,
|
|
161
|
+
currentPath,
|
|
162
|
+
log,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let newPathname = '';
|
|
167
|
+
if (!isAbsolute(href) && currentPath !== startPath) {
|
|
168
|
+
newPathname = relative(parse(startPath).dir, file);
|
|
169
|
+
|
|
170
|
+
href = url.format({
|
|
171
|
+
...url.parse(href),
|
|
172
|
+
pathname: newPathname,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (pathname || newPathname) {
|
|
177
|
+
const transformer = transformLink || defaultTransformLink;
|
|
178
|
+
linkToken.attrSet('href', transformer(href));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const index: MarkdownItPluginCb<ProcOpts & Options> = (md, opts) => {
|
|
183
|
+
const plugin = (state: StateCore) => {
|
|
184
|
+
const tokens = state.tokens;
|
|
185
|
+
let i = 0;
|
|
186
|
+
|
|
187
|
+
while (i < tokens.length) {
|
|
188
|
+
if (tokens[i].type === 'inline') {
|
|
189
|
+
const childrenTokens = tokens[i].children || [];
|
|
190
|
+
let j = 0;
|
|
191
|
+
|
|
192
|
+
while (j < childrenTokens.length) {
|
|
193
|
+
const isLinkOpenToken = childrenTokens[j].type === 'link_open';
|
|
194
|
+
const tokenClass = childrenTokens[j].attrGet('class');
|
|
195
|
+
|
|
196
|
+
/* Don't process anchor links */
|
|
197
|
+
const isYfmAnchor = tokenClass ? tokenClass.includes('yfm-anchor') : false;
|
|
198
|
+
|
|
199
|
+
if (isLinkOpenToken && !isYfmAnchor) {
|
|
200
|
+
processLink(state, childrenTokens, j, opts);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
j++;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
i++;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
md.core.ruler.before('includes', 'links', plugin);
|
|
213
|
+
} catch (e) {
|
|
214
|
+
md.core.ruler.push('links', plugin);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export = index;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import StateInline, {Delimiter} from 'markdown-it/lib/rules_inline/state_inline';
|
|
2
|
+
import {MarkdownItPluginCb} from './typings';
|
|
3
|
+
|
|
4
|
+
const monospace: MarkdownItPluginCb = (md) => {
|
|
5
|
+
function postProcess(state: StateInline, delimiters: Delimiter[]) {
|
|
6
|
+
let token;
|
|
7
|
+
const loneMarkers = [];
|
|
8
|
+
|
|
9
|
+
for (let i = 0; i < delimiters.length; i++) {
|
|
10
|
+
const startDelim = delimiters[i];
|
|
11
|
+
|
|
12
|
+
if (startDelim.marker !== 0x23 /* # */) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (startDelim.end === -1) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const endDelim = delimiters[startDelim.end];
|
|
21
|
+
|
|
22
|
+
token = state.tokens[startDelim.token];
|
|
23
|
+
token.type = 'monospace_open';
|
|
24
|
+
token.tag = 'samp';
|
|
25
|
+
token.nesting = 1;
|
|
26
|
+
token.markup = '##';
|
|
27
|
+
token.content = '';
|
|
28
|
+
|
|
29
|
+
token = state.tokens[endDelim.token];
|
|
30
|
+
token.type = 'monospace_close';
|
|
31
|
+
token.tag = 'samp';
|
|
32
|
+
token.nesting = -1;
|
|
33
|
+
token.markup = '##';
|
|
34
|
+
token.content = '';
|
|
35
|
+
|
|
36
|
+
if (
|
|
37
|
+
state.tokens[endDelim.token - 1].type === 'text' &&
|
|
38
|
+
state.tokens[endDelim.token - 1].content === '#'
|
|
39
|
+
) {
|
|
40
|
+
loneMarkers.push(endDelim.token - 1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// If a marker sequence has an odd number of characters, it's splitted
|
|
45
|
+
// like this: `#####` -> `#` + `##` + `##`, leaving one marker at the
|
|
46
|
+
// start of the sequence.
|
|
47
|
+
//
|
|
48
|
+
// So, we have to move all those markers after subsequent s_close tags.
|
|
49
|
+
//
|
|
50
|
+
while (loneMarkers.length) {
|
|
51
|
+
const currentMarker = loneMarkers.pop();
|
|
52
|
+
if (typeof currentMarker === 'number') {
|
|
53
|
+
let nextMarker = currentMarker + 1;
|
|
54
|
+
|
|
55
|
+
while (
|
|
56
|
+
nextMarker < state.tokens.length &&
|
|
57
|
+
state.tokens[nextMarker].type === 'monospace_close'
|
|
58
|
+
) {
|
|
59
|
+
nextMarker++;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
nextMarker--;
|
|
63
|
+
|
|
64
|
+
if (currentMarker !== nextMarker) {
|
|
65
|
+
token = state.tokens[nextMarker];
|
|
66
|
+
state.tokens[nextMarker] = state.tokens[currentMarker];
|
|
67
|
+
state.tokens[currentMarker] = token;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
md.inline.ruler.before('emphasis', 'monospace', (state, silent) => {
|
|
74
|
+
let token;
|
|
75
|
+
const start = state.pos;
|
|
76
|
+
const marker = state.src.charCodeAt(start);
|
|
77
|
+
|
|
78
|
+
if (silent) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (marker !== 0x23 /* # */) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const scanned = state.scanDelims(state.pos, true);
|
|
87
|
+
let len = scanned.length;
|
|
88
|
+
const ch = String.fromCharCode(marker);
|
|
89
|
+
|
|
90
|
+
if (len < 2) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (len % 2) {
|
|
95
|
+
token = state.push('text', '', 0);
|
|
96
|
+
token.content = ch;
|
|
97
|
+
len--;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (let i = 0; i < len; i += 2) {
|
|
101
|
+
token = state.push('text', '', 0);
|
|
102
|
+
token.content = ch + ch;
|
|
103
|
+
|
|
104
|
+
if (!scanned.can_open && !scanned.can_close) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
state.delimiters.push({
|
|
109
|
+
marker: marker,
|
|
110
|
+
length: 0, // disable "rule of 3" length checks meant for emphasis
|
|
111
|
+
jump: i / 2, // 1 delimiter = 2 characters
|
|
112
|
+
token: state.tokens.length - 1,
|
|
113
|
+
end: -1,
|
|
114
|
+
open: scanned.can_open,
|
|
115
|
+
close: scanned.can_close,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
state.pos += scanned.length;
|
|
120
|
+
|
|
121
|
+
return true;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
md.inline.ruler2.before('emphasis', 'monospace', (state) => {
|
|
125
|
+
const tokensMeta = state.tokens_meta;
|
|
126
|
+
const max = (state.tokens_meta || []).length;
|
|
127
|
+
|
|
128
|
+
postProcess(state, state.delimiters);
|
|
129
|
+
|
|
130
|
+
for (let curr = 0; curr < max; curr++) {
|
|
131
|
+
const currentToken = tokensMeta[curr];
|
|
132
|
+
if (currentToken && currentToken.delimiters) {
|
|
133
|
+
postProcess(state, currentToken.delimiters);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return true;
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export = monospace;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {bold} from 'chalk';
|
|
2
|
+
import StateCore from 'markdown-it/lib/rules_core/state_core';
|
|
3
|
+
import Token from 'markdown-it/lib/token';
|
|
4
|
+
import {MarkdownItPluginCb} from './typings';
|
|
5
|
+
|
|
6
|
+
import {MatchTokenFunction, nestedCloseTokenIdxFactory as closeTokenFactory} from './utils';
|
|
7
|
+
|
|
8
|
+
const ALERT_RE = /^{% note (alert|info|tip|warning)\s*(?:"(.*?)")? %}$/;
|
|
9
|
+
const WRONG_NOTES = /^{% note (.*)%}/;
|
|
10
|
+
|
|
11
|
+
const titles: Record<string, Record<string, string>> = {
|
|
12
|
+
ru: {
|
|
13
|
+
info: 'Примечание',
|
|
14
|
+
tip: 'Совет',
|
|
15
|
+
alert: 'Внимание',
|
|
16
|
+
warning: 'Важно',
|
|
17
|
+
},
|
|
18
|
+
en: {
|
|
19
|
+
info: 'Note',
|
|
20
|
+
tip: 'Tip',
|
|
21
|
+
alert: 'Alert',
|
|
22
|
+
warning: 'Warning',
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function getTitle(type: string, originLang: keyof typeof titles) {
|
|
27
|
+
let lang = originLang;
|
|
28
|
+
|
|
29
|
+
if (!lang || !Object.keys(titles).includes(lang)) {
|
|
30
|
+
lang = 'ru';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return titles[lang][type];
|
|
34
|
+
}
|
|
35
|
+
const matchCloseToken: MatchTokenFunction = (tokens, i) => {
|
|
36
|
+
return (
|
|
37
|
+
tokens[i].type === 'paragraph_open' &&
|
|
38
|
+
tokens[i + 1].type === 'inline' &&
|
|
39
|
+
tokens[i + 1].content.trim() === '{% endnote %}'
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function matchOpenToken(tokens: Token[], i: number) {
|
|
44
|
+
return (
|
|
45
|
+
tokens[i].type === 'paragraph_open' &&
|
|
46
|
+
tokens[i + 1].type === 'inline' &&
|
|
47
|
+
tokens[i + 1].content.match(ALERT_RE)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function matchWrongNotes(tokens: Token[], i: number) {
|
|
52
|
+
return (
|
|
53
|
+
tokens[i].type === 'paragraph_open' &&
|
|
54
|
+
tokens[i + 1].type === 'inline' &&
|
|
55
|
+
tokens[i + 1].content.match(WRONG_NOTES)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const findCloseTokenIdx = closeTokenFactory('Note', matchOpenToken, matchCloseToken);
|
|
60
|
+
|
|
61
|
+
const notes: MarkdownItPluginCb = (md, {lang, path: optPath, log}) => {
|
|
62
|
+
const plugin = (state: StateCore) => {
|
|
63
|
+
const {tokens, env} = state;
|
|
64
|
+
const path = env.path || optPath;
|
|
65
|
+
let i = 0;
|
|
66
|
+
|
|
67
|
+
while (i < tokens.length) {
|
|
68
|
+
const match = matchOpenToken(tokens, i);
|
|
69
|
+
|
|
70
|
+
if (match) {
|
|
71
|
+
const closeTokenIdx = findCloseTokenIdx(tokens, i + 4, path, log);
|
|
72
|
+
|
|
73
|
+
if (!closeTokenIdx) {
|
|
74
|
+
i += 3;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const type = match[1].toLowerCase();
|
|
79
|
+
const newOpenToken = new state.Token('yfm_note_open', 'div', 1);
|
|
80
|
+
newOpenToken.attrSet('class', `yfm-note yfm-accent-${type}`);
|
|
81
|
+
newOpenToken.attrSet('note-type', type);
|
|
82
|
+
newOpenToken.map = tokens[i].map;
|
|
83
|
+
|
|
84
|
+
const newCloseToken = new state.Token('yfm_note_close', 'div', -1);
|
|
85
|
+
newCloseToken.map = tokens[closeTokenIdx].map;
|
|
86
|
+
|
|
87
|
+
// Add extra paragraph
|
|
88
|
+
const titleOpen = new state.Token('yfm_note_title_open', 'p', 1);
|
|
89
|
+
titleOpen.attrSet('class', 'yfm-note-title');
|
|
90
|
+
const titleInline = new state.Token('inline', '', 0);
|
|
91
|
+
const titleClose = new state.Token('yfm_note_title_close', 'p', -1);
|
|
92
|
+
|
|
93
|
+
if (match[2]) titleOpen.attrSet('yfm2xliff-explicit', 'true');
|
|
94
|
+
titleOpen.block = true;
|
|
95
|
+
titleClose.block = true;
|
|
96
|
+
|
|
97
|
+
titleInline.content = match[2] === undefined ? getTitle(type, lang) : match[2];
|
|
98
|
+
titleInline.children = [];
|
|
99
|
+
state.md.inline.parse(
|
|
100
|
+
titleInline.content,
|
|
101
|
+
state.md,
|
|
102
|
+
state.env,
|
|
103
|
+
titleInline.children,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const insideTokens = [
|
|
107
|
+
newOpenToken,
|
|
108
|
+
titleOpen,
|
|
109
|
+
titleInline,
|
|
110
|
+
titleClose,
|
|
111
|
+
...tokens.slice(i + 3, closeTokenIdx),
|
|
112
|
+
newCloseToken,
|
|
113
|
+
];
|
|
114
|
+
tokens.splice(i, closeTokenIdx - i + 3, ...insideTokens);
|
|
115
|
+
|
|
116
|
+
i++;
|
|
117
|
+
} else if (matchWrongNotes(tokens, i) && tokens[i + 1].content !== '{% endnote %}') {
|
|
118
|
+
log.warn(`Incorrect syntax for notes${path ? `, file ${bold(path)}` : ''}`);
|
|
119
|
+
|
|
120
|
+
i += 3;
|
|
121
|
+
} else {
|
|
122
|
+
i++;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
md.core.ruler.before('curly_attributes', 'notes', plugin);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
md.core.ruler.push('notes', plugin);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export = notes;
|