@md-plugins/md-plugin-codeblocks 0.1.0-alpha.1 → 0.1.0-alpha.11
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 +38 -34
- package/dist/index.d.mts +9 -21
- package/dist/index.d.ts +9 -21
- package/dist/index.mjs +86 -115
- 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,25 +29,25 @@ 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',
|
|
39
39
|
preClass: 'markdown-code',
|
|
40
40
|
pageScripts: [
|
|
41
|
-
"import MarkdownPrerender from 'src/components/
|
|
42
|
-
"import MarkdownCopyButton from 'src/components/
|
|
41
|
+
"import MarkdownPrerender from 'src/.q-press/components/MarkdownPrerender'",
|
|
42
|
+
"import MarkdownCopyButton from 'src/.q-press/components/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
|
|
|
@@ -91,55 +90,56 @@ The `md-plugin-codeblocks` plugin supports the following options:
|
|
|
91
90
|
|
|
92
91
|
The plugin supports magic comments for inline annotations:
|
|
93
92
|
|
|
93
|
+
````markup
|
|
94
94
|
```js [numbered]
|
|
95
95
|
// All lines will be numbered
|
|
96
|
-
console.log('Line 1')
|
|
97
|
-
console.log('Line 2')
|
|
98
|
-
console.log('Line 3')
|
|
96
|
+
console.log('Line 1')
|
|
97
|
+
console.log('Line 2')
|
|
98
|
+
console.log('Line 3')
|
|
99
99
|
```
|
|
100
|
+
````
|
|
100
101
|
|
|
101
102
|
### Line Highlighting and Annotations
|
|
102
103
|
|
|
103
|
-
````
|
|
104
|
+
````markup
|
|
104
105
|
```js [highlight=2]
|
|
105
|
-
console.log('Line 1')
|
|
106
|
-
console.log('Line 2')
|
|
107
|
-
console.log('Line 3')
|
|
106
|
+
console.log('Line 1')
|
|
107
|
+
console.log('Line 2') // This line will be highlighted
|
|
108
|
+
console.log('Line 3')
|
|
108
109
|
```
|
|
109
110
|
````
|
|
110
111
|
|
|
111
|
-
````
|
|
112
|
+
````markup
|
|
112
113
|
```js
|
|
113
|
-
console.log('Line 1')
|
|
114
|
-
console.log('Line 2')
|
|
115
|
-
|
|
116
|
-
console.log('Line 3');
|
|
114
|
+
console.log('Line 1')
|
|
115
|
+
console.log('Line 2') [[highlight]] // This line will be highlighted
|
|
116
|
+
console.log('Line 3')
|
|
117
117
|
```
|
|
118
118
|
````
|
|
119
119
|
|
|
120
|
-
````
|
|
120
|
+
````markup
|
|
121
121
|
```js [add=2]
|
|
122
|
-
console.log('Line 1')
|
|
123
|
-
console.log('Line 2')
|
|
124
|
-
console.log('Line 3')
|
|
122
|
+
console.log('Line 1')
|
|
123
|
+
console.log('Line 2') // This line will be accented and prefixed with a '+'
|
|
124
|
+
console.log('Line 3')
|
|
125
125
|
```
|
|
126
126
|
````
|
|
127
127
|
|
|
128
|
-
````
|
|
128
|
+
````markup
|
|
129
129
|
```js [rem=2]
|
|
130
|
-
console.log('Line 1')
|
|
131
|
-
console.log('Line 2')
|
|
132
|
-
console.log('Line 3')
|
|
130
|
+
console.log('Line 1')
|
|
131
|
+
console.log('Line 2') // This line will be accented and prefixed with a '-'
|
|
132
|
+
console.log('Line 3')
|
|
133
133
|
```
|
|
134
134
|
````
|
|
135
135
|
|
|
136
136
|
#### Combining Annotations
|
|
137
137
|
|
|
138
|
-
````
|
|
138
|
+
````markup
|
|
139
139
|
```js [numbered highlight=1 rem=2 add=3]
|
|
140
|
-
console.log('Line 1')
|
|
141
|
-
console.log('Line 2')
|
|
142
|
-
console.log('Line 3')
|
|
140
|
+
console.log('Line 1') // This line will be highlighted
|
|
141
|
+
console.log('Line 2') // This line will be accented and prefixed with a '-'
|
|
142
|
+
console.log('Line 3') // This line will be accented and prefixed
|
|
143
143
|
```
|
|
144
144
|
````
|
|
145
145
|
|
|
@@ -147,7 +147,7 @@ console.log('Line 3'); // This line will be accented and prefixed
|
|
|
147
147
|
|
|
148
148
|
Additonally, with the exception of `numbered`, you can use ranges to annotate multiple lines:
|
|
149
149
|
|
|
150
|
-
```
|
|
150
|
+
```markup
|
|
151
151
|
[highlight=1,10-11 add=4,7-9 rem=12-14]
|
|
152
152
|
```
|
|
153
153
|
|
|
@@ -155,7 +155,7 @@ Additonally, with the exception of `numbered`, you can use ranges to annotate mu
|
|
|
155
155
|
|
|
156
156
|
Easily create tabbed interfaces for multiple code examples:
|
|
157
157
|
|
|
158
|
-
````
|
|
158
|
+
````markup
|
|
159
159
|
```tabs
|
|
160
160
|
<<|js Tab 1|>>
|
|
161
161
|
console.log('Hello from Tab 1');
|
|
@@ -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
|
@@ -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,70 +22,43 @@ 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 = [
|
|
29
|
-
"import MarkdownPrerender from 'src/components/
|
|
28
|
+
"import MarkdownPrerender from 'src/.q-press/components/MarkdownPrerender'",
|
|
30
29
|
// ts file
|
|
31
|
-
"import MarkdownCopyButton from 'src/components/
|
|
30
|
+
"import MarkdownCopyButton from 'src/.q-press/components/MarkdownCopyButton.vue'"
|
|
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,91 @@ 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
|
-
const langClass = lang === "css" ? " language-css" : ` language-${lang}`;
|
|
184
145
|
const preAttrs = maxheight !== void 0 ? ` style="max-height:${maxheight}"` : "";
|
|
185
146
|
const langProp = customCopyLangList.includes(lang) === true ? ` lang="${lang}"` : "";
|
|
186
|
-
|
|
187
|
-
|
|
147
|
+
return (
|
|
148
|
+
// `<pre v-pre class="${preClass}${langClass}"${preAttrs}>` +
|
|
149
|
+
`<pre v-pre class="${preClass}"${preAttrs}>` + renderCodeBlock(html, codeClass) + `</pre><${copyButtonComponent}${langProp} />`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
function parseAttrs(rawAttrs) {
|
|
153
|
+
if (rawAttrs === null) return {};
|
|
154
|
+
const acc = {};
|
|
155
|
+
const attrList = rawAttrs.split(/\s+/);
|
|
156
|
+
for (const attr of attrList) {
|
|
157
|
+
const [key, value] = attr.split("=");
|
|
158
|
+
acc[key.trim()] = value?.trim() || true;
|
|
159
|
+
}
|
|
160
|
+
return acc;
|
|
188
161
|
}
|
|
189
162
|
function parseDefinitionLine(token) {
|
|
190
163
|
const match = token.info.trim().match(definitionLineRE);
|
|
191
164
|
if (match === null) {
|
|
192
165
|
return {
|
|
193
|
-
lang:
|
|
166
|
+
lang: "markup",
|
|
194
167
|
title: null
|
|
195
168
|
};
|
|
196
169
|
}
|
|
197
|
-
const {
|
|
170
|
+
const { lang, attrs, title } = match.groups || {};
|
|
198
171
|
const acc = {
|
|
199
|
-
...parseAttrs(
|
|
200
|
-
lang
|
|
201
|
-
title:
|
|
172
|
+
...parseAttrs(attrs?.trim() || null),
|
|
173
|
+
lang,
|
|
174
|
+
title: title?.trim() || null
|
|
202
175
|
};
|
|
203
176
|
if (acc.lang === "tabs") {
|
|
204
|
-
|
|
205
|
-
if (tabs) {
|
|
206
|
-
acc.tabs = tabs;
|
|
207
|
-
}
|
|
177
|
+
acc.tabs = extractTabs(token.content);
|
|
208
178
|
}
|
|
209
179
|
return acc;
|
|
210
180
|
}
|
|
@@ -213,16 +183,17 @@ const codeblocksPlugin = (md, {
|
|
|
213
183
|
if (!token) {
|
|
214
184
|
return "";
|
|
215
185
|
}
|
|
216
|
-
|
|
186
|
+
if (token.info === "") {
|
|
187
|
+
token.info = defaultLang;
|
|
188
|
+
}
|
|
217
189
|
if (pageScripts.length > 0) {
|
|
218
|
-
|
|
219
|
-
env.pageScripts = /* @__PURE__ */ new Set();
|
|
220
|
-
}
|
|
190
|
+
env.pageScripts = env.pageScripts || /* @__PURE__ */ new Set();
|
|
221
191
|
for (const script of pageScripts) {
|
|
222
192
|
env.pageScripts.add(script);
|
|
223
193
|
}
|
|
224
194
|
}
|
|
225
|
-
|
|
195
|
+
const attrs = parseDefinitionLine(token);
|
|
196
|
+
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
197
|
};
|
|
227
198
|
};
|
|
228
199
|
|
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.11",
|
|
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.11"
|
|
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
|
}
|