Skip to content

Commit 84ccfc5

Browse files
committed
feat(preprocess-readme): support svelte no-eval code block attribute #20
1 parent 58f514f commit 84ccfc5

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/preprocessReadme.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ export function preprocessReadme(opts: Partial<PreprocessReadmeOptions>): Pick<P
4141
html: true,
4242
linkify: true,
4343
typographer: true,
44-
highlight(source, lang) {
44+
highlight(source, lang, attrs) {
4545
if (lang === "svelte") {
46+
const noEval = /no-eval/.test(attrs);
4647
const { instance } = parse(source);
4748

48-
if (instance !== undefined) {
49+
if (instance !== undefined && !noEval) {
4950
script_content = [
5051
...script_content,
5152
...source
@@ -64,7 +65,9 @@ export function preprocessReadme(opts: Partial<PreprocessReadmeOptions>): Pick<P
6465
svelteBracketNewLine: true,
6566
});
6667
const svelteCode = Prism.highlight(formattedCode, Prism.languages.svelte, "svelte");
67-
return `<pre class="language-${lang}" data-svelte="${modifiedSource}">{@html \`${svelteCode}\`}</pre>`;
68+
return `<pre class="language-${lang}" ${
69+
noEval ? "" : `data-svelte="${modifiedSource}"`
70+
}>{@html \`${svelteCode}\`}</pre>`;
6871
}
6972

7073
try {

test/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ Second script block:
8787
8888
```
8989

90+
```svelte eval
91+
<script>
92+
import { onMount } from "svelte";
93+
94+
onMount(() => {
95+
console.log('eval')
96+
});
97+
</script>
98+
99+
```
100+
90101
```svelte no-eval
91102
<script>
92103
import { onMount } from "svelte";

0 commit comments

Comments
 (0)