fix: alias svelte and vue to html

This commit is contained in:
EGOIST
2022-05-24 11:15:47 +08:00
parent 51c31e31af
commit b3136143aa
5 changed files with 39 additions and 2 deletions
+1
View File
@@ -50,6 +50,7 @@
"react-hot-toast": "^2.2.0",
"react-query": "^3.39.0",
"react-sortablejs": "^6.1.1",
"refractor": "^4.7.0",
"rehype-prism-plus": "^1.4.0",
"rehype-raw": "^6.1.1",
"rehype-sanitize": "^5.0.1",
+2
View File
@@ -58,6 +58,7 @@ specifiers:
react-hot-toast: ^2.2.0
react-query: ^3.39.0
react-sortablejs: ^6.1.1
refractor: ^4.7.0
rehype-prism-plus: ^1.4.0
rehype-raw: ^6.1.1
rehype-sanitize: ^5.0.1
@@ -114,6 +115,7 @@ dependencies:
react-hot-toast: 2.2.0_ef5jwxihqo6n7gxfmzogljlgcm
react-query: 3.39.0_ef5jwxihqo6n7gxfmzogljlgcm
react-sortablejs: 6.1.1_kjz72dk5lh6hp4ogspefhm7r2u
refractor: 4.7.0
rehype-prism-plus: 1.4.0
rehype-raw: 6.1.1
rehype-sanitize: 5.0.1
+11 -2
View File
@@ -5,11 +5,13 @@ import rehypeStringify from "rehype-stringify"
import remarkGfm from "remark-gfm"
import rehypeSanitize, { defaultSchema } from "rehype-sanitize"
import rehypeRaw from "rehype-raw"
import rehypePrism from "rehype-prism-plus"
import { refractor } from "refractor/lib/common"
import rehypePrismGenerator from "rehype-prism-plus/generator"
import { rehypeImage } from "./rehype-image"
import { remarkExcerpt } from "./remark-excerpt"
import { rehypeTable } from "./rehype-table"
import { allowedBlockquoteAttrs, remarkCallout } from "./remark-callout"
import { rehypeExternalLink } from "./rehyper-external-link"
export type MarkdownEnv = {
excerpt: string
@@ -21,6 +23,10 @@ export type Rendered = {
excerpt: string
}
refractor.alias("html", ["svelte", "vue"])
const rehypePrism = rehypePrismGenerator(refractor)
export const renderPageContent = async (content: string): Promise<Rendered> => {
const env: MarkdownEnv = { excerpt: "", __internal: {} }
@@ -40,9 +46,12 @@ export const renderPageContent = async (content: string): Promise<Rendered> => {
blockquote: allowedBlockquoteAttrs,
},
})
.use(rehypePrism)
.use(rehypePrism, {
ignoreMissing: true,
})
.use(rehypeTable)
.use(rehypeStringify)
.use(rehypeExternalLink)
.process(content)
const contentHTML = result.toString()
+1
View File
@@ -20,6 +20,7 @@ export const rehypeImage: Plugin<Array<void>, Root> = () => {
if (!url.startsWith("https:")) {
throw new Error(`External image url must start with https`)
}
return
}
node.properties.src = getUserContentsUrl(url)
+24
View File
@@ -0,0 +1,24 @@
import { Root } from "rehype-raw"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
export const rehypeExternalLink: Plugin<Array<void>, Root> = () => {
return (tree) => {
visit(tree, { type: "element", tagName: "a" }, (node) => {
if (!node.properties) return
const url = node.properties.href
if (!url || typeof url !== "string") {
return
}
if (isExternLink(url)) {
node.properties.target = "_blank"
node.properties.rel = "noopener noreferrer"
}
})
}
}