zenn-markdown-html 0.1.100-alpha.1 → 0.1.101
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/lib/index.js +0 -2
- package/lib/utils/highlight.d.ts +1 -0
- package/lib/utils/highlight.js +38 -0
- package/lib/utils/md-renderer-fence.d.ts +5 -0
- package/lib/utils/md-renderer-fence.js +69 -31
- package/package.json +2 -2
- package/lib/utils/md-prism.d.ts +0 -21
- package/lib/utils/md-prism.js +0 -149
package/lib/index.js
CHANGED
|
@@ -9,7 +9,6 @@ var crypto_1 = __importDefault(require("crypto"));
|
|
|
9
9
|
var md_container_1 = require("./utils/md-container");
|
|
10
10
|
var md_renderer_fence_1 = require("./utils/md-renderer-fence");
|
|
11
11
|
var md_linkify_to_card_1 = require("./utils/md-linkify-to-card");
|
|
12
|
-
var md_prism_1 = require("./utils/md-prism");
|
|
13
12
|
var md_katex_1 = require("./utils/md-katex");
|
|
14
13
|
var md_br_1 = require("./utils/md-br");
|
|
15
14
|
var md_custom_block_1 = require("./utils/md-custom-block");
|
|
@@ -26,7 +25,6 @@ var md = markdown_it_1.default({
|
|
|
26
25
|
});
|
|
27
26
|
md.linkify.set({ fuzzyLink: false });
|
|
28
27
|
md.use(md_br_1.mdBr)
|
|
29
|
-
.use(md_prism_1.mdPrism)
|
|
30
28
|
.use(md_renderer_fence_1.mdRendererFence)
|
|
31
29
|
.use(markdown_it_imsize_1.default)
|
|
32
30
|
.use(md_custom_block_1.mdCustomBlock)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function highlight(text: string, langName: string, hasDiff: boolean): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.highlight = void 0;
|
|
7
|
+
var prismjs_1 = __importDefault(require("prismjs"));
|
|
8
|
+
var utils_1 = require("markdown-it/lib/common/utils");
|
|
9
|
+
var components_1 = __importDefault(require("prismjs/components/"));
|
|
10
|
+
var prism_diff_highlight_1 = __importDefault(require("@steelydylan/prism-diff-highlight"));
|
|
11
|
+
// diffプラグインを有効化
|
|
12
|
+
prism_diff_highlight_1.default();
|
|
13
|
+
function loadPrismGrammer(lang) {
|
|
14
|
+
if (!lang)
|
|
15
|
+
return undefined;
|
|
16
|
+
var langObject = prismjs_1.default.languages[lang];
|
|
17
|
+
if (langObject === undefined) {
|
|
18
|
+
components_1.default([lang]);
|
|
19
|
+
langObject = prismjs_1.default.languages[lang];
|
|
20
|
+
}
|
|
21
|
+
return langObject;
|
|
22
|
+
}
|
|
23
|
+
function highlightContent(_a) {
|
|
24
|
+
var text = _a.text, prismGrammer = _a.prismGrammer, langName = _a.langName, hasDiff = _a.hasDiff;
|
|
25
|
+
if (prismGrammer && langName) {
|
|
26
|
+
if (hasDiff)
|
|
27
|
+
return prismjs_1.default.highlight(text, prismjs_1.default.languages.diff, "diff-" + langName);
|
|
28
|
+
return prismjs_1.default.highlight(text, prismGrammer, langName);
|
|
29
|
+
}
|
|
30
|
+
if (hasDiff)
|
|
31
|
+
return prismjs_1.default.highlight(text, prismjs_1.default.languages.diff, 'diff');
|
|
32
|
+
return utils_1.escapeHtml(text);
|
|
33
|
+
}
|
|
34
|
+
function highlight(text, langName, hasDiff) {
|
|
35
|
+
var prismGrammer = loadPrismGrammer(langName);
|
|
36
|
+
return highlightContent({ text: text, prismGrammer: prismGrammer, langName: langName, hasDiff: hasDiff });
|
|
37
|
+
}
|
|
38
|
+
exports.highlight = highlight;
|
|
@@ -1,22 +1,64 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mdRendererFence = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
exports.mdRendererFence = exports.parseInfo = void 0;
|
|
4
|
+
var utils_1 = require("markdown-it/lib/common/utils");
|
|
5
|
+
var highlight_1 = require("./highlight");
|
|
6
|
+
function getHtml(_a) {
|
|
7
|
+
var content = _a.content, className = _a.className, fileName = _a.fileName;
|
|
8
|
+
var escapedClass = utils_1.escapeHtml(className);
|
|
9
|
+
return "<div class=\"code-block-container\">" + (fileName
|
|
10
|
+
? "<div class=\"code-block-filename-container\"><span class=\"code-block-filename\">" + utils_1.escapeHtml(fileName) + "</span></div>"
|
|
11
|
+
: '') + "<pre class=\"" + escapedClass + "\"><code class=\"" + escapedClass + "\">" + content + "</code></pre></div>";
|
|
12
|
+
}
|
|
13
|
+
function getClassName(_a) {
|
|
14
|
+
var _b = _a.langName, langName = _b === void 0 ? '' : _b, hasDiff = _a.hasDiff;
|
|
15
|
+
var isSafe = /^[\w-]{0,30}$/.test(langName);
|
|
16
|
+
if (!isSafe)
|
|
17
|
+
return '';
|
|
18
|
+
if (hasDiff) {
|
|
19
|
+
return "diff-highlight " + (langName.length ? "language-diff-" + langName : '');
|
|
20
|
+
}
|
|
21
|
+
return langName ? "language-" + langName : '';
|
|
22
|
+
}
|
|
23
|
+
var fallbackLanguages = {
|
|
24
|
+
vue: 'html',
|
|
25
|
+
react: 'jsx',
|
|
26
|
+
fish: 'shell',
|
|
27
|
+
sh: 'shell',
|
|
28
|
+
cwl: 'yaml',
|
|
29
|
+
tf: 'hcl',
|
|
30
|
+
};
|
|
8
31
|
function normalizeLangName(str) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
32
|
+
var _a;
|
|
33
|
+
if (!(str === null || str === void 0 ? void 0 : str.length))
|
|
34
|
+
return '';
|
|
35
|
+
var langName = str.toLocaleLowerCase();
|
|
36
|
+
return (_a = fallbackLanguages[langName]) !== null && _a !== void 0 ? _a : langName;
|
|
13
37
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
38
|
+
function parseInfo(str) {
|
|
39
|
+
if (str.trim() === '') {
|
|
40
|
+
return {
|
|
41
|
+
langName: '',
|
|
42
|
+
fileName: undefined,
|
|
43
|
+
hasDiff: false,
|
|
19
44
|
};
|
|
45
|
+
}
|
|
46
|
+
// e.g. foo:filename => ["foo", "filename"]
|
|
47
|
+
// e.g. foo diff:filename => ["foo diff", "filename"]
|
|
48
|
+
var _a = str.split(':'), langInfo = _a[0], fileName = _a[1];
|
|
49
|
+
var langNames = langInfo.split(' ');
|
|
50
|
+
var hasDiff = langNames.some(function (name) { return name === 'diff'; });
|
|
51
|
+
var langName = hasDiff
|
|
52
|
+
? langNames.find(function (lang) { return lang !== 'diff'; })
|
|
53
|
+
: langNames[0];
|
|
54
|
+
return {
|
|
55
|
+
langName: normalizeLangName(langName),
|
|
56
|
+
fileName: fileName,
|
|
57
|
+
hasDiff: hasDiff,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
exports.parseInfo = parseInfo;
|
|
61
|
+
function mdRendererFence(md) {
|
|
20
62
|
// override fence
|
|
21
63
|
md.renderer.rules.fence = function () {
|
|
22
64
|
var args = [];
|
|
@@ -24,25 +66,21 @@ function mdRendererFence(md) {
|
|
|
24
66
|
args[_i] = arguments[_i];
|
|
25
67
|
}
|
|
26
68
|
var tokens = args[0], idx = args[1];
|
|
27
|
-
|
|
28
|
-
var
|
|
29
|
-
var langName = (langInfo === null || langInfo === void 0 ? void 0 : langInfo.length) ? normalizeLangName(langInfo[0]) : '';
|
|
69
|
+
var _a = tokens[idx], info = _a.info, content = _a.content;
|
|
70
|
+
var _b = parseInfo(info), langName = _b.langName, fileName = _b.fileName, hasDiff = _b.hasDiff;
|
|
30
71
|
if (langName === 'mermaid') {
|
|
31
|
-
return "<div class=\"embed-mermaid\"><embed-mermaid><pre class=\"zenn-mermaid\">" +
|
|
72
|
+
return "<div class=\"embed-mermaid\"><embed-mermaid><pre class=\"zenn-mermaid\">" + utils_1.escapeHtml(content.trim()) + "</pre></embed-mermaid></div>";
|
|
32
73
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return "\n <div class=\"code-block-container\">\n " + (labelText
|
|
44
|
-
? "<div class=\"code-block-filename-container\"><span class=\"code-block-filename\">" + md.utils.escapeHtml(labelText) + "</span></div>"
|
|
45
|
-
: '') + "\n " + originalHTML + "\n </div>\n ";
|
|
74
|
+
var className = getClassName({
|
|
75
|
+
langName: langName,
|
|
76
|
+
hasDiff: hasDiff,
|
|
77
|
+
});
|
|
78
|
+
var highlightedContent = highlight_1.highlight(content, langName, hasDiff);
|
|
79
|
+
return getHtml({
|
|
80
|
+
content: highlightedContent,
|
|
81
|
+
className: className,
|
|
82
|
+
fileName: fileName,
|
|
83
|
+
});
|
|
46
84
|
};
|
|
47
85
|
}
|
|
48
86
|
exports.mdRendererFence = mdRendererFence;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zenn-markdown-html",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.101",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Convert markdown to zenn flavor html.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"markdown-it-task-lists": "^2.1.1",
|
|
46
46
|
"prismjs": "^1.25.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "9713490577e00d1dc2dd710d7a0eb32b12b15436",
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|
|
51
51
|
}
|
package/lib/utils/md-prism.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import Prism from 'prismjs';
|
|
2
|
-
import MarkdownIt from 'markdown-it';
|
|
3
|
-
interface Options {
|
|
4
|
-
plugins: string[];
|
|
5
|
-
/**
|
|
6
|
-
* Callback for Prism initialisation. Useful for initialising plugins.
|
|
7
|
-
* @param prism The Prism instance that will be used by the plugin.
|
|
8
|
-
*/
|
|
9
|
-
init: (prism: typeof Prism) => void;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Initialisation function of the plugin. This function is not called directly by clients, but is rather provided
|
|
13
|
-
* to MarkdownIt’s {@link MarkdownIt.use} function.
|
|
14
|
-
*
|
|
15
|
-
* @param markdownit
|
|
16
|
-
* The markdown it instance the plugin is being registered to.
|
|
17
|
-
* @param useroptions
|
|
18
|
-
* The options this plugin is being initialised with.
|
|
19
|
-
*/
|
|
20
|
-
export declare function mdPrism(markdownit: MarkdownIt, useroptions: Options): void;
|
|
21
|
-
export {};
|
package/lib/utils/md-prism.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
-
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
-
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
-
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
-
r[k] = a[j];
|
|
7
|
-
return r;
|
|
8
|
-
};
|
|
9
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.mdPrism = void 0;
|
|
14
|
-
var prismjs_1 = __importDefault(require("prismjs"));
|
|
15
|
-
var components_1 = __importDefault(require("prismjs/components/"));
|
|
16
|
-
var prism_diff_highlight_1 = __importDefault(require("@steelydylan/prism-diff-highlight"));
|
|
17
|
-
// diffプラグインを有効化
|
|
18
|
-
prism_diff_highlight_1.default();
|
|
19
|
-
var DEFAULTS = {
|
|
20
|
-
plugins: [],
|
|
21
|
-
init: function () {
|
|
22
|
-
// do nothing by default
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Loads the provided {@code lang} into prism.
|
|
27
|
-
*
|
|
28
|
-
* @param lang
|
|
29
|
-
* Code of the language to load.
|
|
30
|
-
* @param isDiff
|
|
31
|
-
* whether to use diff with language or not
|
|
32
|
-
* @return The Prism language object for the provided {@code lang} code. {@code undefined} if the language is not known to Prism.
|
|
33
|
-
*/
|
|
34
|
-
function loadPrismLang(lang) {
|
|
35
|
-
if (!lang)
|
|
36
|
-
return undefined;
|
|
37
|
-
var langObject = prismjs_1.default.languages[lang];
|
|
38
|
-
if (langObject === undefined) {
|
|
39
|
-
components_1.default([lang]);
|
|
40
|
-
langObject = prismjs_1.default.languages[lang];
|
|
41
|
-
}
|
|
42
|
-
return langObject;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Check given lang should be treated as `diff` and parse name of language.
|
|
46
|
-
*
|
|
47
|
-
* @param lang
|
|
48
|
-
* String given by `markdown-it`. this must be lower case.
|
|
49
|
-
* @return whether `lang` includes `diff` or not, and parsed name if recognized as `diff`.
|
|
50
|
-
* If not, return `lang` directly.
|
|
51
|
-
*
|
|
52
|
-
*/
|
|
53
|
-
function checkIncludingDiff(lang) {
|
|
54
|
-
var _a;
|
|
55
|
-
// TODO: Determine the method to find `diff`(`match`, `indexOf` e.t.c.).
|
|
56
|
-
var langs = lang.split('-');
|
|
57
|
-
var hasDiff = langs.some(function (lang) { return lang === 'diff'; });
|
|
58
|
-
if (hasDiff) {
|
|
59
|
-
var newLang = (_a = langs.find(function (lang) { return lang !== 'diff'; })) !== null && _a !== void 0 ? _a : '';
|
|
60
|
-
return {
|
|
61
|
-
isDiff: true,
|
|
62
|
-
lang: newLang,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
isDiff: false,
|
|
67
|
-
lang: lang,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
var fallbackLanguages = {
|
|
71
|
-
vue: 'html',
|
|
72
|
-
react: 'jsx',
|
|
73
|
-
fish: 'shell',
|
|
74
|
-
sh: 'shell',
|
|
75
|
-
cwl: 'yaml',
|
|
76
|
-
tf: 'hcl',
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Select the language to use for highlighting, based on the provided options and the specified language.
|
|
80
|
-
*
|
|
81
|
-
* @param lang
|
|
82
|
-
* Code of the language to highlight the text in.
|
|
83
|
-
* @return The name of the language to use and the Prism language object for that language.
|
|
84
|
-
*/
|
|
85
|
-
function selectLanguage(lang) {
|
|
86
|
-
var loweredLang = (lang === null || lang === void 0 ? void 0 : lang.toLowerCase()) || '';
|
|
87
|
-
var _a = checkIncludingDiff(loweredLang), isDiff = _a.isDiff, langNormalized = _a.lang;
|
|
88
|
-
var langAlias = fallbackLanguages[langNormalized];
|
|
89
|
-
var langToUse = langAlias || langNormalized;
|
|
90
|
-
var prismLang = loadPrismLang(langToUse);
|
|
91
|
-
return {
|
|
92
|
-
langToUse: langToUse,
|
|
93
|
-
grammer: prismLang,
|
|
94
|
-
isDiff: isDiff,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Highlights the provided text using Prism.
|
|
99
|
-
*
|
|
100
|
-
* @param markdownit
|
|
101
|
-
* The markdown-it instance
|
|
102
|
-
* @param text
|
|
103
|
-
* The text to highlight.
|
|
104
|
-
* @param lang
|
|
105
|
-
* Code of the language to highlight the text in.
|
|
106
|
-
* @return {@code text} wrapped in {@code <pre>} and {@code <code>}, both equipped with the appropriate class
|
|
107
|
-
* (markdown-it’s langPrefix + lang). If Prism knows {@code lang}, {@code text} will be highlighted by it.
|
|
108
|
-
*/
|
|
109
|
-
function highlight(markdownit, text, lang) {
|
|
110
|
-
var _a = selectLanguage(lang), langToUse = _a.langToUse, isDiff = _a.isDiff, grammer = _a.grammer;
|
|
111
|
-
// 1. Use `diff` highlight with `language` if set.
|
|
112
|
-
// 2. Use `language` (or `diff`, which is included) only.
|
|
113
|
-
// 3. Use plain Markdown.
|
|
114
|
-
var code = grammer
|
|
115
|
-
? isDiff
|
|
116
|
-
? prismjs_1.default.highlight(text, prismjs_1.default.languages.diff, 'diff-' + langToUse)
|
|
117
|
-
: prismjs_1.default.highlight(text, grammer, langToUse)
|
|
118
|
-
: isDiff
|
|
119
|
-
? prismjs_1.default.highlight(text, prismjs_1.default.languages.diff, 'diff')
|
|
120
|
-
: markdownit.utils.escapeHtml(text);
|
|
121
|
-
var classAttribute = langToUse
|
|
122
|
-
? isDiff
|
|
123
|
-
? " class=\"diff-highlight " + markdownit.options.langPrefix + "diff-" + markdownit.utils.escapeHtml(langToUse) + "\""
|
|
124
|
-
: " class=\"" + markdownit.options.langPrefix + markdownit.utils.escapeHtml(langToUse) + "\""
|
|
125
|
-
: '';
|
|
126
|
-
return "<pre" + classAttribute + "><code" + classAttribute + ">" + code + "</code></pre>";
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Initialisation function of the plugin. This function is not called directly by clients, but is rather provided
|
|
130
|
-
* to MarkdownIt’s {@link MarkdownIt.use} function.
|
|
131
|
-
*
|
|
132
|
-
* @param markdownit
|
|
133
|
-
* The markdown it instance the plugin is being registered to.
|
|
134
|
-
* @param useroptions
|
|
135
|
-
* The options this plugin is being initialised with.
|
|
136
|
-
*/
|
|
137
|
-
function mdPrism(markdownit, useroptions) {
|
|
138
|
-
var options = Object.assign({}, DEFAULTS, useroptions);
|
|
139
|
-
options.init(prismjs_1.default);
|
|
140
|
-
// register ourselves as highlighter
|
|
141
|
-
markdownit.options.highlight = function () {
|
|
142
|
-
var args = [];
|
|
143
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
144
|
-
args[_i] = arguments[_i];
|
|
145
|
-
}
|
|
146
|
-
return highlight.apply(void 0, __spreadArrays([markdownit], args));
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
exports.mdPrism = mdPrism;
|