Skip to content

Commit c4ed9fd

Browse files
authored
use github-like Tip & Warning syntax (#468)
* use github-like Tip & Warning syntax * update readme
1 parent f1a4880 commit c4ed9fd

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ markers.
236236

237237
Syntax:
238238

239+
```md
240+
> [!TIP]
241+
> Here is a tip. Go to this url [website](www.tip.com)
242+
>
243+
> Second line
244+
```
245+
246+
or
247+
239248
```html
240249
<Tip>
241250

@@ -249,6 +258,13 @@ Example: [here](https://github.com/huggingface/transformers/blob/0f0e1a2c2bff685
249258
For warnings, change the introduction to:
250259

251260
Syntax:
261+
262+
```md
263+
> [!WARNING]
264+
```
265+
266+
or
267+
252268
```html
253269
`<Tip warning={true}>`
254270
```

kit/preprocessors/mdsvex/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ function treeVisitor() {
174174
});
175175
visit(tree, "text", onText);
176176
visit(tree, "html", onHtml);
177+
visit(tree, "blockquote", onBlockquote);
177178

178179
let jsonString = JSON.stringify(headings[0]);
179180
if (jsonString) {
@@ -239,6 +240,42 @@ function treeVisitor() {
239240
}
240241
}
241242
}
243+
244+
function onBlockquote(node, index, parent) {
245+
// use github-like Tip & Warning syntax
246+
// see https://github.com/orgs/community/discussions/16925
247+
const { children: childrenLevel1 } = node;
248+
if (!childrenLevel1.length || childrenLevel1[0].type !== "paragraph") {
249+
return;
250+
}
251+
252+
const { children: childrenLevel2 } = childrenLevel1[0];
253+
if (!childrenLevel2.length || childrenLevel2[0].type !== "linkReference") {
254+
return;
255+
}
256+
257+
const TIP_MARKERS = ["!tip", "!warning"];
258+
const { identifier } = childrenLevel2[0];
259+
if (!TIP_MARKERS.includes(identifier)) {
260+
return;
261+
}
262+
263+
if (!parent) {
264+
return;
265+
}
266+
267+
childrenLevel1[0].children = childrenLevel1[0].children.slice(1);
268+
const nodeTagOpen = {
269+
type: "html",
270+
value: `<Tip warning={${identifier === "!warning"}}>\n\n`,
271+
};
272+
const nodeTagClose = {
273+
type: "html",
274+
value: "\n\n</Tip>",
275+
};
276+
const nodes = [nodeTagOpen, ...childrenLevel1, nodeTagClose];
277+
parent.children.splice(index, 1, ...nodes);
278+
}
242279
}
243280

244281
const _mdsvexPreprocess = mdsvex({

0 commit comments

Comments
 (0)