Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(md-enhance): add stylize option (#1895)
Co-authored-by: 史荣久 <trydofor@gmail.com>
- Loading branch information
1 parent
492cc67
commit 57c57dd
Showing
28 changed files
with
1,314 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
--- | ||
title: Stylize | ||
icon: style | ||
--- | ||
|
||
This plugin can stylize inline tokens including changing tags, adding attributes and modifying content. | ||
|
||
It's useful for you to create inline snippet with it. | ||
|
||
<!-- more --> | ||
|
||
## Config | ||
|
||
::: code-tabs#language | ||
|
||
@tab TS | ||
|
||
```ts | ||
// .vuepress/config.ts | ||
import { mdEnhance } from "vuepress-plugin-md-enhance"; | ||
|
||
export default { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
// options here | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
@tab JS | ||
|
||
```js | ||
// .vuepress/config.js | ||
const { mdEnhance } = require("vuepress-plugin-md-enhance"); | ||
|
||
module.exports = { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
// options here | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
The `stylize` receives an array, where each element accepts 2 options: | ||
|
||
- `matcher`: should be `string` or `RegExp`. | ||
|
||
- `replacer`: a function cutomizing the matched token | ||
|
||
For example, you can use the following cofig to transform `*Recommanded*` into a Badge `<Badge type="tip">Recommanded</Badge>`: | ||
|
||
::: code-tabs#language | ||
|
||
@tab TS | ||
|
||
```ts | ||
// .vuepress/config.ts | ||
import { mdEnhance } from "vuepress-plugin-md-enhance"; | ||
|
||
export default { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
{ | ||
match: "Recommanded", | ||
replacer: ({ tag, attrs }) => { | ||
if (tag === "em") | ||
return { | ||
tag: "Badge", | ||
attrs: { type: "tip" }, | ||
content: "Recommanded", | ||
}; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
@tab JS | ||
|
||
```js | ||
// .vuepress/config.js | ||
const { mdEnhance } = require("vuepress-plugin-md-enhance"); | ||
|
||
module.exports = { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
{ | ||
match: "Recommanded", | ||
replacer: ({ tag, attrs }) => { | ||
if (tag === "em") | ||
return { | ||
tag: "Badge", | ||
attrs: { type: "tip" }, | ||
content: "Recommanded", | ||
}; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
::: | ||
|
||
Another example is you want a to set all the emphsis `n't` words to red color, so that `Setting this to a invliad stytax *doesn't* have any effect.` becomes: "Setting this to a invliad stytax <span style="color:red">doesn't</span> have any effect." | ||
|
||
::: code-tabs#language | ||
|
||
@tab TS | ||
|
||
```ts | ||
// .vuepress/config.ts | ||
import { mdEnhance } from "vuepress-plugin-md-enhance"; | ||
|
||
export default { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
{ | ||
match: /n't$/, | ||
replacer: ({ tag, attrs, content }) => { | ||
if (tag === "em") | ||
return { | ||
tag: "span", | ||
attrs: { style: "color: red" }, | ||
content, | ||
}; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
@tab JS | ||
|
||
```js | ||
// .vuepress/config.js | ||
const { mdEnhance } = require("vuepress-plugin-md-enhance"); | ||
|
||
module.exports = { | ||
plugins: [ | ||
mdEnhance({ | ||
stylize: [ | ||
{ | ||
match: /n't$/, | ||
replacer: ({ tag, attrs, content }) => { | ||
if (tag === "em") | ||
return { | ||
tag: "span", | ||
attrs: { style: "color: red" }, | ||
content, | ||
}; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
|
||
::: | ||
|
||
If you want to skip some words in some pages, you can set `noStylize` in page frontmatter with an array containing content you don't want to stylize. | ||
|
||
::: info Performance | ||
|
||
To avoid preformance impact, you should try to avoid using RegExp for better performance unless you need it. | ||
|
||
Also try to create snippets with RegExp having lower costs, e.g: RegExp starting with `^` and ending with `$`. | ||
|
||
For example, if you only want to match "SHOULD", "MUST" and "MAY", you should write `/^(?:SHOULD|M(?:UST|AY))$/u` instead of `/SHOULD|MUST|MAY/u`. The fist one will only match 2 time with "A loo...oong content" with 1000 characters, but will match nearly 3000 times with the second RegExp. | ||
|
||
::: |
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
Oops, something went wrong.