@md-plugins/md-plugin-codeblocks 0.1.0-alpha.9 → 0.1.0-beta.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/README.md +9 -5
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +60 -25
- package/package.json +14 -16
package/README.md
CHANGED
|
@@ -16,12 +16,12 @@ A **Markdown-It** plugin that enhances code block rendering by providing syntax
|
|
|
16
16
|
Install the plugin via your preferred package manager:
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
|
-
#
|
|
20
|
-
npm install @md-plugins/md-plugin-codeblocks
|
|
21
|
-
# Or with Yarn:
|
|
22
|
-
yarn add @md-plugins/md-plugin-codeblocks
|
|
23
|
-
# Or with pnpm:
|
|
19
|
+
# with pnpm:
|
|
24
20
|
pnpm add @md-plugins/md-plugin-codeblocks
|
|
21
|
+
# with Yarn:
|
|
22
|
+
yarn add @md-plugins/md-plugin-codeblocks
|
|
23
|
+
# with npm:
|
|
24
|
+
npm install @md-plugins/md-plugin-codeblocks
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
## Usage
|
|
@@ -173,6 +173,10 @@ Run the unit tests with `Vitest`:
|
|
|
173
173
|
pnpm test
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
+
## Documentation
|
|
177
|
+
|
|
178
|
+
In case this README falls out of date, please refer to the [documentation](https://md-plugins.netlify.app/md-plugins/codeblocks/overview) for the latest information.
|
|
179
|
+
|
|
176
180
|
## License
|
|
177
181
|
|
|
178
182
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
|
package/dist/index.d.mts
CHANGED
|
@@ -62,4 +62,5 @@ declare module '@md-plugins/shared' {
|
|
|
62
62
|
|
|
63
63
|
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
64
64
|
|
|
65
|
-
export {
|
|
65
|
+
export { codeblocksPlugin };
|
|
66
|
+
export type { CodeblockPluginOptions, Lang };
|
package/dist/index.d.ts
CHANGED
|
@@ -62,4 +62,5 @@ declare module '@md-plugins/shared' {
|
|
|
62
62
|
|
|
63
63
|
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
64
64
|
|
|
65
|
-
export {
|
|
65
|
+
export { codeblocksPlugin };
|
|
66
|
+
export type { CodeblockPluginOptions, Lang };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import prism from 'prismjs';
|
|
2
2
|
import loadLanguages from 'prismjs/components/index.js';
|
|
3
3
|
|
|
4
|
+
function resolvePluginOptions(options, key, defaults) {
|
|
5
|
+
if (options && typeof options === "object" && key in options) {
|
|
6
|
+
return { ...defaults, ...options[key] };
|
|
7
|
+
}
|
|
8
|
+
return { ...defaults, ...options };
|
|
9
|
+
}
|
|
10
|
+
|
|
4
11
|
const defaultLangList = [
|
|
5
12
|
{ name: "markup" },
|
|
6
13
|
{ name: "bash", customCopy: true },
|
|
@@ -16,23 +23,39 @@ const defaultLangList = [
|
|
|
16
23
|
{ name: "diff" }
|
|
17
24
|
// special grammars
|
|
18
25
|
];
|
|
19
|
-
const
|
|
20
|
-
defaultLang
|
|
21
|
-
containerComponent
|
|
22
|
-
copyButtonComponent
|
|
23
|
-
preClass
|
|
24
|
-
codeClass
|
|
25
|
-
tabPanelTagName
|
|
26
|
-
tabPanelTagClass
|
|
27
|
-
pageScripts
|
|
26
|
+
const DEFAULT_CODEBLOCK_PLUGIN_OPTIONS = {
|
|
27
|
+
defaultLang: "markup",
|
|
28
|
+
containerComponent: "MarkdownPrerender",
|
|
29
|
+
copyButtonComponent: "MarkdownCopyButton",
|
|
30
|
+
preClass: "markdown-code",
|
|
31
|
+
codeClass: "",
|
|
32
|
+
tabPanelTagName: "q-tab-panel",
|
|
33
|
+
tabPanelTagClass: "q-pa-none",
|
|
34
|
+
pageScripts: [
|
|
28
35
|
"import MarkdownPrerender from 'src/.q-press/components/MarkdownPrerender'",
|
|
29
|
-
// ts file
|
|
30
36
|
"import MarkdownCopyButton from 'src/.q-press/components/MarkdownCopyButton.vue'"
|
|
31
37
|
],
|
|
32
|
-
langList
|
|
33
|
-
}
|
|
34
|
-
|
|
38
|
+
langList: defaultLangList
|
|
39
|
+
};
|
|
40
|
+
const codeblocksPlugin = (md, options) => {
|
|
41
|
+
const resolvedOptions = resolvePluginOptions(
|
|
42
|
+
options,
|
|
43
|
+
"codeblocksPlugin",
|
|
44
|
+
DEFAULT_CODEBLOCK_PLUGIN_OPTIONS
|
|
45
|
+
);
|
|
46
|
+
const {
|
|
47
|
+
defaultLang = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.defaultLang,
|
|
48
|
+
containerComponent = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.containerComponent,
|
|
49
|
+
copyButtonComponent = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.copyButtonComponent,
|
|
50
|
+
preClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.preClass,
|
|
51
|
+
codeClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.codeClass,
|
|
52
|
+
tabPanelTagName = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.tabPanelTagName,
|
|
53
|
+
tabPanelTagClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.tabPanelTagClass,
|
|
54
|
+
pageScripts = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.pageScripts,
|
|
55
|
+
langList = defaultLangList
|
|
56
|
+
} = resolvedOptions;
|
|
35
57
|
loadLanguages(langList.map((l) => l.name));
|
|
58
|
+
const customCopyLangList = langList.filter((l) => l.customCopy === true).map((l) => l.name);
|
|
36
59
|
const langMatch = langList.map((l) => l.aliases || l.name).join("|");
|
|
37
60
|
const definitionLineRE = new RegExp(
|
|
38
61
|
`^(?<lang>(tabs|${langMatch}))(\\s+\\[(?<attrs>.*)\\])?(\\s+(?<title>.+))?$`
|
|
@@ -57,7 +80,7 @@ const codeblocksPlugin = (md, {
|
|
|
57
80
|
},
|
|
58
81
|
content: []
|
|
59
82
|
};
|
|
60
|
-
} else if (currentTabName
|
|
83
|
+
} else if (currentTabName) {
|
|
61
84
|
tabMap[currentTabName].content.push(line);
|
|
62
85
|
}
|
|
63
86
|
}
|
|
@@ -102,8 +125,10 @@ const codeblocksPlugin = (md, {
|
|
|
102
125
|
const hasRemOrAdd = props.rem.length !== 0 || props.add.length !== 0;
|
|
103
126
|
for (const type of magicCommentList) {
|
|
104
127
|
const target = props[type];
|
|
128
|
+
if (target === void 0 || target.length === 0) continue;
|
|
105
129
|
for (const value of target) {
|
|
106
130
|
let [from, to] = value.split("-").map((i) => parseInt(i, 10));
|
|
131
|
+
if (from === void 0) continue;
|
|
107
132
|
if (to === void 0) to = from;
|
|
108
133
|
for (let i = from; i <= to; i++) {
|
|
109
134
|
acc[i - 1].classList.push(`line-${type}`);
|
|
@@ -116,17 +141,24 @@ const codeblocksPlugin = (md, {
|
|
|
116
141
|
acc[lineIndex].prefix.push(("" + (lineIndex + 1)).padStart(lineCount, " "));
|
|
117
142
|
});
|
|
118
143
|
}
|
|
119
|
-
hasRemOrAdd === true
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
target
|
|
123
|
-
|
|
124
|
-
|
|
144
|
+
if (hasRemOrAdd === true) {
|
|
145
|
+
lines.forEach((_, lineIndex) => {
|
|
146
|
+
const target = acc[lineIndex];
|
|
147
|
+
if (target === void 0) return;
|
|
148
|
+
target.prefix.push(
|
|
149
|
+
target.classList.includes(`line-add`) ? "+" : target.classList.includes(`line-rem`) ? "-" : " "
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
125
153
|
return acc;
|
|
126
154
|
}
|
|
127
155
|
function renderCodeBlock(html, codeClass2) {
|
|
128
156
|
return `<code${codeClass2 ? ` class="${codeClass2}"` : ""}>${html}</code>`;
|
|
129
157
|
}
|
|
158
|
+
function getPrismHighlightedContent(rawContent, lang) {
|
|
159
|
+
const content = rawContent.trim();
|
|
160
|
+
return prism.highlight(content, prism.languages[lang], lang);
|
|
161
|
+
}
|
|
130
162
|
function getHighlightedContent(rawContent, attrs) {
|
|
131
163
|
const { lang, maxheight } = attrs;
|
|
132
164
|
let content = rawContent.trim();
|
|
@@ -134,18 +166,21 @@ const codeblocksPlugin = (md, {
|
|
|
134
166
|
if (lang !== "markup") {
|
|
135
167
|
content = content.trim().replace(magicCommentGlobalRE, "");
|
|
136
168
|
}
|
|
137
|
-
const html =
|
|
169
|
+
const html = getPrismHighlightedContent(content, lang).split("\n").map((line, lineIndex) => {
|
|
138
170
|
const target = lineList[lineIndex];
|
|
171
|
+
if (target === void 0) return line;
|
|
139
172
|
let lineHtml = "";
|
|
140
173
|
lineHtml += target.classList.length !== 0 ? `<span class="c-line ${target.classList.join(" ")}"></span>` : "";
|
|
141
174
|
lineHtml += target.prefix.length !== 0 ? `<span class="c-lpref">${target.prefix.join(" ")}</span>` : "";
|
|
142
175
|
lineHtml += line;
|
|
143
176
|
return lineHtml;
|
|
144
177
|
}).join("\n");
|
|
145
|
-
const langClass = lang === "css" ? " language-css" : ` language-${lang}`;
|
|
146
178
|
const preAttrs = maxheight !== void 0 ? ` style="max-height:${maxheight}"` : "";
|
|
147
179
|
const langProp = customCopyLangList.includes(lang) === true ? ` lang="${lang}"` : "";
|
|
148
|
-
return
|
|
180
|
+
return (
|
|
181
|
+
// `<pre v-pre class="${preClass}${langClass}"${preAttrs}>` +
|
|
182
|
+
`<pre v-pre class="${preClass}"${preAttrs}>` + renderCodeBlock(html, codeClass) + `</pre><${copyButtonComponent}${langProp} />`
|
|
183
|
+
);
|
|
149
184
|
}
|
|
150
185
|
function parseAttrs(rawAttrs) {
|
|
151
186
|
if (rawAttrs === null) return {};
|
|
@@ -182,9 +217,9 @@ const codeblocksPlugin = (md, {
|
|
|
182
217
|
return "";
|
|
183
218
|
}
|
|
184
219
|
if (token.info === "") {
|
|
185
|
-
token.info = defaultLang;
|
|
220
|
+
token.info = defaultLang ?? "markup";
|
|
186
221
|
}
|
|
187
|
-
if (pageScripts.length > 0) {
|
|
222
|
+
if (pageScripts && pageScripts.length > 0) {
|
|
188
223
|
env.pageScripts = env.pageScripts || /* @__PURE__ */ new Set();
|
|
189
224
|
for (const script of pageScripts) {
|
|
190
225
|
env.pageScripts.add(script);
|
package/package.json
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@md-plugins/md-plugin-codeblocks",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
4
|
"description": "A markdown-it plugin for code blocks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown-it",
|
|
7
7
|
"quasarframework",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"types",
|
|
9
|
+
"vue"
|
|
10
10
|
],
|
|
11
11
|
"homepage": "https://github.com/md-plugins",
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/md-plugins/md-plugins/issues"
|
|
14
14
|
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "hawkeye64 <galbraith64@gmail.com>",
|
|
15
17
|
"repository": {
|
|
16
18
|
"type": "git",
|
|
17
19
|
"url": "git+https://github.com/md-plugins/md-plugins.git"
|
|
18
20
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
+
"files": [
|
|
22
|
+
"./dist"
|
|
23
|
+
],
|
|
21
24
|
"type": "module",
|
|
25
|
+
"module": "./dist/index.mjs",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
22
27
|
"exports": {
|
|
23
28
|
".": {
|
|
24
29
|
"import": {
|
|
@@ -27,22 +32,15 @@
|
|
|
27
32
|
}
|
|
28
33
|
}
|
|
29
34
|
},
|
|
30
|
-
"module": "./dist/index.mjs",
|
|
31
|
-
"types": "./dist/index.d.ts",
|
|
32
|
-
"files": [
|
|
33
|
-
"./dist"
|
|
34
|
-
],
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"markdown-it": "^14.1.0",
|
|
37
|
-
"prismjs": "^1.29.0",
|
|
38
|
-
"@md-plugins/shared": "0.1.0-alpha.9"
|
|
39
|
-
},
|
|
40
35
|
"publishConfig": {
|
|
41
36
|
"access": "public"
|
|
42
37
|
},
|
|
43
38
|
"devDependencies": {
|
|
44
39
|
"@types/markdown-it": "^14.1.2",
|
|
45
|
-
"@types/prismjs": "^1.26.
|
|
40
|
+
"@types/prismjs": "^1.26.6",
|
|
41
|
+
"markdown-it": "^14.1.1",
|
|
42
|
+
"prismjs": "^1.30.0",
|
|
43
|
+
"@md-plugins/shared": "0.1.0-beta.0"
|
|
46
44
|
},
|
|
47
45
|
"peerDependencies": {
|
|
48
46
|
"markdown-it": "^14.1.0",
|