@md-plugins/md-plugin-codeblocks 0.1.0-alpha.1 → 0.1.0-alpha.5
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/LICENSE.md +1 -1
- package/README.md +30 -32
- package/dist/index.d.mts +9 -21
- package/dist/index.d.ts +9 -21
- package/dist/index.mjs +81 -112
- package/package.json +7 -3
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2024-present,
|
|
3
|
+
Copyright (c) 2024-present, MD-PLUGINS
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -29,10 +29,10 @@ pnpm add @md-plugins/md-plugin-codeblocks
|
|
|
29
29
|
### Basic Setup
|
|
30
30
|
|
|
31
31
|
```js
|
|
32
|
-
import MarkdownIt from 'markdown-it'
|
|
33
|
-
import { codeblocksPlugin } from '@md-plugins/md-plugin-codeblocks'
|
|
32
|
+
import MarkdownIt from 'markdown-it'
|
|
33
|
+
import { codeblocksPlugin } from '@md-plugins/md-plugin-codeblocks'
|
|
34
34
|
|
|
35
|
-
const md = new MarkdownIt()
|
|
35
|
+
const md = new MarkdownIt()
|
|
36
36
|
md.use(codeblocksPlugin, {
|
|
37
37
|
containerComponent: 'MarkdownPrerender',
|
|
38
38
|
copyButtonComponent: '<MarkdownCopyButton',
|
|
@@ -41,13 +41,13 @@ md.use(codeblocksPlugin, {
|
|
|
41
41
|
"import MarkdownPrerender from 'src/components/md/MarkdownPrerender'",
|
|
42
42
|
"import MarkdownCopyButton from 'src/components/md/MarkdownCopyButton.vue'",
|
|
43
43
|
],
|
|
44
|
-
})
|
|
44
|
+
})
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
## Example Markdown Input
|
|
48
48
|
|
|
49
49
|
```javascript
|
|
50
|
-
console.log('Hello, world!')
|
|
50
|
+
console.log('Hello, world!')
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
### Example Output
|
|
@@ -81,7 +81,6 @@ The `md-plugin-codeblocks` plugin supports the following options:
|
|
|
81
81
|
| copyButtonComponent | string | 'markdown-copy-button' | Custom copy button component. |
|
|
82
82
|
| preClass | string | 'markdown-code' | CSS class for the `<pre>` element. |
|
|
83
83
|
| codeClass | string | '' | CSS class for the `<code>` element. |
|
|
84
|
-
| linePrefixClass | string | 'line-' | Prefix for classes used in line annotations. |
|
|
85
84
|
| tabPanelTagName | string | 'q-tab-panel' | Tag name for the tab panels. |
|
|
86
85
|
| tabPanelTagClass | string | 'q-pa-none' | CSS class for the tab panels. |
|
|
87
86
|
|
|
@@ -93,53 +92,52 @@ The plugin supports magic comments for inline annotations:
|
|
|
93
92
|
|
|
94
93
|
```js [numbered]
|
|
95
94
|
// All lines will be numbered
|
|
96
|
-
console.log('Line 1')
|
|
97
|
-
console.log('Line 2')
|
|
98
|
-
console.log('Line 3')
|
|
95
|
+
console.log('Line 1')
|
|
96
|
+
console.log('Line 2')
|
|
97
|
+
console.log('Line 3')
|
|
99
98
|
```
|
|
100
99
|
|
|
101
100
|
### Line Highlighting and Annotations
|
|
102
101
|
|
|
103
|
-
````
|
|
102
|
+
````markup
|
|
104
103
|
```js [highlight=2]
|
|
105
|
-
console.log('Line 1')
|
|
106
|
-
console.log('Line 2')
|
|
107
|
-
console.log('Line 3')
|
|
104
|
+
console.log('Line 1')
|
|
105
|
+
console.log('Line 2') // This line will be highlighted
|
|
106
|
+
console.log('Line 3')
|
|
108
107
|
```
|
|
109
108
|
````
|
|
110
109
|
|
|
111
|
-
````
|
|
110
|
+
````markup
|
|
112
111
|
```js
|
|
113
|
-
console.log('Line 1')
|
|
114
|
-
console.log('Line 2')
|
|
115
|
-
|
|
116
|
-
console.log('Line 3');
|
|
112
|
+
console.log('Line 1')
|
|
113
|
+
console.log('Line 2') [[highlight]] // This line will be highlighted
|
|
114
|
+
console.log('Line 3')
|
|
117
115
|
```
|
|
118
116
|
````
|
|
119
117
|
|
|
120
|
-
````
|
|
118
|
+
````markup
|
|
121
119
|
```js [add=2]
|
|
122
|
-
console.log('Line 1')
|
|
123
|
-
console.log('Line 2')
|
|
124
|
-
console.log('Line 3')
|
|
120
|
+
console.log('Line 1')
|
|
121
|
+
console.log('Line 2') // This line will be accented and prefixed with a '+'
|
|
122
|
+
console.log('Line 3')
|
|
125
123
|
```
|
|
126
124
|
````
|
|
127
125
|
|
|
128
|
-
````
|
|
126
|
+
````markup
|
|
129
127
|
```js [rem=2]
|
|
130
|
-
console.log('Line 1')
|
|
131
|
-
console.log('Line 2')
|
|
132
|
-
console.log('Line 3')
|
|
128
|
+
console.log('Line 1')
|
|
129
|
+
console.log('Line 2') // This line will be accented and prefixed with a '-'
|
|
130
|
+
console.log('Line 3')
|
|
133
131
|
```
|
|
134
132
|
````
|
|
135
133
|
|
|
136
134
|
#### Combining Annotations
|
|
137
135
|
|
|
138
|
-
````
|
|
136
|
+
````markup
|
|
139
137
|
```js [numbered highlight=1 rem=2 add=3]
|
|
140
|
-
console.log('Line 1')
|
|
141
|
-
console.log('Line 2')
|
|
142
|
-
console.log('Line 3')
|
|
138
|
+
console.log('Line 1') // This line will be highlighted
|
|
139
|
+
console.log('Line 2') // This line will be accented and prefixed with a '-'
|
|
140
|
+
console.log('Line 3') // This line will be accented and prefixed
|
|
143
141
|
```
|
|
144
142
|
````
|
|
145
143
|
|
|
@@ -147,7 +145,7 @@ console.log('Line 3'); // This line will be accented and prefixed
|
|
|
147
145
|
|
|
148
146
|
Additonally, with the exception of `numbered`, you can use ranges to annotate multiple lines:
|
|
149
147
|
|
|
150
|
-
```
|
|
148
|
+
```markup
|
|
151
149
|
[highlight=1,10-11 add=4,7-9 rem=12-14]
|
|
152
150
|
```
|
|
153
151
|
|
|
@@ -155,7 +153,7 @@ Additonally, with the exception of `numbered`, you can use ranges to annotate mu
|
|
|
155
153
|
|
|
156
154
|
Easily create tabbed interfaces for multiple code examples:
|
|
157
155
|
|
|
158
|
-
````
|
|
156
|
+
````markup
|
|
159
157
|
```tabs
|
|
160
158
|
<<|js Tab 1|>>
|
|
161
159
|
console.log('Hello from Tab 1');
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { PluginWithOptions } from 'markdown-it';
|
|
2
2
|
|
|
3
|
+
interface Lang {
|
|
4
|
+
name: string;
|
|
5
|
+
customCopy?: boolean;
|
|
6
|
+
aliases?: string;
|
|
7
|
+
}
|
|
3
8
|
interface CodeblockPluginOptions {
|
|
4
9
|
/**
|
|
5
10
|
* The default language to use if none is detected.
|
|
@@ -26,27 +31,14 @@ interface CodeblockPluginOptions {
|
|
|
26
31
|
* @default 'q-pa-none'
|
|
27
32
|
*/
|
|
28
33
|
tabPanelTagClass?: string;
|
|
29
|
-
/**
|
|
30
|
-
* The class to be used for the line added.
|
|
31
|
-
* @default 'line-add'
|
|
32
|
-
*/
|
|
33
|
-
lineAddClass?: string;
|
|
34
|
-
/**
|
|
35
|
-
* The prefix to be used for magic classes.
|
|
36
|
-
* The 3 magic classes are `add`, `rem`, and `highlight`.
|
|
37
|
-
* Classes in your code are then prefixed with this prefix.
|
|
38
|
-
* @default 'line-'
|
|
39
|
-
* The final class names would be: `line-add`, `line-rem`, `line-highlight`.
|
|
40
|
-
*/
|
|
41
|
-
linePrefixClass?: string;
|
|
42
34
|
/**
|
|
43
35
|
* The class to be used for the pre tag.
|
|
44
|
-
* @default 'markdown-
|
|
36
|
+
* @default 'markdown-code'
|
|
45
37
|
*/
|
|
46
38
|
preClass?: string;
|
|
47
39
|
/**
|
|
48
40
|
* The class to be used for the code tag (ok to be empty).
|
|
49
|
-
* @default '
|
|
41
|
+
* @default ''
|
|
50
42
|
*/
|
|
51
43
|
codeClass?: string;
|
|
52
44
|
/**
|
|
@@ -57,11 +49,7 @@ interface CodeblockPluginOptions {
|
|
|
57
49
|
* Optional Prism languages configuration array. This allows you to override or add custom language definitions.
|
|
58
50
|
* Each item can have a `name`, optional `aliases`, and `customCopy` boolean.
|
|
59
51
|
*/
|
|
60
|
-
langList?:
|
|
61
|
-
name: string;
|
|
62
|
-
aliases?: string;
|
|
63
|
-
customCopy?: boolean;
|
|
64
|
-
}>;
|
|
52
|
+
langList?: Lang[];
|
|
65
53
|
}
|
|
66
54
|
declare module '@md-plugins/shared' {
|
|
67
55
|
interface MarkdownItEnv {
|
|
@@ -74,4 +62,4 @@ declare module '@md-plugins/shared' {
|
|
|
74
62
|
|
|
75
63
|
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
76
64
|
|
|
77
|
-
export { type CodeblockPluginOptions, codeblocksPlugin };
|
|
65
|
+
export { type CodeblockPluginOptions, type Lang, codeblocksPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { PluginWithOptions } from 'markdown-it';
|
|
2
2
|
|
|
3
|
+
interface Lang {
|
|
4
|
+
name: string;
|
|
5
|
+
customCopy?: boolean;
|
|
6
|
+
aliases?: string;
|
|
7
|
+
}
|
|
3
8
|
interface CodeblockPluginOptions {
|
|
4
9
|
/**
|
|
5
10
|
* The default language to use if none is detected.
|
|
@@ -26,27 +31,14 @@ interface CodeblockPluginOptions {
|
|
|
26
31
|
* @default 'q-pa-none'
|
|
27
32
|
*/
|
|
28
33
|
tabPanelTagClass?: string;
|
|
29
|
-
/**
|
|
30
|
-
* The class to be used for the line added.
|
|
31
|
-
* @default 'line-add'
|
|
32
|
-
*/
|
|
33
|
-
lineAddClass?: string;
|
|
34
|
-
/**
|
|
35
|
-
* The prefix to be used for magic classes.
|
|
36
|
-
* The 3 magic classes are `add`, `rem`, and `highlight`.
|
|
37
|
-
* Classes in your code are then prefixed with this prefix.
|
|
38
|
-
* @default 'line-'
|
|
39
|
-
* The final class names would be: `line-add`, `line-rem`, `line-highlight`.
|
|
40
|
-
*/
|
|
41
|
-
linePrefixClass?: string;
|
|
42
34
|
/**
|
|
43
35
|
* The class to be used for the pre tag.
|
|
44
|
-
* @default 'markdown-
|
|
36
|
+
* @default 'markdown-code'
|
|
45
37
|
*/
|
|
46
38
|
preClass?: string;
|
|
47
39
|
/**
|
|
48
40
|
* The class to be used for the code tag (ok to be empty).
|
|
49
|
-
* @default '
|
|
41
|
+
* @default ''
|
|
50
42
|
*/
|
|
51
43
|
codeClass?: string;
|
|
52
44
|
/**
|
|
@@ -57,11 +49,7 @@ interface CodeblockPluginOptions {
|
|
|
57
49
|
* Optional Prism languages configuration array. This allows you to override or add custom language definitions.
|
|
58
50
|
* Each item can have a `name`, optional `aliases`, and `customCopy` boolean.
|
|
59
51
|
*/
|
|
60
|
-
langList?:
|
|
61
|
-
name: string;
|
|
62
|
-
aliases?: string;
|
|
63
|
-
customCopy?: boolean;
|
|
64
|
-
}>;
|
|
52
|
+
langList?: Lang[];
|
|
65
53
|
}
|
|
66
54
|
declare module '@md-plugins/shared' {
|
|
67
55
|
interface MarkdownItEnv {
|
|
@@ -74,4 +62,4 @@ declare module '@md-plugins/shared' {
|
|
|
74
62
|
|
|
75
63
|
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
76
64
|
|
|
77
|
-
export { type CodeblockPluginOptions, codeblocksPlugin };
|
|
65
|
+
export { type CodeblockPluginOptions, type Lang, codeblocksPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -13,8 +13,8 @@ const defaultLangList = [
|
|
|
13
13
|
{ name: "xml" },
|
|
14
14
|
{ name: "nginx" },
|
|
15
15
|
{ name: "html" },
|
|
16
|
-
// special grammars:
|
|
17
16
|
{ name: "diff" }
|
|
17
|
+
// special grammars
|
|
18
18
|
];
|
|
19
19
|
const codeblocksPlugin = (md, {
|
|
20
20
|
defaultLang = "markup",
|
|
@@ -22,7 +22,6 @@ const codeblocksPlugin = (md, {
|
|
|
22
22
|
copyButtonComponent = "MarkdownCopyButton",
|
|
23
23
|
preClass = "markdown-code",
|
|
24
24
|
codeClass = "",
|
|
25
|
-
linePrefixClass = "line-",
|
|
26
25
|
tabPanelTagName = "q-tab-panel",
|
|
27
26
|
tabPanelTagClass = "q-pa-none",
|
|
28
27
|
pageScripts = [
|
|
@@ -32,60 +31,34 @@ const codeblocksPlugin = (md, {
|
|
|
32
31
|
],
|
|
33
32
|
langList = defaultLangList
|
|
34
33
|
} = {}) => {
|
|
35
|
-
const customCopyLangList = langList.filter((l) => l.customCopy).map((l) => l.name);
|
|
34
|
+
const customCopyLangList = langList.filter((l) => l.customCopy === true).map((l) => l.name);
|
|
36
35
|
loadLanguages(langList.map((l) => l.name));
|
|
37
36
|
const langMatch = langList.map((l) => l.aliases || l.name).join("|");
|
|
38
37
|
const definitionLineRE = new RegExp(
|
|
39
|
-
`^(?<lang>(tabs|${langMatch}))(\\s+\\[(?<attrs
|
|
38
|
+
`^(?<lang>(tabs|${langMatch}))(\\s+\\[(?<attrs>.*)\\])?(\\s+(?<title>.+))?$`
|
|
40
39
|
);
|
|
41
40
|
const tabsLineRE = new RegExp(
|
|
42
|
-
`^<<\\|\\s
|
|
43
|
-
// ends with "|>>" + optional spaces
|
|
41
|
+
`^<<\\|\\s+(?<lang>${langMatch})(\\s+\\[(?<attrs>.*)\\])?(\\s+(?<title>.+))?\\s*\\|>>$`
|
|
44
42
|
);
|
|
45
|
-
const magicCommentList = ["highlight", "rem", "add"];
|
|
46
|
-
const magicCommentRE = new RegExp(
|
|
47
|
-
` *\\[\\[! (?<type>(${magicCommentList.join("|")}))\\]\\] *`
|
|
48
|
-
);
|
|
49
|
-
const magicCommentGlobalRE = new RegExp(magicCommentRE, "g");
|
|
50
|
-
function parseAttrs(rawAttrs) {
|
|
51
|
-
if (rawAttrs === null) return {};
|
|
52
|
-
const acc = {};
|
|
53
|
-
const attrList = rawAttrs.split(/\s+/);
|
|
54
|
-
for (const attr of attrList) {
|
|
55
|
-
const [key, value] = attr.split("=");
|
|
56
|
-
if (key !== void 0) {
|
|
57
|
-
const normalizedValue = value !== void 0 ? value.trim().replace(/^['"]|['"]$/g, "") : true;
|
|
58
|
-
acc[key.trim()] = normalizedValue;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return acc;
|
|
62
|
-
}
|
|
63
43
|
function extractTabs(content) {
|
|
64
44
|
const list = [];
|
|
65
45
|
const tabMap = {};
|
|
66
46
|
let currentTabName = null;
|
|
67
|
-
for (const line of content.split("\n")
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
...parseAttrs(attrs?.trim() || null),
|
|
81
|
-
lang
|
|
82
|
-
},
|
|
83
|
-
content: []
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
}
|
|
47
|
+
for (const line of content.split("\n")) {
|
|
48
|
+
const tabsMatch = line.match(tabsLineRE);
|
|
49
|
+
if (tabsMatch !== null) {
|
|
50
|
+
const { lang, attrs, title } = tabsMatch.groups || {};
|
|
51
|
+
currentTabName = title?.trim() || `Tab ${list.length + 1}`;
|
|
52
|
+
list.push(currentTabName);
|
|
53
|
+
tabMap[currentTabName] = {
|
|
54
|
+
attrs: {
|
|
55
|
+
...parseAttrs(attrs?.trim() || null),
|
|
56
|
+
lang
|
|
57
|
+
},
|
|
58
|
+
content: []
|
|
59
|
+
};
|
|
87
60
|
} else if (currentTabName !== null) {
|
|
88
|
-
tabMap[currentTabName]
|
|
61
|
+
tabMap[currentTabName].content.push(line);
|
|
89
62
|
}
|
|
90
63
|
}
|
|
91
64
|
if (list.length === 0) return;
|
|
@@ -93,13 +66,13 @@ const codeblocksPlugin = (md, {
|
|
|
93
66
|
param: `[ ${list.map((tab) => `'${tab}'`).join(", ")} ]`,
|
|
94
67
|
content: list.map((tabName) => {
|
|
95
68
|
const props = tabMap[tabName];
|
|
96
|
-
|
|
97
|
-
return `<${tabPanelTagName} class="${tabPanelTagClass}" name="${tabName}">` + getHighlightedContent(props.content.join("\n"), props.attrs) + `</${tabPanelTagName}>`;
|
|
98
|
-
}
|
|
99
|
-
return "";
|
|
69
|
+
return `<${tabPanelTagName} class="${tabPanelTagClass}" name="${tabName}">` + getHighlightedContent(props.content.join("\n"), props.attrs) + `</${tabPanelTagName}>`;
|
|
100
70
|
}).join("\n")
|
|
101
71
|
};
|
|
102
72
|
}
|
|
73
|
+
const magicCommentList = ["highlight", "rem", "add"];
|
|
74
|
+
const magicCommentRE = new RegExp(` *\\[\\[! (?<type>(${magicCommentList.join("|")}))\\]\\] *`);
|
|
75
|
+
const magicCommentGlobalRE = new RegExp(magicCommentRE, "g");
|
|
103
76
|
function extractCodeLineProps(lines, attrs) {
|
|
104
77
|
const acc = {};
|
|
105
78
|
for (const type of magicCommentList) {
|
|
@@ -107,9 +80,9 @@ const codeblocksPlugin = (md, {
|
|
|
107
80
|
}
|
|
108
81
|
lines.forEach((line, lineIndex) => {
|
|
109
82
|
const match = line.match(magicCommentRE);
|
|
110
|
-
if (match !== null
|
|
111
|
-
const
|
|
112
|
-
if (type
|
|
83
|
+
if (match !== null) {
|
|
84
|
+
const type = match.groups?.type;
|
|
85
|
+
if (type !== void 0) {
|
|
113
86
|
acc[type].push("" + (lineIndex + 1));
|
|
114
87
|
}
|
|
115
88
|
}
|
|
@@ -117,94 +90,89 @@ const codeblocksPlugin = (md, {
|
|
|
117
90
|
return acc;
|
|
118
91
|
}
|
|
119
92
|
function parseCodeLine(content, attrs) {
|
|
120
|
-
const
|
|
121
|
-
const
|
|
93
|
+
const lines = content.split("\n");
|
|
94
|
+
const acc = lines.map(() => ({
|
|
95
|
+
prefix: [],
|
|
96
|
+
classList: []
|
|
97
|
+
}));
|
|
98
|
+
if (attrs.lang === "markup") {
|
|
99
|
+
return acc;
|
|
100
|
+
}
|
|
122
101
|
const props = extractCodeLineProps(lines, attrs);
|
|
123
|
-
const
|
|
124
|
-
const hasRemOrAdd = (props.rem?.length || 0) > 0 || (props.add?.length || 0) > 0;
|
|
102
|
+
const hasRemOrAdd = props.rem.length !== 0 || props.add.length !== 0;
|
|
125
103
|
for (const type of magicCommentList) {
|
|
126
104
|
const target = props[type];
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (!isNaN(from) && !isNaN(to)) {
|
|
133
|
-
for (let i = from; i <= to; i++) {
|
|
134
|
-
if (acc[i - 1]) {
|
|
135
|
-
acc[i - 1].classList.push(`${linePrefixClass}${type}`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
105
|
+
for (const value of target) {
|
|
106
|
+
let [from, to] = value.split("-").map((i) => parseInt(i, 10));
|
|
107
|
+
if (to === void 0) to = from;
|
|
108
|
+
for (let i = from; i <= to; i++) {
|
|
109
|
+
acc[i - 1].classList.push(`line-${type}`);
|
|
139
110
|
}
|
|
140
111
|
}
|
|
141
112
|
}
|
|
142
113
|
if (attrs.numbered === true) {
|
|
143
|
-
const lineCount =
|
|
114
|
+
const lineCount = ("" + lines.length).length;
|
|
144
115
|
lines.forEach((_, lineIndex) => {
|
|
145
|
-
|
|
146
|
-
acc[lineIndex].prefix.push(
|
|
147
|
-
String(lineIndex + 1).padStart(lineCount, " ")
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
if (hasRemOrAdd) {
|
|
153
|
-
lines.forEach((_, lineIndex) => {
|
|
154
|
-
const target = acc[lineIndex];
|
|
155
|
-
if (target) {
|
|
156
|
-
target.prefix.push(
|
|
157
|
-
target.classList.includes(`${linePrefixClass}add`) ? "+" : target.classList.includes(`${linePrefixClass}rem`) ? "-" : " "
|
|
158
|
-
);
|
|
159
|
-
}
|
|
116
|
+
acc[lineIndex].prefix.push(("" + (lineIndex + 1)).padStart(lineCount, " "));
|
|
160
117
|
});
|
|
161
118
|
}
|
|
119
|
+
hasRemOrAdd === true && lines.forEach((_, lineIndex) => {
|
|
120
|
+
const target = acc[lineIndex];
|
|
121
|
+
target.prefix.push(
|
|
122
|
+
target.classList.includes(`line-add`) === true ? "+" : target.classList.includes(`line-rem`) === true ? "-" : " "
|
|
123
|
+
);
|
|
124
|
+
});
|
|
162
125
|
return acc;
|
|
163
126
|
}
|
|
127
|
+
function renderCodeBlock(html, codeClass2) {
|
|
128
|
+
return `<code${codeClass2 ? ` class="${codeClass2}"` : ""}>${html}</code>`;
|
|
129
|
+
}
|
|
164
130
|
function getHighlightedContent(rawContent, attrs) {
|
|
165
|
-
const { lang
|
|
166
|
-
|
|
131
|
+
const { lang, maxheight } = attrs;
|
|
132
|
+
let content = rawContent.trim();
|
|
167
133
|
const lineList = parseCodeLine(content, attrs);
|
|
168
|
-
|
|
169
|
-
|
|
134
|
+
if (lang !== "markup") {
|
|
135
|
+
content = content.trim().replace(magicCommentGlobalRE, "");
|
|
136
|
+
}
|
|
137
|
+
const html = prism.highlight(content, prism.languages[lang], lang).split("\n").map((line, lineIndex) => {
|
|
170
138
|
const target = lineList[lineIndex];
|
|
171
139
|
let lineHtml = "";
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
lineHtml += `<span class="c-line ${target.classList.join(" ")}"></span>`;
|
|
175
|
-
}
|
|
176
|
-
if (target.prefix.length !== 0) {
|
|
177
|
-
lineHtml += `<span class="c-lpref">${target.prefix.join(" ")}</span>`;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
140
|
+
lineHtml += target.classList.length !== 0 ? `<span class="c-line ${target.classList.join(" ")}"></span>` : "";
|
|
141
|
+
lineHtml += target.prefix.length !== 0 ? `<span class="c-lpref">${target.prefix.join(" ")}</span>` : "";
|
|
180
142
|
lineHtml += line;
|
|
181
143
|
return lineHtml;
|
|
182
144
|
}).join("\n");
|
|
183
145
|
const langClass = lang === "css" ? " language-css" : ` language-${lang}`;
|
|
184
146
|
const preAttrs = maxheight !== void 0 ? ` style="max-height:${maxheight}"` : "";
|
|
185
147
|
const langProp = customCopyLangList.includes(lang) === true ? ` lang="${lang}"` : "";
|
|
186
|
-
|
|
187
|
-
|
|
148
|
+
return `<pre v-pre class="${preClass}${langClass}"${preAttrs}>` + renderCodeBlock(html, codeClass) + `</pre><${copyButtonComponent}${langProp} />`;
|
|
149
|
+
}
|
|
150
|
+
function parseAttrs(rawAttrs) {
|
|
151
|
+
if (rawAttrs === null) return {};
|
|
152
|
+
const acc = {};
|
|
153
|
+
const attrList = rawAttrs.split(/\s+/);
|
|
154
|
+
for (const attr of attrList) {
|
|
155
|
+
const [key, value] = attr.split("=");
|
|
156
|
+
acc[key.trim()] = value?.trim() || true;
|
|
157
|
+
}
|
|
158
|
+
return acc;
|
|
188
159
|
}
|
|
189
160
|
function parseDefinitionLine(token) {
|
|
190
161
|
const match = token.info.trim().match(definitionLineRE);
|
|
191
162
|
if (match === null) {
|
|
192
163
|
return {
|
|
193
|
-
lang:
|
|
164
|
+
lang: "markup",
|
|
194
165
|
title: null
|
|
195
166
|
};
|
|
196
167
|
}
|
|
197
|
-
const {
|
|
168
|
+
const { lang, attrs, title } = match.groups || {};
|
|
198
169
|
const acc = {
|
|
199
|
-
...parseAttrs(
|
|
200
|
-
lang
|
|
201
|
-
title:
|
|
170
|
+
...parseAttrs(attrs?.trim() || null),
|
|
171
|
+
lang,
|
|
172
|
+
title: title?.trim() || null
|
|
202
173
|
};
|
|
203
174
|
if (acc.lang === "tabs") {
|
|
204
|
-
|
|
205
|
-
if (tabs) {
|
|
206
|
-
acc.tabs = tabs;
|
|
207
|
-
}
|
|
175
|
+
acc.tabs = extractTabs(token.content);
|
|
208
176
|
}
|
|
209
177
|
return acc;
|
|
210
178
|
}
|
|
@@ -213,16 +181,17 @@ const codeblocksPlugin = (md, {
|
|
|
213
181
|
if (!token) {
|
|
214
182
|
return "";
|
|
215
183
|
}
|
|
216
|
-
|
|
184
|
+
if (token.info === "") {
|
|
185
|
+
token.info = defaultLang;
|
|
186
|
+
}
|
|
217
187
|
if (pageScripts.length > 0) {
|
|
218
|
-
|
|
219
|
-
env.pageScripts = /* @__PURE__ */ new Set();
|
|
220
|
-
}
|
|
188
|
+
env.pageScripts = env.pageScripts || /* @__PURE__ */ new Set();
|
|
221
189
|
for (const script of pageScripts) {
|
|
222
190
|
env.pageScripts.add(script);
|
|
223
191
|
}
|
|
224
192
|
}
|
|
225
|
-
|
|
193
|
+
const attrs = parseDefinitionLine(token);
|
|
194
|
+
return `<${containerComponent}${attrs.title !== null ? ` title="${attrs.title}"` : ""}${attrs.tabs !== void 0 ? ` :tabs="${attrs.tabs.param}"` : ""}>` + (attrs.tabs !== void 0 ? attrs.tabs.content : getHighlightedContent(token.content, attrs)) + `</${containerComponent}>`;
|
|
226
195
|
};
|
|
227
196
|
};
|
|
228
197
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@md-plugins/md-plugin-codeblocks",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.5",
|
|
4
4
|
"description": "A markdown-it plugin for code blocks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown-it",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"markdown-it": "^14.1.0",
|
|
37
37
|
"prismjs": "^1.29.0",
|
|
38
|
-
"@md-plugins/shared": "0.1.0-alpha.
|
|
38
|
+
"@md-plugins/shared": "0.1.0-alpha.5"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
|
@@ -44,9 +44,13 @@
|
|
|
44
44
|
"@types/markdown-it": "^14.1.2",
|
|
45
45
|
"@types/prismjs": "^1.26.5"
|
|
46
46
|
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"markdown-it": "^14.1.0",
|
|
49
|
+
"prismjs": "^1.29.0"
|
|
50
|
+
},
|
|
47
51
|
"scripts": {
|
|
48
52
|
"build": "unbuild",
|
|
49
|
-
"clean": "rm -rf dist",
|
|
53
|
+
"clean": "rm -rf dist/ node_modules/",
|
|
50
54
|
"test": "vitest"
|
|
51
55
|
}
|
|
52
56
|
}
|