-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-md-power): add
<Plot />
and =||=
syntax
- Loading branch information
1 parent
7daf764
commit 9a5eda4
Showing
9 changed files
with
212 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<script setup lang="ts"> | ||
import { computed, ref, shallowRef } from 'vue' | ||
import { onClickOutside, useMediaQuery } from '@vueuse/core' | ||
import { usePageFrontmatter } from 'vuepress/client' | ||
import type { PlotOptions } from '../../shared/plot.js' | ||
import { pluginOptions } from '../options.js' | ||
const props = defineProps<Omit<PlotOptions, 'tag'>>() | ||
const matter = usePageFrontmatter() | ||
const options = computed(() => { | ||
const plot = typeof pluginOptions.plot === 'object' ? pluginOptions.plot : {} | ||
return { | ||
trigger: props.trigger || matter.value.plotTrigger || plot.trigger || 'hover', | ||
color: props.color || plot.color, | ||
mask: props.mask || plot.mask, | ||
} | ||
}) | ||
const styles = computed(() => { | ||
const plot = options.value | ||
if (!plot.color && !plot.mask) | ||
return {} | ||
const style: Record<string, string> = {} | ||
if (plot.color) { | ||
if (typeof plot.color === 'string') { | ||
style['--vp-c-plot-light'] = plot.color | ||
} | ||
else { | ||
style['--vp-c-plot-light'] = plot.color.light | ||
style['--vp-c-plot-dark'] = plot.color.dark | ||
} | ||
} | ||
if (plot.mask) { | ||
if (typeof plot.mask === 'string') { | ||
style['--vp-c-bg-plot-light'] = plot.mask | ||
} | ||
else { | ||
style['--vp-c-bg-plot-light'] = plot.mask.light | ||
style['--vp-c-bg-plot-dark'] = plot.mask.dark | ||
} | ||
} | ||
return style | ||
}) | ||
const isMobile = useMediaQuery('(max-width: 768px)') | ||
const active = ref(false) | ||
const el = shallowRef<HTMLElement>() | ||
onClickOutside(el, () => { | ||
if (options.value.trigger === 'click' || isMobile.value) | ||
active.value = false | ||
}) | ||
function onClick() { | ||
if (props.trigger === 'click' || isMobile.value) | ||
active.value = !active.value | ||
} | ||
</script> | ||
|
||
<template> | ||
<span | ||
ref="el" | ||
class="vp-plot" | ||
:class="{ hover: options.trigger !== 'click', active }" | ||
:style="styles" | ||
@click="onClick" | ||
> | ||
<slot /> | ||
</span> | ||
</template> | ||
|
||
<style> | ||
.vp-plot { | ||
padding-right: 2px; | ||
padding-left: 2px; | ||
color: transparent; | ||
background-color: var(--vp-c-bg-plot-light, #000); | ||
transition: color ease 0.25s, background-color ease 0.25s; | ||
} | ||
.dark .vp-plot { | ||
background-color: var(--vp-c-bg-plot-dark, #fff); | ||
} | ||
.vp-plot.hover:hover, | ||
.vp-plot.active { | ||
color: var(--vp-c-plot-light, #fff); | ||
} | ||
.dark .vp-plot.hover:hover, | ||
.dark .vp-plot.active { | ||
color: var(--vp-c-plot-dark, #000); | ||
} | ||
</style> |
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,73 @@ | ||
/** | ||
* =|这里的文本将被黑幕隐藏,通过点击或者 hover 才可重现|= | ||
*/ | ||
import type { PluginWithOptions } from 'markdown-it' | ||
import type { RuleInline } from 'markdown-it/lib/parser_inline.mjs' | ||
|
||
const [openTag, endTag] = ['=|', '|='] | ||
|
||
function createTokenizer(): RuleInline { | ||
return (state, silent) => { | ||
let found = false | ||
const max = state.posMax | ||
const start = state.pos | ||
|
||
if (state.src.slice(start, start + 2) !== openTag) | ||
return false | ||
|
||
if (silent) | ||
return false | ||
|
||
// =||= | ||
if (max - start < 5) | ||
return false | ||
|
||
state.pos = start + 2 | ||
|
||
while (state.pos < max) { | ||
if (state.src.slice(state.pos - 1, state.pos + 1) === endTag) { | ||
found = true | ||
break | ||
} | ||
|
||
state.md.inline.skipToken(state) | ||
} | ||
|
||
if (!found || start + 2 === state.pos) { | ||
state.pos = start | ||
|
||
return false | ||
} | ||
const content = state.src.slice(start + 2, state.pos - 1) | ||
|
||
// 不允许前后带有空格 | ||
if (/^\s|\s$/.test(content)) { | ||
state.pos = start | ||
return false | ||
} | ||
|
||
// found! | ||
state.posMax = state.pos - 1 | ||
state.pos = start + 2 | ||
|
||
const open = state.push('plot_open', 'Plot', 1) | ||
open.markup = openTag | ||
|
||
const text = state.push('text', '', 0) | ||
text.content = content | ||
|
||
const close = state.push('plot_close', 'Plot', -1) | ||
close.markup = endTag | ||
|
||
state.pos = state.posMax + 2 | ||
state.posMax = max | ||
|
||
return true | ||
} | ||
} | ||
|
||
export const plotPlugin: PluginWithOptions<never> = ( | ||
md, | ||
) => { | ||
md.inline.ruler.before('emphasis', 'plot', createTokenizer()) | ||
} |
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,24 @@ | ||
export interface PlotOptions { | ||
/** | ||
* 是否启用 `=| |=` markdown (该标记为非标准标记,脱离插件将不生效) | ||
* @default true | ||
*/ | ||
tag?: boolean | ||
|
||
/** | ||
* 遮罩层颜色 | ||
*/ | ||
mask?: string | { light: string, dark: string } | ||
|
||
/** | ||
* 文本颜色 | ||
*/ | ||
color?: string | { light: string, dark: string } | ||
|
||
/** | ||
* 触发方式 | ||
* | ||
* @default 'hover' | ||
*/ | ||
trigger?: 'hover' | 'click' | ||
} |
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