@md-plugins/md-plugin-codeblocks 0.1.0-alpha.1
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 +21 -0
- package/README.md +178 -0
- package/dist/index.d.mts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.mjs +229 -0
- package/package.json +52 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present, Jeff Galbraith
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# md-plugin-codeblocks
|
|
2
|
+
|
|
3
|
+
A **Markdown-It** plugin that enhances code block rendering by providing syntax highlighting, line numbering, and support for advanced features like tabbed code blocks. It integrates with Prism.js for syntax highlighting and allows customization for various use cases.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Syntax Highlighting**: Automatically highlights code blocks using **Prism.js**.
|
|
8
|
+
- **Line Numbering**: Optionally adds line numbers to code blocks.
|
|
9
|
+
- **Magic Comments**: Supports special comments like `[[! highlight]]`, `[[! add]]`, and `[[! rem]]` for inline code annotations.
|
|
10
|
+
- **Tabbed Code Blocks**: Enables the creation of tabbed code blocks for multi-language or multi-file examples.
|
|
11
|
+
- **Customizable Components**: Supports custom wrapper and copy button components.
|
|
12
|
+
- **Fallback for Unsupported Languages**: Gracefully handles code blocks written in unsupported languages.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install the plugin via your preferred package manager:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# With npm:
|
|
20
|
+
npm install @md-plugins/md-plugin-codeblocks
|
|
21
|
+
# Or with Yarn:
|
|
22
|
+
yarn add @md-plugins/md-plugin-codeblocks
|
|
23
|
+
# Or with pnpm:
|
|
24
|
+
pnpm add @md-plugins/md-plugin-codeblocks
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Basic Setup
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import MarkdownIt from 'markdown-it';
|
|
33
|
+
import { codeblocksPlugin } from '@md-plugins/md-plugin-codeblocks';
|
|
34
|
+
|
|
35
|
+
const md = new MarkdownIt();
|
|
36
|
+
md.use(codeblocksPlugin, {
|
|
37
|
+
containerComponent: 'MarkdownPrerender',
|
|
38
|
+
copyButtonComponent: '<MarkdownCopyButton',
|
|
39
|
+
preClass: 'markdown-code',
|
|
40
|
+
pageScripts: [
|
|
41
|
+
"import MarkdownPrerender from 'src/components/md/MarkdownPrerender'",
|
|
42
|
+
"import MarkdownCopyButton from 'src/components/md/MarkdownCopyButton.vue'",
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Example Markdown Input
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
console.log('Hello, world!');
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Example Output
|
|
54
|
+
|
|
55
|
+
The rendered output will include syntax-highlighted code wrapped in customizable components:
|
|
56
|
+
|
|
57
|
+
```html
|
|
58
|
+
<markdown-prerender>
|
|
59
|
+
<pre v-pre class="markdown-code language-javascript">
|
|
60
|
+
<code>
|
|
61
|
+
console<span class="token punctuation">.</span>
|
|
62
|
+
<span class="token function">log</span>
|
|
63
|
+
<span class="token punctuation">(</span>
|
|
64
|
+
<span class="token string">'Hello, world!'</span>
|
|
65
|
+
<span class="token punctuation">)</span>
|
|
66
|
+
<span class="token punctuation">;</span>
|
|
67
|
+
</code>
|
|
68
|
+
</pre>
|
|
69
|
+
<markdown-copy-button></markdown-copy-button>
|
|
70
|
+
</markdown-prerender>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Options
|
|
74
|
+
|
|
75
|
+
The `md-plugin-codeblocks` plugin supports the following options:
|
|
76
|
+
|
|
77
|
+
| Option | Type | Default | Description |
|
|
78
|
+
| ------------------- | ------ | ---------------------- | -------------------------------------------------------------- |
|
|
79
|
+
| defaultLang | string | 'markup' | Default language for code blocks without a specified language. |
|
|
80
|
+
| containerComponent | string | 'markdown-prerender' | Custom wrapper component for code blocks. |
|
|
81
|
+
| copyButtonComponent | string | 'markdown-copy-button' | Custom copy button component. |
|
|
82
|
+
| preClass | string | 'markdown-code' | CSS class for the `<pre>` element. |
|
|
83
|
+
| codeClass | string | '' | CSS class for the `<code>` element. |
|
|
84
|
+
| linePrefixClass | string | 'line-' | Prefix for classes used in line annotations. |
|
|
85
|
+
| tabPanelTagName | string | 'q-tab-panel' | Tag name for the tab panels. |
|
|
86
|
+
| tabPanelTagClass | string | 'q-pa-none' | CSS class for the tab panels. |
|
|
87
|
+
|
|
88
|
+
## Advanced Features
|
|
89
|
+
|
|
90
|
+
### Line Numbering
|
|
91
|
+
|
|
92
|
+
The plugin supports magic comments for inline annotations:
|
|
93
|
+
|
|
94
|
+
```js [numbered]
|
|
95
|
+
// All lines will be numbered
|
|
96
|
+
console.log('Line 1');
|
|
97
|
+
console.log('Line 2');
|
|
98
|
+
console.log('Line 3');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Line Highlighting and Annotations
|
|
102
|
+
|
|
103
|
+
````markdown
|
|
104
|
+
```js [highlight=2]
|
|
105
|
+
console.log('Line 1');
|
|
106
|
+
console.log('Line 2'); // This line will be highlighted
|
|
107
|
+
console.log('Line 3');
|
|
108
|
+
```
|
|
109
|
+
````
|
|
110
|
+
|
|
111
|
+
````markdown
|
|
112
|
+
```js
|
|
113
|
+
console.log('Line 1');
|
|
114
|
+
console.log('Line 2');
|
|
115
|
+
[[highlight]]; // This line will be highlighted
|
|
116
|
+
console.log('Line 3');
|
|
117
|
+
```
|
|
118
|
+
````
|
|
119
|
+
|
|
120
|
+
````markdown
|
|
121
|
+
```js [add=2]
|
|
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
|
+
```
|
|
126
|
+
````
|
|
127
|
+
|
|
128
|
+
````markdown
|
|
129
|
+
```js [rem=2]
|
|
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
|
+
```
|
|
134
|
+
````
|
|
135
|
+
|
|
136
|
+
#### Combining Annotations
|
|
137
|
+
|
|
138
|
+
````markdown
|
|
139
|
+
```js [numbered highlight=1 rem=2 add=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
|
+
```
|
|
144
|
+
````
|
|
145
|
+
|
|
146
|
+
### Using Ranges
|
|
147
|
+
|
|
148
|
+
Additonally, with the exception of `numbered`, you can use ranges to annotate multiple lines:
|
|
149
|
+
|
|
150
|
+
```markdown
|
|
151
|
+
[highlight=1,10-11 add=4,7-9 rem=12-14]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Tabbed Code Blocks
|
|
155
|
+
|
|
156
|
+
Easily create tabbed interfaces for multiple code examples:
|
|
157
|
+
|
|
158
|
+
````markdown
|
|
159
|
+
```tabs
|
|
160
|
+
<<|js Tab 1|>>
|
|
161
|
+
console.log('Hello from Tab 1');
|
|
162
|
+
|
|
163
|
+
<<|ts Tab 2|>>
|
|
164
|
+
console.log('Hello from Tab 2');
|
|
165
|
+
```
|
|
166
|
+
````
|
|
167
|
+
|
|
168
|
+
## Testing
|
|
169
|
+
|
|
170
|
+
Run the unit tests with `Vitest`:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pnpm test
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
|
|
3
|
+
interface CodeblockPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The default language to use if none is detected.
|
|
6
|
+
* @default 'markup'
|
|
7
|
+
*/
|
|
8
|
+
defaultLang?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The component used to wrap code blocks.
|
|
11
|
+
* @default 'markdown-prerender'
|
|
12
|
+
*/
|
|
13
|
+
containerComponent?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The component used to render the copy button.
|
|
16
|
+
* @default 'markdown-copy-button'
|
|
17
|
+
*/
|
|
18
|
+
copyButtonComponent?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The comonent name for the tab panel.
|
|
21
|
+
* @default 'q-tab-panel'
|
|
22
|
+
*/
|
|
23
|
+
tabPanelTagName?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The class(es) to be used with the tab panel.
|
|
26
|
+
* @default 'q-pa-none'
|
|
27
|
+
*/
|
|
28
|
+
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
|
+
/**
|
|
43
|
+
* The class to be used for the pre tag.
|
|
44
|
+
* @default 'markdown-pre'
|
|
45
|
+
*/
|
|
46
|
+
preClass?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The class to be used for the code tag (ok to be empty).
|
|
49
|
+
* @default 'markdown-code'
|
|
50
|
+
*/
|
|
51
|
+
codeClass?: string;
|
|
52
|
+
/**
|
|
53
|
+
* An array of page scripts to be included.
|
|
54
|
+
*/
|
|
55
|
+
pageScripts?: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Optional Prism languages configuration array. This allows you to override or add custom language definitions.
|
|
58
|
+
* Each item can have a `name`, optional `aliases`, and `customCopy` boolean.
|
|
59
|
+
*/
|
|
60
|
+
langList?: Array<{
|
|
61
|
+
name: string;
|
|
62
|
+
aliases?: string;
|
|
63
|
+
customCopy?: boolean;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
declare module '@md-plugins/shared' {
|
|
67
|
+
interface MarkdownItEnv {
|
|
68
|
+
/**
|
|
69
|
+
* An array of page script (import statements) to be included.
|
|
70
|
+
*/
|
|
71
|
+
pageScripts?: Set<string>;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
76
|
+
|
|
77
|
+
export { type CodeblockPluginOptions, codeblocksPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
|
|
3
|
+
interface CodeblockPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The default language to use if none is detected.
|
|
6
|
+
* @default 'markup'
|
|
7
|
+
*/
|
|
8
|
+
defaultLang?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The component used to wrap code blocks.
|
|
11
|
+
* @default 'markdown-prerender'
|
|
12
|
+
*/
|
|
13
|
+
containerComponent?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The component used to render the copy button.
|
|
16
|
+
* @default 'markdown-copy-button'
|
|
17
|
+
*/
|
|
18
|
+
copyButtonComponent?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The comonent name for the tab panel.
|
|
21
|
+
* @default 'q-tab-panel'
|
|
22
|
+
*/
|
|
23
|
+
tabPanelTagName?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The class(es) to be used with the tab panel.
|
|
26
|
+
* @default 'q-pa-none'
|
|
27
|
+
*/
|
|
28
|
+
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
|
+
/**
|
|
43
|
+
* The class to be used for the pre tag.
|
|
44
|
+
* @default 'markdown-pre'
|
|
45
|
+
*/
|
|
46
|
+
preClass?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The class to be used for the code tag (ok to be empty).
|
|
49
|
+
* @default 'markdown-code'
|
|
50
|
+
*/
|
|
51
|
+
codeClass?: string;
|
|
52
|
+
/**
|
|
53
|
+
* An array of page scripts to be included.
|
|
54
|
+
*/
|
|
55
|
+
pageScripts?: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Optional Prism languages configuration array. This allows you to override or add custom language definitions.
|
|
58
|
+
* Each item can have a `name`, optional `aliases`, and `customCopy` boolean.
|
|
59
|
+
*/
|
|
60
|
+
langList?: Array<{
|
|
61
|
+
name: string;
|
|
62
|
+
aliases?: string;
|
|
63
|
+
customCopy?: boolean;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
declare module '@md-plugins/shared' {
|
|
67
|
+
interface MarkdownItEnv {
|
|
68
|
+
/**
|
|
69
|
+
* An array of page script (import statements) to be included.
|
|
70
|
+
*/
|
|
71
|
+
pageScripts?: Set<string>;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
declare const codeblocksPlugin: PluginWithOptions<CodeblockPluginOptions>;
|
|
76
|
+
|
|
77
|
+
export { type CodeblockPluginOptions, codeblocksPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import prism from 'prismjs';
|
|
2
|
+
import loadLanguages from 'prismjs/components/index.js';
|
|
3
|
+
|
|
4
|
+
const defaultLangList = [
|
|
5
|
+
{ name: "markup" },
|
|
6
|
+
{ name: "bash", customCopy: true },
|
|
7
|
+
{ name: "javascript", aliases: "javascript|js" },
|
|
8
|
+
{ name: "typescript", aliases: "typescript|ts" },
|
|
9
|
+
{ name: "sass" },
|
|
10
|
+
{ name: "scss" },
|
|
11
|
+
{ name: "css" },
|
|
12
|
+
{ name: "json" },
|
|
13
|
+
{ name: "xml" },
|
|
14
|
+
{ name: "nginx" },
|
|
15
|
+
{ name: "html" },
|
|
16
|
+
// special grammars:
|
|
17
|
+
{ name: "diff" }
|
|
18
|
+
];
|
|
19
|
+
const codeblocksPlugin = (md, {
|
|
20
|
+
defaultLang = "markup",
|
|
21
|
+
containerComponent = "MarkdownPrerender",
|
|
22
|
+
copyButtonComponent = "MarkdownCopyButton",
|
|
23
|
+
preClass = "markdown-code",
|
|
24
|
+
codeClass = "",
|
|
25
|
+
linePrefixClass = "line-",
|
|
26
|
+
tabPanelTagName = "q-tab-panel",
|
|
27
|
+
tabPanelTagClass = "q-pa-none",
|
|
28
|
+
pageScripts = [
|
|
29
|
+
"import MarkdownPrerender from 'src/components/md/MarkdownPrerender'",
|
|
30
|
+
// ts file
|
|
31
|
+
"import MarkdownCopyButton from 'src/components/md/MarkdownCopyButton.vue'"
|
|
32
|
+
],
|
|
33
|
+
langList = defaultLangList
|
|
34
|
+
} = {}) => {
|
|
35
|
+
const customCopyLangList = langList.filter((l) => l.customCopy).map((l) => l.name);
|
|
36
|
+
loadLanguages(langList.map((l) => l.name));
|
|
37
|
+
const langMatch = langList.map((l) => l.aliases || l.name).join("|");
|
|
38
|
+
const definitionLineRE = new RegExp(
|
|
39
|
+
`^(?<lang>(tabs|${langMatch}))(\\s+\\[(?<attrs>[^\\]]+)\\])?(\\s+(?<title>.+))?$`
|
|
40
|
+
);
|
|
41
|
+
const tabsLineRE = new RegExp(
|
|
42
|
+
`^<<\\|\\s*(?<lang>${langMatch})(?:\\s+\\[(?<attrs>[^\\]]+)\\])?(?:\\s+(?<title>.+?))?\\s*\\|>>$`
|
|
43
|
+
// ends with "|>>" + optional spaces
|
|
44
|
+
);
|
|
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
|
+
function extractTabs(content) {
|
|
64
|
+
const list = [];
|
|
65
|
+
const tabMap = {};
|
|
66
|
+
let currentTabName = null;
|
|
67
|
+
for (const line of content.split("\n").map((line2) => line2.trim())) {
|
|
68
|
+
if (line.startsWith("<<|")) {
|
|
69
|
+
const tabsMatch = line.match(tabsLineRE);
|
|
70
|
+
if (tabsMatch !== null) {
|
|
71
|
+
const { groups } = tabsMatch;
|
|
72
|
+
const lang = groups?.lang;
|
|
73
|
+
const attrs = groups?.attrs;
|
|
74
|
+
const title = groups?.title;
|
|
75
|
+
currentTabName = title?.trim() || `Tab ${list.length + 1}`;
|
|
76
|
+
if (currentTabName) {
|
|
77
|
+
list.push(currentTabName);
|
|
78
|
+
tabMap[currentTabName] = {
|
|
79
|
+
attrs: {
|
|
80
|
+
...parseAttrs(attrs?.trim() || null),
|
|
81
|
+
lang
|
|
82
|
+
},
|
|
83
|
+
content: []
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
} else if (currentTabName !== null) {
|
|
88
|
+
tabMap[currentTabName]?.content.push(line);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (list.length === 0) return;
|
|
92
|
+
return {
|
|
93
|
+
param: `[ ${list.map((tab) => `'${tab}'`).join(", ")} ]`,
|
|
94
|
+
content: list.map((tabName) => {
|
|
95
|
+
const props = tabMap[tabName];
|
|
96
|
+
if (props) {
|
|
97
|
+
return `<${tabPanelTagName} class="${tabPanelTagClass}" name="${tabName}">` + getHighlightedContent(props.content.join("\n"), props.attrs) + `</${tabPanelTagName}>`;
|
|
98
|
+
}
|
|
99
|
+
return "";
|
|
100
|
+
}).join("\n")
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function extractCodeLineProps(lines, attrs) {
|
|
104
|
+
const acc = {};
|
|
105
|
+
for (const type of magicCommentList) {
|
|
106
|
+
acc[type] = attrs[type] !== void 0 ? attrs[type].split(",") : [];
|
|
107
|
+
}
|
|
108
|
+
lines.forEach((line, lineIndex) => {
|
|
109
|
+
const match = line.match(magicCommentRE);
|
|
110
|
+
if (match !== null && match.groups) {
|
|
111
|
+
const { type } = match.groups;
|
|
112
|
+
if (type && type in acc) {
|
|
113
|
+
acc[type].push("" + (lineIndex + 1));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return acc;
|
|
118
|
+
}
|
|
119
|
+
function parseCodeLine(content, attrs) {
|
|
120
|
+
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
121
|
+
const lines = normalized.split("\n");
|
|
122
|
+
const props = extractCodeLineProps(lines, attrs);
|
|
123
|
+
const acc = lines.map(() => ({ prefix: [], classList: [] }));
|
|
124
|
+
const hasRemOrAdd = (props.rem?.length || 0) > 0 || (props.add?.length || 0) > 0;
|
|
125
|
+
for (const type of magicCommentList) {
|
|
126
|
+
const target = props[type];
|
|
127
|
+
if (target) {
|
|
128
|
+
for (const value of target) {
|
|
129
|
+
const [fromStr, toStr] = value.split("-");
|
|
130
|
+
const from = parseInt(String(fromStr), 10);
|
|
131
|
+
const to = toStr ? parseInt(toStr, 10) : from;
|
|
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
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (attrs.numbered === true) {
|
|
143
|
+
const lineCount = String(lines.length).length;
|
|
144
|
+
lines.forEach((_, lineIndex) => {
|
|
145
|
+
if (acc[lineIndex]) {
|
|
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
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return acc;
|
|
163
|
+
}
|
|
164
|
+
function getHighlightedContent(rawContent, attrs) {
|
|
165
|
+
const { lang = defaultLang, maxheight } = attrs;
|
|
166
|
+
const content = rawContent.trim();
|
|
167
|
+
const lineList = parseCodeLine(content, attrs);
|
|
168
|
+
const grammar = prism.languages[lang || defaultLang];
|
|
169
|
+
const html = prism.highlight(content.replace(magicCommentGlobalRE, ""), grammar, lang).split("\n").map((line, lineIndex) => {
|
|
170
|
+
const target = lineList[lineIndex];
|
|
171
|
+
let lineHtml = "";
|
|
172
|
+
if (target) {
|
|
173
|
+
if (target.classList.length !== 0) {
|
|
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
|
+
}
|
|
180
|
+
lineHtml += line;
|
|
181
|
+
return lineHtml;
|
|
182
|
+
}).join("\n");
|
|
183
|
+
const langClass = lang === "css" ? " language-css" : ` language-${lang}`;
|
|
184
|
+
const preAttrs = maxheight !== void 0 ? ` style="max-height:${maxheight}"` : "";
|
|
185
|
+
const langProp = customCopyLangList.includes(lang) === true ? ` lang="${lang}"` : "";
|
|
186
|
+
const results = `<pre v-pre class="${preClass}${langClass}"${preAttrs}><code${codeClass ? ` class="${codeClass}"` : ""}>${!!grammar ? html : rawContent}</code></pre>` + (copyButtonComponent ? `<${copyButtonComponent} ${langProp} />` : "");
|
|
187
|
+
return results;
|
|
188
|
+
}
|
|
189
|
+
function parseDefinitionLine(token) {
|
|
190
|
+
const match = token.info.trim().match(definitionLineRE);
|
|
191
|
+
if (match === null) {
|
|
192
|
+
return {
|
|
193
|
+
lang: defaultLang,
|
|
194
|
+
title: null
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const { groups } = match;
|
|
198
|
+
const acc = {
|
|
199
|
+
...parseAttrs(groups?.attrs?.trim() || null),
|
|
200
|
+
lang: groups?.lang || defaultLang,
|
|
201
|
+
title: groups?.title?.trim() || null
|
|
202
|
+
};
|
|
203
|
+
if (acc.lang === "tabs") {
|
|
204
|
+
const tabs = extractTabs(token.content);
|
|
205
|
+
if (tabs) {
|
|
206
|
+
acc.tabs = tabs;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return acc;
|
|
210
|
+
}
|
|
211
|
+
md.renderer.rules.fence = (tokens, idx, _options, env) => {
|
|
212
|
+
const token = tokens[idx];
|
|
213
|
+
if (!token) {
|
|
214
|
+
return "";
|
|
215
|
+
}
|
|
216
|
+
const attrs = parseDefinitionLine(token);
|
|
217
|
+
if (pageScripts.length > 0) {
|
|
218
|
+
if (!env.pageScripts) {
|
|
219
|
+
env.pageScripts = /* @__PURE__ */ new Set();
|
|
220
|
+
}
|
|
221
|
+
for (const script of pageScripts) {
|
|
222
|
+
env.pageScripts.add(script);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return `<${containerComponent}${attrs.title !== null ? ` title="${attrs.title}"` : ""}${"tabs" in attrs && attrs.tabs !== void 0 ? ` :tabs="${attrs.tabs.param}"` : ""}>` + ("tabs" in attrs && attrs.tabs !== void 0 ? attrs.tabs.content : getHighlightedContent(token.content, attrs)) + `</${containerComponent}>`;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
export { codeblocksPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@md-plugins/md-plugin-codeblocks",
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "A markdown-it plugin for code blocks.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"markdown-it",
|
|
7
|
+
"quasarframework",
|
|
8
|
+
"vue",
|
|
9
|
+
"types"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/md-plugins",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/md-plugins/md-plugins/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/md-plugins/md-plugins.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "hawkeye64 <galbraith64@gmail.com>",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
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.1"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/markdown-it": "^14.1.2",
|
|
45
|
+
"@types/prismjs": "^1.26.5"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "unbuild",
|
|
49
|
+
"clean": "rm -rf dist",
|
|
50
|
+
"test": "vitest"
|
|
51
|
+
}
|
|
52
|
+
}
|