Skip to content

Commit

Permalink
feat: improve md render
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackman99 committed Dec 8, 2024
1 parent 7d6ec72 commit eb98ca7
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-pumas-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltepress/vite": patch
---

fix: frontmatter should respect user defined in +page.svelte files"
8 changes: 8 additions & 0 deletions .changeset/cool-donuts-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@sveltepress/theme-default": patch
"@sveltepress/vite": patch
"@sveltepress/create": patch
"@sveltepress/twoslash": patch
---

feat: command in md
8 changes: 8 additions & 0 deletions .changeset/cuddly-trainers-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@sveltepress/theme-default": patch
"@sveltepress/vite": patch
"@sveltepress/create": patch
"@sveltepress/twoslash": patch
---

docs: update links"
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description: some description

## Svelte এ Frontmatter

[Svelte context module](https://svelte.dev/docs#component-format-script-context-module)`frontmatter` নামক একটি const variable এক্সপোর্ট করুন
[Svelte script module](https://svelte.dev/docs/svelte/svelte-files#script-module)`frontmatter` নামক একটি const variable এক্সপোর্ট করুন

```svelte title="/src/routes/foo/+page.svelte"
<script module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description: 页面描述

## Svelte 中的 frontmatter

`+page.svelte`[Svelte context module](https://svelte.dev/docs#component-format-script-context-module)中导出一个名为 `frontmatter` 的变量即可
`+page.svelte` [Svelte script module](https://svelte.dev/docs/svelte/svelte-files#script-module) 中导出一个名为 `frontmatter` 的变量即可

```svelte title="/src/routes/foo/+page.svelte"
<script module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description: some description

## Frontmatter in svelte

Export a const variable name as `frontmatter` in [Svelte context module](https://svelte.dev/docs#component-format-script-context-module) would do
Export a const variable name as `frontmatter` in [Svelte script module](https://svelte.dev/docs/svelte/svelte-files#script-module) would do

```svelte title="/src/routes/foo/+page.svelte"
<script module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,22 @@ Toggle

:::note[Syntax Restrictions]{icon=solar:chat-square-code-outline}
Always use quotes in markdown files.
```svelte
```md
<script>
let count = $state(0)
</script>
<button onclick={() => count++}></button> // [svp! --]
<button onclick={() => count++}></button> // [svp! ++]
<button onclick="{() => count++}"></button> // [svp! ++]
```
Or you can wrap it with a `div`
```md
<script>
let count = $state(0)
</script>
<button onclick={() => count++}></button> // [svp! --]
<div> // [svp! ++]
<button onclick={() => count++}></button> // [svp! ++]
</div> // [svp! ++]
```
:::

Expand All @@ -141,9 +151,11 @@ Always use quotes in markdown files.

> A counter
<button onclick="{() => count++}" style="margin-bottom: 12px;">
You've clicked {count} times
</button>
<div>
<button onclick={() => count++} style="margin-bottom: 12px;">
You've clicked {count} times
</button>
</div>

</TabPanel>

Expand All @@ -155,9 +167,11 @@ Always use quotes in markdown files.
let count = $state(0)
</script>

<button onclick="{() => count++}">
You've clicked {count} times
</button>
<div>
<button onclick={() => count++} style="margin-bottom: 12px;">
You've clicked {count} times
</button>
</div>
```

</TabPanel>
Expand Down
12 changes: 5 additions & 7 deletions packages/theme-default/src/markdown/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ const highlighter: Highlighter = async (code, lang, meta) => {
let noErrorsFirstLine: string | undefined
if (lines[0] === '// @noErrors')
noErrorsFirstLine = lines.shift()
if (lang !== 'md') {
code = lines.map((line, i) => {
const [commandDomsInOneLine, newLine] = processCommands(line, i, lines.length)
commandDoms.push(...commandDomsInOneLine)
return newLine
}).join('\n')
}
code = lines.map((line, i) => {
const [commandDomsInOneLine, newLine] = processCommands(line, i, lines.length)
commandDoms.push(...commandDomsInOneLine)
return newLine
}).join('\n')
let title: string | undefined
if (titleMeta)
title = titleMeta.split('=')[1].replace(/(^")|("$)/g, '')
Expand Down
24 changes: 15 additions & 9 deletions packages/vite/__tests__/md-svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ function hello(msg) {
something
{/snippet}
<button onclick="{() => count++}">
You've clicked {count} times
</button>
<div>
<button onclick={() => count++}>
You've clicked {count} times
</button>
</div>
{@render someSnippet()}
Expand Down Expand Up @@ -73,9 +75,11 @@ describe('md to svelte', () => {
{#snippet someSnippet()}
something
{/snippet}
<button onclick="{() => count++}">
You've clicked {count} times
</button>
<div>
<button onclick={() => count++}>
You've clicked {count} times
</button>
</div>
{@render someSnippet()}
<style>
.foo {
Expand Down Expand Up @@ -128,9 +132,11 @@ describe('md to svelte', () => {
{#snippet someSnippet()}
something
{/snippet}
<button onclick="{() => count++}">
You've clicked {count} times
</button>
<div>
<button onclick={() => count++}>
You've clicked {count} times
</button>
</div>
{@render someSnippet()}
</PageLayout>
<style>
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/__tests__/parse-svelte-frontmatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function wrapFrontmatter(obj: Record<string, any>) {
export const frontmatter: any = ${JSON.stringify(obj)}
</script>
<script lang="ts">
export let propName: string
let { propName } = $props()
const someVariable = 'some value'
</script>
Expand Down
4 changes: 0 additions & 4 deletions packages/vite/src/markdown/md-to-svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,3 @@ export function applyRemarkPluginsBeforeRehype(remarkPlugins?: Array<Plugin<any[

return processorAfterRemarkParse
}

export function getBuiltinRemarkExtensions() {

}
2 changes: 1 addition & 1 deletion packages/vite/src/utils/wrap-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export async function wrapPage({
}
else if (id.endsWith('page.svelte')) {
fm = {
...(await parseSvelteFrontmatter(mdOrSvelteCode)),
pageType: 'svelte',
lastUpdate,
...(await parseSvelteFrontmatter(mdOrSvelteCode)),
}
svelteCode = mdOrSvelteCode
}
Expand Down

0 comments on commit eb98ca7

Please sign in to comment.