Skip to content

Commit 298b6af

Browse files
authored
feat(plugins): transform note styles (#52)
* feat(plugins): transform note styles * chore: add comment * refactor: handle multiple lines with more container types
1 parent c31623d commit 298b6af

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

scripts/plugin-scraper.mjs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function main() {
4646
// save the file
4747
fse.outputFileSync(
4848
`./${plugin.link}.md`,
49-
`${headerSnippet}\n\n${repoSnippet}\n\n${data.trim()}`
49+
`${headerSnippet}\n\n${repoSnippet}\n\n${transformNoteStyle(data)}`
5050
)
5151
log(chalk.green(`File saved for plugin: ${plugin.name}`))
5252
}
@@ -80,4 +80,38 @@ async function main() {
8080
}
8181
}
8282

83+
function transformNoteStyle(content) {
84+
// transforms note styles for vitepress
85+
const debug = false;
86+
const NOTE_RE = /^>\s+\*{2}([^\*]+)\*{2}:?[^\S\r\n]+(.+(?:\n>.*)*)/gim;
87+
let match_;
88+
const containerTypeMap = {
89+
'note': 'tip',
90+
'warning': 'warning',
91+
}
92+
93+
while(match_ = NOTE_RE.exec(content)) {
94+
let [match, title, cont] = match_;
95+
96+
debug && console.log({
97+
match, title, cont
98+
})
99+
100+
// normalize title
101+
title = title.replace(/:/g, '');
102+
103+
// replace "> " from multiline quotes
104+
cont = cont.replace(/^>\s*/gim, '');
105+
106+
// generate replacement
107+
let containerType = containerTypeMap[title.toLowerCase()] ?? 'tip';
108+
const replacement = `:::${containerType} ${title}\n\n${cont}\n\n:::`;
109+
110+
// replace the match with the replacement
111+
content = content.replace(match, replacement);
112+
}
113+
114+
return content;
115+
}
116+
83117
main()

0 commit comments

Comments
 (0)