@scalar/code-highlight 0.0.22 → 0.0.24
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/CHANGELOG.md +14 -0
- package/dist/code/highlight.d.ts.map +1 -1
- package/dist/code/highlight.js +2 -6
- package/dist/code/line-numbers.d.ts.map +1 -1
- package/dist/code/line-numbers.js +5 -14
- package/dist/css/code.css +1 -1
- package/dist/css/markdown.css +73 -5
- package/dist/markdown/markdown.d.ts.map +1 -1
- package/dist/markdown/markdown.js +5 -0
- package/dist/rehype-alert/index.d.ts +2 -0
- package/dist/rehype-alert/index.d.ts.map +1 -0
- package/dist/rehype-alert/index.js +1 -0
- package/dist/rehype-alert/rehype-alert.d.ts +3 -0
- package/dist/rehype-alert/rehype-alert.d.ts.map +1 -0
- package/dist/rehype-alert/rehype-alert.js +92 -0
- package/dist/rehype-highlight/rehype-highlight.d.ts.map +1 -1
- package/dist/rehype-highlight/rehype-highlight.js +2 -7
- package/package.json +9 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @scalar/code-highlight
|
|
2
2
|
|
|
3
|
+
## 0.0.24
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9319c0c: fix: allows alert plugin classes in markdown
|
|
8
|
+
- 9319c0c: feat: updates markdown alert style
|
|
9
|
+
|
|
10
|
+
## 0.0.23
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 3feee27: feat: adds markdown alert support
|
|
15
|
+
- c10bbf5: chore: code style
|
|
16
|
+
|
|
3
17
|
## 0.0.22
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../../src/code/highlight.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAW9C;;GAEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE;IACP,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,
|
|
1
|
+
{"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../../src/code/highlight.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAW9C;;GAEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE;IACP,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,UA4CF"}
|
package/dist/code/highlight.js
CHANGED
|
@@ -11,9 +11,7 @@ import { rehypeHighlight } from '../rehype-highlight/rehype-highlight.js';
|
|
|
11
11
|
*/
|
|
12
12
|
function syntaxHighlight(codeString, options) {
|
|
13
13
|
// Simple restriction on credentials to prevent unexpected behavior
|
|
14
|
-
const credentials = (typeof options?.maskCredentials === 'string'
|
|
15
|
-
? [options.maskCredentials]
|
|
16
|
-
: (options?.maskCredentials ?? [])).filter((c) => {
|
|
14
|
+
const credentials = (typeof options?.maskCredentials === 'string' ? [options.maskCredentials] : (options?.maskCredentials ?? [])).filter((c) => {
|
|
17
15
|
// Credentials must be at least 3 characters to mask.
|
|
18
16
|
if (c.length < 3) {
|
|
19
17
|
return false;
|
|
@@ -41,9 +39,7 @@ function syntaxHighlight(codeString, options) {
|
|
|
41
39
|
const htmlString = html.toString();
|
|
42
40
|
// Replace any credentials with a wrapper element
|
|
43
41
|
return credentials.length
|
|
44
|
-
? credentials.reduce((acc, credential) => acc
|
|
45
|
-
.split(credential)
|
|
46
|
-
.join(`<span class="credentials">${credential}</span>`), htmlString)
|
|
42
|
+
? credentials.reduce((acc, credential) => acc.split(credential).join(`<span class="credentials">${credential}</span>`), htmlString)
|
|
47
43
|
: htmlString;
|
|
48
44
|
}
|
|
49
45
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"line-numbers.d.ts","sourceRoot":"","sources":["../../src/code/line-numbers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAM/D,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,IAAI,IAAI,CAEhE;AAED,wBAAgB,SAAS,CAAC,IAAI,CAAC,EAAE,cAAc,GAAG,IAAI,IAAI,OAAO,CAEhE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAE/C;AAED,wBAAgB,SAAS,IAAI,IAAI,CAEhC;AAED;;GAEG;AACH,wBAAgB,oBAAoB,WACpB,IAAI,
|
|
1
|
+
{"version":3,"file":"line-numbers.d.ts","sourceRoot":"","sources":["../../src/code/line-numbers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAM/D,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,IAAI,IAAI,CAEhE;AAED,wBAAgB,SAAS,CAAC,IAAI,CAAC,EAAE,cAAc,GAAG,IAAI,IAAI,OAAO,CAEhE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAE/C;AAED,wBAAgB,SAAS,IAAI,IAAI,CAEhC;AAED;;GAEG;AACH,wBAAgB,oBAAoB,WACpB,IAAI,UA2BnB"}
|
|
@@ -20,9 +20,7 @@ function lineBreak() {
|
|
|
20
20
|
function codeBlockLinesPlugin() {
|
|
21
21
|
return (tree) => {
|
|
22
22
|
visit(tree, 'element', (node, _i, parent) => {
|
|
23
|
-
if (parent?.type === 'element' &&
|
|
24
|
-
parent.tagName === 'pre' &&
|
|
25
|
-
node.tagName === 'code') {
|
|
23
|
+
if (parent?.type === 'element' && parent.tagName === 'pre' && node.tagName === 'code') {
|
|
26
24
|
let numLines = 0;
|
|
27
25
|
// Wraps each line in a span
|
|
28
26
|
node.children = addLines(node);
|
|
@@ -30,19 +28,14 @@ function codeBlockLinesPlugin() {
|
|
|
30
28
|
node.children.forEach((child) => {
|
|
31
29
|
if (child.type === 'element' && child.tagName === 'span') {
|
|
32
30
|
const lastChild = child.children[child.children.length - 1];
|
|
33
|
-
if (lastChild &&
|
|
34
|
-
(!isText(lastChild) ||
|
|
35
|
-
(isText(lastChild) && !hasLineBreak(lastChild)))) {
|
|
31
|
+
if (lastChild && (!isText(lastChild) || (isText(lastChild) && !hasLineBreak(lastChild)))) {
|
|
36
32
|
child.children.push(lineBreak());
|
|
37
33
|
numLines++;
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
36
|
});
|
|
41
37
|
// We need to maintain a count of the total lines to allow space for the labels
|
|
42
|
-
node.properties.style = [
|
|
43
|
-
`--line-count: ${numLines};`,
|
|
44
|
-
`--line-digits: ${numLines.toString().length};`,
|
|
45
|
-
];
|
|
38
|
+
node.properties.style = [`--line-count: ${numLines};`, `--line-digits: ${numLines.toString().length};`];
|
|
46
39
|
}
|
|
47
40
|
});
|
|
48
41
|
// console.log('NUMBER OF LINES IS: ', numLines)
|
|
@@ -56,8 +49,7 @@ function codeBlockLinesPlugin() {
|
|
|
56
49
|
* @param copyParent - Whether to copy the parent node to save the original node styles
|
|
57
50
|
*/
|
|
58
51
|
function addLines(node, lines = [], copyParent) {
|
|
59
|
-
const line = () => lines[lines.length - 1] ??
|
|
60
|
-
(lines.push(createLine()) && lines[lines.length - 1]);
|
|
52
|
+
const line = () => lines[lines.length - 1] ?? (lines.push(createLine()) && lines[lines.length - 1]);
|
|
61
53
|
node.children.forEach((child) => {
|
|
62
54
|
if (isText(child) && hasLineBreak(child)) {
|
|
63
55
|
const split = child.value.split(/\n/);
|
|
@@ -96,8 +88,7 @@ function createLine(...children) {
|
|
|
96
88
|
* @param node - The node to check
|
|
97
89
|
*/
|
|
98
90
|
function hasLineBreak(node) {
|
|
99
|
-
return (
|
|
100
|
-
(isElement(node) && node.children.some(hasLineBreak)));
|
|
91
|
+
return (isText(node) && /\r?\n/.test(node.value)) || (isElement(node) && node.children.some(hasLineBreak));
|
|
101
92
|
}
|
|
102
93
|
|
|
103
94
|
export { codeBlockLinesPlugin, isElement, isText, lineBreak, textElement };
|
package/dist/css/code.css
CHANGED
package/dist/css/markdown.css
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
@import url(
|
|
1
|
+
@import url("./code.css");
|
|
2
2
|
|
|
3
3
|
.scalar-app {
|
|
4
4
|
.markdown {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
|
|
44
44
|
.markdown summary::after {
|
|
45
45
|
display: block;
|
|
46
|
-
content:
|
|
46
|
+
content: "";
|
|
47
47
|
|
|
48
48
|
position: absolute;
|
|
49
49
|
top: -1px;
|
|
@@ -284,13 +284,81 @@
|
|
|
284
284
|
background: var(--scalar-background-2);
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
-
.markdown tr > [align=
|
|
287
|
+
.markdown tr > [align="left"] {
|
|
288
288
|
text-align: left;
|
|
289
289
|
}
|
|
290
|
-
.markdown tr > [align=
|
|
290
|
+
.markdown tr > [align="right"] {
|
|
291
291
|
text-align: right;
|
|
292
292
|
}
|
|
293
|
-
.markdown tr > [align=
|
|
293
|
+
.markdown tr > [align="center"] {
|
|
294
294
|
text-align: center;
|
|
295
295
|
}
|
|
296
|
+
|
|
297
|
+
/* Markdown Alert */
|
|
298
|
+
.markdown .markdown-alert {
|
|
299
|
+
align-items: stretch;
|
|
300
|
+
border-radius: var(--scalar-radius-lg);
|
|
301
|
+
background-color: color-mix(in srgb, var(--scalar-background-2), transparent);
|
|
302
|
+
border: var(--scalar-border-width) solid var(--scalar-border-color);
|
|
303
|
+
display: flex;
|
|
304
|
+
font-size: var(--scalar-small);
|
|
305
|
+
gap: 12px;
|
|
306
|
+
padding: 12px;
|
|
307
|
+
padding-left: 42px;
|
|
308
|
+
position: relative;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
.markdown .markdown-alert::before {
|
|
312
|
+
content: "";
|
|
313
|
+
position: absolute;
|
|
314
|
+
left: 12px;
|
|
315
|
+
top: 12px;
|
|
316
|
+
width: 20px;
|
|
317
|
+
height: 20px;
|
|
318
|
+
background-color: currentColor;
|
|
319
|
+
mask-repeat: no-repeat;
|
|
320
|
+
mask-size: contain;
|
|
321
|
+
mask-position: center;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.markdown .markdown-alert.markdown-alert-note::before,
|
|
325
|
+
.markdown .markdown-alert.markdown-alert-tip::before {
|
|
326
|
+
mask-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 17v-6a.5.5 0 0 0-.5-.5l-.5.001h-1M12 17h-2m2 0h2m-2 5c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10Z" stroke="currentColor" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/><path fill-rule="evenodd" clip-rule="evenodd" d="M10.75 7.5a1 1 0 1 1 2 0 1 1 0 0 1-2 0Z" fill="currentColor"/></svg>');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
.markdown .markdown-alert.markdown-alert-important::before,
|
|
330
|
+
.markdown .markdown-alert.markdown-alert-warning::before {
|
|
331
|
+
mask-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 8v4m10 0c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path fill-rule="evenodd" clip-rule="evenodd" d="M11 16a1 1 0 1 1 2 0 1 1 0 0 1-2 0Z" fill="currentColor"/></svg>');
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
.markdown .markdown-alert.markdown-alert-caution::before {
|
|
335
|
+
mask-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 8v4m3.312-10a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586l-4.688-4.688A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2h6.624Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path fill-rule="evenodd" clip-rule="evenodd" d="M11 16a1 1 0 1 1 2 0 1 1 0 0 1-2 0Z" fill="currentColor"/></svg>');
|
|
336
|
+
color: var(--scalar-color-red);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
.markdown .markdown-alert.markdown-alert-success::before {
|
|
340
|
+
mask-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M16 9.5 10.5 15 8 12.5m14-.5c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>');
|
|
341
|
+
color: var(--scalar-color-green);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
.markdown .markdown-alert.markdown-alert-note::before {
|
|
345
|
+
color: var(--scalar-color-blue);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
.markdown .markdown-alert.markdown-alert-tip::before {
|
|
349
|
+
color: var(--scalar-color-2);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
.markdown .markdown-alert.markdown-alert-important::before {
|
|
353
|
+
color: var(--scalar-color-purple);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
.markdown .markdown-alert.markdown-alert-warning::before {
|
|
357
|
+
color: var(--scalar-color-orange);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
.markdown .markdown-alert .markdown-alert-content {
|
|
361
|
+
margin: 0;
|
|
362
|
+
line-height: 1.5;
|
|
363
|
+
}
|
|
296
364
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/markdown/markdown.ts"],"names":[],"mappings":"AAyCA;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC9D,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB,
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/markdown/markdown.ts"],"names":[],"mappings":"AAyCA;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC9D,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB,UAqDF;AASD;;GAEG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,KAAK,GAAE,MAAU,GAChB;IACD,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd,EAAE,CAiBF;AAuBD;;GAEG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,YAgC5C"}
|
|
@@ -9,6 +9,7 @@ import remarkRehype from 'remark-rehype';
|
|
|
9
9
|
import remarkStringify from 'remark-stringify';
|
|
10
10
|
import { unified } from 'unified';
|
|
11
11
|
import { visit, SKIP } from 'unist-util-visit';
|
|
12
|
+
import { rehypeAlert } from '../rehype-alert/rehype-alert.js';
|
|
12
13
|
import { rehypeHighlight } from '../rehype-highlight/rehype-highlight.js';
|
|
13
14
|
import { standardLanguages } from '../languages/standard.js';
|
|
14
15
|
|
|
@@ -43,6 +44,8 @@ function htmlFromMarkdown(markdown, options) {
|
|
|
43
44
|
})
|
|
44
45
|
// Allows any HTML tags
|
|
45
46
|
.use(remarkRehype, { allowDangerousHtml: true })
|
|
47
|
+
// Adds GitHub alerts
|
|
48
|
+
.use(rehypeAlert)
|
|
46
49
|
// Creates a HTML AST
|
|
47
50
|
.use(rehypeRaw)
|
|
48
51
|
// Removes disallowed tags
|
|
@@ -55,6 +58,8 @@ function htmlFromMarkdown(markdown, options) {
|
|
|
55
58
|
attributes: {
|
|
56
59
|
...defaultSchema.attributes,
|
|
57
60
|
abbr: ['title'],
|
|
61
|
+
// Allow alert classes
|
|
62
|
+
div: ['class', ['className', /^markdown-alert(-.*)?$/]],
|
|
58
63
|
},
|
|
59
64
|
})
|
|
60
65
|
// Syntax highlighting
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rehype-alert/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rehypeAlert } from './rehype-alert.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rehype-alert.d.ts","sourceRoot":"","sources":["../../src/rehype-alert/rehype-alert.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAA;AASjC,wBAAgB,WAAW,WACX,IAAI,UAkFnB"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
|
|
3
|
+
const ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'];
|
|
4
|
+
// Simple whitespace check function
|
|
5
|
+
function isWhitespace(node) {
|
|
6
|
+
return node.type === 'text' && typeof node.value === 'string' && /^\s*$/.test(node.value);
|
|
7
|
+
}
|
|
8
|
+
function rehypeAlert() {
|
|
9
|
+
return (tree) => {
|
|
10
|
+
visit(tree, 'element', (node, index, parent) => {
|
|
11
|
+
if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
// Find the first non-whitespace child
|
|
15
|
+
const headIndex = node.children.findIndex((child) => !isWhitespace(child));
|
|
16
|
+
if (headIndex === -1)
|
|
17
|
+
return;
|
|
18
|
+
const head = node.children[headIndex];
|
|
19
|
+
if (!head || head.type !== 'element' || head.tagName !== 'p')
|
|
20
|
+
return;
|
|
21
|
+
const text = head.children[0];
|
|
22
|
+
if (!text || text.type !== 'text' || !text.value.startsWith('[!'))
|
|
23
|
+
return;
|
|
24
|
+
const end = text.value.indexOf(']');
|
|
25
|
+
if (end === -1)
|
|
26
|
+
return;
|
|
27
|
+
// Extract the alert type
|
|
28
|
+
const alertType = text.value.slice(2, end).toLowerCase();
|
|
29
|
+
if (!ALERT_TYPES.includes(alertType))
|
|
30
|
+
return;
|
|
31
|
+
// Remove the blockquote if it's empty
|
|
32
|
+
if (end + 1 === text.value.length) {
|
|
33
|
+
const next = head.children[1];
|
|
34
|
+
if (next) {
|
|
35
|
+
if (next.type !== 'element' || next.tagName !== 'br')
|
|
36
|
+
return;
|
|
37
|
+
if (!head.children[2])
|
|
38
|
+
return;
|
|
39
|
+
head.children = head.children.slice(2);
|
|
40
|
+
const node = head.children[0];
|
|
41
|
+
if (node && node.type === 'text' && node.value.charAt(0) === '\n') {
|
|
42
|
+
node.value = node.value.slice(1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]);
|
|
47
|
+
const nextIndex = skipped ? headIndex + 2 : headIndex + 1;
|
|
48
|
+
if (nextIndex >= node.children.length || node.children[nextIndex].type !== 'element') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
node.children = node.children.slice(nextIndex);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (text.value.charAt(end + 1) === '\n' &&
|
|
55
|
+
// Check if the next character is a newline or a non-whitespace character
|
|
56
|
+
(end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) {
|
|
57
|
+
text.value = text.value.slice(end + 2);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Remove the alert marker if it’s not followed by a newline or a non-whitespace character
|
|
61
|
+
text.value = text.value.replace(/^\s*\[!.*?\]\s*/, '');
|
|
62
|
+
}
|
|
63
|
+
// Extract content from paragraphs to avoid wrapping in <p> tags
|
|
64
|
+
const contentChildren = [];
|
|
65
|
+
for (let i = headIndex; i < node.children.length; i++) {
|
|
66
|
+
const child = node.children[i];
|
|
67
|
+
if (child.type === 'element' && child.tagName === 'p' && child.children) {
|
|
68
|
+
contentChildren.push(...child.children);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
contentChildren.push(child);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Replace the blockquote with a div containing the alert
|
|
75
|
+
parent.children[index] = {
|
|
76
|
+
type: 'element',
|
|
77
|
+
tagName: 'div',
|
|
78
|
+
properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },
|
|
79
|
+
children: [
|
|
80
|
+
{
|
|
81
|
+
type: 'element',
|
|
82
|
+
tagName: 'div',
|
|
83
|
+
properties: { className: ['markdown-alert-content'] },
|
|
84
|
+
children: [{ type: 'text', value: ' ' }, ...contentChildren],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { rehypeAlert };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehype-highlight.d.ts","sourceRoot":"","sources":["../../src/rehype-highlight/rehype-highlight.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2B,IAAI,EAAE,MAAM,MAAM,CAAA;AAEzD,OAAO,EAAE,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAIlC,KAAK,gBAAgB,GAAG;IACtB,iDAAiD;IACjD,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,GAAG,SAAS,CAAA;IACxD,2EAA2E;IAC3E,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"rehype-highlight.d.ts","sourceRoot":"","sources":["../../src/rehype-highlight/rehype-highlight.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2B,IAAI,EAAE,MAAM,MAAM,CAAA;AAEzD,OAAO,EAAE,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAIlC,KAAK,gBAAgB,GAAG;IACtB,iDAAiD;IACjD,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,GAAG,SAAS,CAAA;IACxD,2EAA2E;IAC3E,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,CAAA;IACrF,2EAA2E;IAC3E,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,CAAA;IACnE,yGAAyG;IACzG,SAAS,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,SAAS,CAAA;IACpD,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;IAClC,qFAAqF;IACrF,MAAM,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,SAAS,CAAA;IACjD,qCAAqC;IACrC,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAID;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,gBAAgB,CAAC,GAAG,IAAI,GAAG,SAAS,UAqBvE,IAAI,QAAQ,KAAK,UAsDhC"}
|
|
@@ -29,16 +29,11 @@ function rehypeHighlight(options) {
|
|
|
29
29
|
/** Transform.*/
|
|
30
30
|
return (tree, file) => {
|
|
31
31
|
visit(tree, 'element', (node, _, parent) => {
|
|
32
|
-
if (node.tagName !== 'code' ||
|
|
33
|
-
!parent ||
|
|
34
|
-
parent.type !== 'element' ||
|
|
35
|
-
parent.tagName !== 'pre') {
|
|
32
|
+
if (node.tagName !== 'code' || !parent || parent.type !== 'element' || parent.tagName !== 'pre') {
|
|
36
33
|
return;
|
|
37
34
|
}
|
|
38
35
|
const lang = language(node);
|
|
39
|
-
if (lang === 'no-highlight' ||
|
|
40
|
-
(!lang && !detect) ||
|
|
41
|
-
(lang && plainText?.includes(lang))) {
|
|
36
|
+
if (lang === 'no-highlight' || (!lang && !detect) || (lang && plainText?.includes(lang))) {
|
|
42
37
|
return;
|
|
43
38
|
}
|
|
44
39
|
if (!Array.isArray(node.properties.className)) {
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"lowlight",
|
|
17
17
|
"highlightjs"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.0.
|
|
19
|
+
"version": "0.0.24",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
@@ -33,6 +33,11 @@
|
|
|
33
33
|
"types": "./dist/rehype-highlight/index.d.ts",
|
|
34
34
|
"default": "./dist/rehype-highlight/index.js"
|
|
35
35
|
},
|
|
36
|
+
"./rehype-alert": {
|
|
37
|
+
"import": "./dist/rehype-alert/index.js",
|
|
38
|
+
"types": "./dist/rehype-alert/index.d.ts",
|
|
39
|
+
"default": "./dist/rehype-alert/index.js"
|
|
40
|
+
},
|
|
36
41
|
"./markdown": {
|
|
37
42
|
"import": "./dist/markdown/index.js",
|
|
38
43
|
"types": "./dist/markdown/index.d.ts",
|
|
@@ -85,15 +90,15 @@
|
|
|
85
90
|
"unist-util-visit": "^5.0.0"
|
|
86
91
|
},
|
|
87
92
|
"devDependencies": {
|
|
88
|
-
"@rollup/plugin-typescript": "^
|
|
93
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
89
94
|
"@types/hast": "^3.0.4",
|
|
90
95
|
"@types/mdast": "^4.0.4",
|
|
91
96
|
"@types/unist": "^3.0.2",
|
|
92
97
|
"vfile": "^6.0.1",
|
|
93
98
|
"vite": "^5.4.10",
|
|
94
99
|
"vue": "^3.5.12",
|
|
95
|
-
"@scalar/build-tooling": "0.1.
|
|
96
|
-
"@scalar/themes": "0.9.
|
|
100
|
+
"@scalar/build-tooling": "0.1.17",
|
|
101
|
+
"@scalar/themes": "0.9.74"
|
|
97
102
|
},
|
|
98
103
|
"scripts": {
|
|
99
104
|
"build": "scalar-build-rollup",
|