Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use magic-string to do markdown pre-transform (#1496)
Co-authored-by: _Kerman <kermanx@qq.com>
- Loading branch information
Showing
19 changed files
with
279 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import { getCodeBlocks } from './utils' | ||
import type { MarkdownTransformContext } from '@slidev/types' | ||
|
||
/** | ||
* Transform <style> in markdown to scoped style with page selector | ||
*/ | ||
export function transformPageCSS(md: string, id: string) { | ||
export function transformPageCSS(ctx: MarkdownTransformContext, id: string) { | ||
const page = id.match(/(\d+)\.md$/)?.[1] | ||
if (!page) | ||
return md | ||
return | ||
|
||
const { isInsideCodeblocks } = getCodeBlocks(md) | ||
|
||
const result = md.replace( | ||
ctx.s.replace( | ||
/(\n<style[^>]*?>)([\s\S]+?)(<\/style>)/g, | ||
(full, start, css, end, index) => { | ||
// don't replace `<style>` inside code blocks, #101 | ||
if (index < 0 || isInsideCodeblocks(index)) | ||
if (ctx.isIgnored(index)) | ||
return full | ||
if (!start.includes('scoped')) | ||
start = start.replace('<style', '<style scoped') | ||
return `${start}\n${css}${end}` | ||
}, | ||
) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import lz from 'lz-string' | ||
import type { MarkdownTransformContext } from '@slidev/types' | ||
|
||
/** | ||
* Transform Mermaid code blocks (render done on client side) | ||
*/ | ||
export function transformMermaid(md: string): string { | ||
return md | ||
.replace(/^```mermaid\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', code = '') => { | ||
code = code.trim() | ||
options = options.trim() || '{}' | ||
const encoded = lz.compressToBase64(code) | ||
return `<Mermaid code-lz="${encoded}" v-bind="${options}" />` | ||
}) | ||
export function transformMermaid(ctx: MarkdownTransformContext) { | ||
ctx.s.replace(/^```mermaid\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', code = '', index: number) => { | ||
code = code.trim() | ||
options = options.trim() || '{}' | ||
const encoded = lz.compressToBase64(code) | ||
ctx.ignores.push([index, index + full.length]) | ||
return `<Mermaid code-lz="${encoded}" v-bind="${options}" />` | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { encode as encodePlantUml } from 'plantuml-encoder' | ||
import type { MarkdownTransformContext } from '@slidev/types' | ||
|
||
export function transformPlantUml(md: string, server: string): string { | ||
return md | ||
.replace(/^```plantuml\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', content = '') => { | ||
const code = encodePlantUml(content.trim()) | ||
options = options.trim() || '{}' | ||
return `<PlantUml :code="'${code}'" :server="'${server}'" v-bind="${options}" />` | ||
}) | ||
export function transformPlantUml(ctx: MarkdownTransformContext, server: string) { | ||
ctx.s.replace(/^```plantuml\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', content = '', index: number) => { | ||
const code = encodePlantUml(content.trim()) | ||
options = options.trim() || '{}' | ||
ctx.ignores.push([index, index + full.length]) | ||
return `<PlantUml :code="'${code}'" :server="'${server}'" v-bind="${options}" />` | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
import { getCodeBlocks } from './utils' | ||
import type { MarkdownTransformContext } from '@slidev/types' | ||
|
||
export function transformSlotSugar(md: string) { | ||
const lines = md.split(/\r?\n/g) | ||
export function transformSlotSugar( | ||
ctx: MarkdownTransformContext, | ||
) { | ||
const linesWithNewline = ctx.s.original.split(/(\r?\n)/g) | ||
const lines: string[] = [] | ||
for (let i = 0; i < linesWithNewline.length; i += 2) { | ||
const line = linesWithNewline[i] | ||
const newline = linesWithNewline[i + 1] || '' | ||
lines.push(line + newline) | ||
} | ||
|
||
let prevSlot = false | ||
|
||
const { isLineInsideCodeblocks } = getCodeBlocks(md) | ||
|
||
lines.forEach((line, idx) => { | ||
if (isLineInsideCodeblocks(idx)) | ||
let offset = 0 | ||
lines.forEach((line) => { | ||
const start = offset | ||
offset += line.length | ||
if (ctx.isIgnored(start)) | ||
return | ||
|
||
const match = line.trimEnd().match(/^::\s*([\w\.\-\:]+)\s*::$/) | ||
const match = line.match(/^::\s*([\w\.\-\:]+)\s*::(\s*)?$/) | ||
if (match) { | ||
lines[idx] = `${prevSlot ? '\n\n</template>\n' : '\n'}<template v-slot:${match[1]}="slotProps">\n` | ||
ctx.s.overwrite(start, offset - match[2].length, `${prevSlot ? '\n\n</template>\n' : '\n'}<template v-slot:${match[1]}="slotProps">\n`) | ||
prevSlot = true | ||
} | ||
}) | ||
|
||
if (prevSlot) | ||
lines[lines.length - 1] += '\n\n</template>' | ||
|
||
return lines.join('\n') | ||
ctx.s.append('\n\n</template>') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.