-
-
Notifications
You must be signed in to change notification settings - Fork 152
Demo file plugin #2142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Demo file plugin #2142
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4531e5
docs: experiment with putting example/demo in separate file
dwgray 934d4f0
docs: Fix typing for markdown-it-class
dwgray b1f62a1
doc: Get an initial demo plugin working
dwgray 874ace0
docs: Use unplugin-vue-components to clean up examples & md files
dwgray ca6769c
docs: Remove stale comment from demo plugin
dwgray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
declare module '@toycode/markdown-it-class' { | ||
import {PluginSimple} from 'markdown-it' | ||
const markdownItClass: PluginSimple | ||
export default markdownItClass | ||
} |
This file contains hidden or 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,83 @@ | ||
import type {MarkdownEnv, MarkdownRenderer} from 'vitepress' | ||
import type {RuleBlock} from 'markdown-it/lib/parser_block.mjs' | ||
import path from 'path' | ||
|
||
// This plugin is inspired by vitepress' snippet plugin and must run before it to work | ||
// It accepts all of the same syntax as the snippet plugin, but will render the demo | ||
// _and_ display the example code inside of a HighlightCard component | ||
|
||
export const demoContainer = (md: MarkdownRenderer, srcDir: string) => { | ||
const blockParser: RuleBlock = (state, startLine, endLine, silent) => { | ||
const sentinal = '<<< DEMO ' | ||
const pos = state.bMarks[startLine] + state.tShift[startLine] | ||
const max = state.eMarks[startLine] | ||
|
||
// if it's indented more than 3 spaces, it should be a code block | ||
if (state.sCount[startLine] - state.blkIndent >= 4) { | ||
return false | ||
} | ||
|
||
if (state.src.slice(pos, pos + sentinal.length) !== sentinal) { | ||
return false | ||
} | ||
|
||
if (silent) { | ||
return true | ||
} | ||
|
||
const start = pos + sentinal.length | ||
const end = state.skipSpacesBack(max, pos) | ||
|
||
const rawPath = state.src.slice(start, end).trim().replace(/^@/, srcDir).trim() | ||
|
||
const {filepath, extension, region, lines, lang, title} = rawPathToToken(rawPath) | ||
const component = title.substring(0, title.indexOf('.')) | ||
|
||
state.line += 1 | ||
|
||
const prefixToken = state.push('html_block', '', 0) | ||
prefixToken.content = `<HighlightCard><${component}/><template #html>` | ||
|
||
const codeToken = state.push('fence', 'code', 0) | ||
codeToken.info = `${lang || extension}${lines ? `{${lines}}` : ''}${title ? `[${title}]` : ''}` | ||
|
||
const {realPath, path: _path} = state.env as MarkdownEnv | ||
const resolvedPath = path.resolve(path.dirname(realPath ?? _path), filepath) | ||
|
||
// @ts-ignore | ||
codeToken.src = [resolvedPath, region.slice(1)] | ||
codeToken.markup = '```' | ||
codeToken.map = [startLine, startLine + 1] | ||
|
||
const postfixToken = state.push('html_block', '', 0) | ||
postfixToken.content = `</template></HighlightCard>` | ||
|
||
return true | ||
} | ||
|
||
md.block.ruler.before('snippet', 'demo', blockParser) | ||
} | ||
|
||
/** | ||
* raw path format: "/path/to/file.extension#region {meta} [title]" | ||
* where #region, {meta} and [title] are optional | ||
* meta can be like '1,2,4-6 lang', 'lang' or '1,2,4-6' | ||
* lang can contain special characters like C++, C#, F#, etc. | ||
* path can be relative to the current file or absolute | ||
* file extension is optional | ||
* path can contain spaces and dots | ||
* | ||
* captures: ['/path/to/file.extension', 'extension', '#region', '{meta}', '[title]'] | ||
*/ | ||
const rawPathRegexp = | ||
/^(.+?(?:(?:\.([a-z0-9]+))?))(?:(#[\w-]+))?(?: ?(?:{(\d+(?:[,-]\d+)*)? ?(\S+)?}))? ?(?:\[(.+)\])?$/ | ||
|
||
function rawPathToToken(rawPath: string) { | ||
const [filepath = '', extension = '', region = '', lines = '', lang = '', rawTitle = ''] = ( | ||
rawPathRegexp.exec(rawPath) || [] | ||
).slice(1) | ||
|
||
const title = rawTitle || filepath.split('/').pop() || '' | ||
|
||
return {filepath, extension, region, lines, lang, title} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
<template> | ||
<BBreadcrumb :items="breadcrumbStringArray" /> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const breadcrumbStringArray = ['Admin', 'Manage', 'Library'] | ||
</script> |
This file contains hidden or 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,15 @@ | ||
<template> | ||
<BBreadcrumb> | ||
<BBreadcrumbItem href="#home"> Home </BBreadcrumbItem> | ||
<BBreadcrumbItem href="#foo">Foo</BBreadcrumbItem> | ||
<BBreadcrumbItem href="#bar" @click="alertEvent">Bar</BBreadcrumbItem> | ||
<BBreadcrumbItem active>Baz</BBreadcrumbItem> | ||
</BBreadcrumb> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const alertEvent = (event: PointerEvent) => { | ||
// eslint-disable-next-line no-alert | ||
alert(`Event ${event.target}`) | ||
} | ||
</script> |
This file contains hidden or 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,14 @@ | ||
<template> | ||
<BBreadcrumb :items="breadcrumbItems" /> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import {type BreadcrumbItem} from 'bootstrap-vue-next' | ||
import {ref} from 'vue' | ||
|
||
const breadcrumbItems = ref<BreadcrumbItem[]>([ | ||
{text: 'Admin', href: 'https://getbootstrap.com/'}, | ||
{text: 'Manage', href: '#'}, | ||
{text: 'Library'}, | ||
]) | ||
</script> |
This file contains hidden or 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,12 @@ | ||
<template> | ||
<!-- #region template --> | ||
<BBreadcrumb> | ||
<BBreadcrumbItem href="#home"> Home </BBreadcrumbItem> | ||
<BBreadcrumbItem href="#foo">Foo</BBreadcrumbItem> | ||
<BBreadcrumbItem href="#bar">Bar</BBreadcrumbItem> | ||
<BBreadcrumbItem active>Baz</BBreadcrumbItem> | ||
<template #prepend><span class="mx-2">prepend text</span></template> | ||
<template #append><span class="mx-2">append text</span></template> | ||
</BBreadcrumb> | ||
<!-- #endregion template --> | ||
</template> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.