-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sébastien Chopin <[email protected]>
- Loading branch information
Showing
9 changed files
with
243 additions
and
21 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
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
71 changes: 71 additions & 0 deletions
71
src/runtime/markdown-parser/remark-mdc/micromark-extension/tokenize-binding.ts
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types' | ||
import { Codes } from './constants' | ||
|
||
function attempClose (this: TokenizeContext, effects: Effects, ok: State, nok: State) { | ||
return start | ||
|
||
function start (code: Code) { | ||
if (code !== Codes.closingCurlyBracket) { | ||
return nok(code) | ||
} | ||
effects.exit('bindingContent') | ||
effects.enter('bindingFence') | ||
effects.consume(code) | ||
return secondBracket | ||
} | ||
|
||
function secondBracket (code: Code) { | ||
if (code !== Codes.closingCurlyBracket) { | ||
return nok(code) | ||
} | ||
effects.consume(code) | ||
effects.exit('bindingFence') | ||
|
||
return ok | ||
} | ||
} | ||
|
||
function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: State) { | ||
return start | ||
|
||
function start (code: Code): void | State { | ||
if (code !== Codes.openingCurlyBracket) { | ||
throw new Error('expected `{`') | ||
} | ||
|
||
effects.enter('bindingFence') | ||
effects.consume(code) | ||
return secondBracket | ||
} | ||
|
||
function secondBracket (code: Code): void | State { | ||
if (code !== Codes.openingCurlyBracket) { | ||
return nok(code) | ||
} | ||
effects.consume(code) | ||
effects.exit('bindingFence') | ||
effects.enter('bindingContent') | ||
|
||
return content | ||
} | ||
|
||
function content (code: Code): void | State { | ||
if (code === Codes.closingCurlyBracket) { | ||
return effects.attempt({ tokenize: attempClose, partial: true }, close, (code) => { | ||
effects.consume(code) | ||
return content | ||
})(code) | ||
} | ||
|
||
effects.consume(code) | ||
return content | ||
} | ||
|
||
function close (code: Code): void | State { | ||
return ok(code) | ||
} | ||
} | ||
|
||
export default { | ||
tokenize | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import { $fetch } from '@nuxt/test-utils' | ||
|
||
const content = `--- | ||
title: MDC | ||
cover: https://nuxtjs.org/design-kit/colored-logo.svg | ||
--- | ||
:img{:src="cover"} | ||
# {{ $doc.title }} | ||
MDC stands for _**M**ark**D**own **C**omponents_. | ||
This syntax supercharges regular Markdown to write documents interacting deeply with any Vue component from your \`components/content/\` directory or provided by a module. | ||
## Next steps | ||
- [Install Nuxt Content](/get-started) | ||
- [Explore the MDC syntax](/guide/writing/mdc) | ||
You are visiting document: {{ $doc._id }}. | ||
Current route is: {{ $route.path }} | ||
::alert | ||
--- | ||
type: success | ||
--- | ||
This is an alert for {{ type }} | ||
:: | ||
::alert{type="danger"} | ||
This is an alert for {{ type }} | ||
:: | ||
` | ||
|
||
export const testMarkdownRenderer = () => { | ||
describe('renderer:markdown', () => { | ||
test('bindings', async () => { | ||
const rendered = await $fetch('/parse', { | ||
params: { | ||
content | ||
} | ||
}) | ||
|
||
expect(rendered).toContain('<img src="https://nuxtjs.org/design-kit/colored-logo.svg" alt>') | ||
|
||
expect(rendered).toContain('<h1 id><!--[-->MDC<!--]--></h1>') | ||
expect(rendered).toContain('You are visiting document: content:index.md.') | ||
expect(rendered).toContain('Current route is: /parse') | ||
expect(rendered).toContain('This is an alert for success') | ||
expect(rendered).toContain('This is an alert for danger') | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<div> | ||
<MarkdownRenderer :value="data" /> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
const { content } = useRoute().query | ||
const { data } = await useAsyncData(content, async () => { | ||
return await $fetch('/api/parse', { | ||
method: 'POST', | ||
cors: true, | ||
body: { | ||
id: 'content:index.md', | ||
content | ||
} | ||
}) | ||
}) | ||
</script> |