feat: built-in callout (#1016)

* feat: built-in callout

* update callout padding

* lock dep version
This commit is contained in:
Stephen Zhou
2023-10-06 12:14:28 +08:00
committed by GitHub
parent e011c8924a
commit 9ade4e847c
6 changed files with 119 additions and 62 deletions
+4 -2
View File
@@ -29,6 +29,9 @@ import remarkParse from "remark-parse"
import remarkRehype from "remark-rehype"
import { unified } from "unified"
// @ts-expect-error
import remarkCalloutDirectives from "@microflash/remark-callout-directives"
import AdvancedImage from "~/components/ui/AdvancedImage"
import { transformers } from "./embed-transformers"
@@ -44,7 +47,6 @@ import { rehypeTable } from "./rehype-table"
import { rehypeVideo } from "./rehype-video"
import { rehypeWrapCode } from "./rehype-wrap-code"
import { rehypeExternalLink } from "./rehyper-external-link"
import { remarkCallout } from "./remark-callout"
import { remarkMermaid } from "./remark-mermaid"
import { remarkPangu } from "./remark-pangu"
import { remarkYoutube } from "./remark-youtube"
@@ -121,9 +123,9 @@ export const renderPageContent = (
.use(remarkGfm, {
singleTilde: false,
})
.use(remarkCallout)
.use(remarkDirective)
.use(remarkDirectiveRehype)
.use(remarkCalloutDirectives)
.use(remarkYoutube)
.use(remarkMermaid)
.use(remarkMath, {
-50
View File
@@ -1,50 +0,0 @@
import type { Root } from "mdast"
import type { Plugin } from "unified"
import { visit } from "unist-util-visit"
const CALLOUT_RE = /^(TIP|WARN|SUCCESS|DANGER):(\n|$)/
export const remarkCallout: Plugin<[], Root> = () => (tree: Root) => {
visit(tree, { type: "blockquote" }, (node, index, parent) => {
if (node.type !== "blockquote") return
const firstChild = node.children[0]
if (firstChild?.type === "paragraph") {
const paragraphFirstChild = firstChild.children[0]
if (
paragraphFirstChild.type === "text" &&
CALLOUT_RE.test(paragraphFirstChild.value)
) {
const type = paragraphFirstChild.value.match(CALLOUT_RE)![1]
const newValue = paragraphFirstChild.value.replace(CALLOUT_RE, "")
// In the following case `newValue` will be empty empty
// We should remove the paragraph
// > TIP:
// >
// > some tip
if (!newValue) {
node.children.splice(0, 1)
}
paragraphFirstChild.value = newValue
const className = `callout callout-${type.toLowerCase()}`
node.data = {
...node.data,
hProperties: {
class: className,
},
}
}
}
})
}
export const allowedBlockquoteAttrs: Array<[string, ...string[]]> = [
[
"className",
"callout",
"callout-tip",
"callout-warn",
"callout-success",
"callout-danger",
],
]
+15 -3
View File
@@ -1,7 +1,5 @@
import { defaultSchema } from "rehype-sanitize"
import { allowedBlockquoteAttrs } from "./remark-callout"
const scheme = {
...defaultSchema,
tagNames: [
@@ -16,11 +14,12 @@ const scheme = {
"audio",
"source",
"mermaid",
"aside",
...["svg", "path", "circle"],
],
attributes: {
...defaultSchema.attributes,
"*": [...(defaultSchema.attributes?.["*"] || []), "className", "style"],
blockquote: allowedBlockquoteAttrs,
video: ["src", "controls", "loop", "muted", "autoPlay", "playsInline"],
audio: [
"src",
@@ -35,6 +34,19 @@ const scheme = {
],
source: ["src", "type"],
iframe: ["src", "allowFullScreen", "frameborder", "allow"],
svg: [
"xmlns",
"width",
"height",
"viewBox",
"fill",
"stroke",
"strokeLineCap",
"strokeLineJoin",
"strokeWidth",
],
path: ["d", "fill"],
circle: ["cx", "cy", "r", "fill"],
},
}