From 5baa81ac42e6a2921380182a6b221f5dfc7e79cc Mon Sep 17 00:00:00 2001 From: zce Date: Sun, 19 Nov 2023 15:19:02 +0800 Subject: [PATCH] feat: remove built-in platten-listitem --- src/plugins/remark-flatten-listitem.ts | 15 --------------- src/plugins/remark-remove-comments.ts | 4 ++-- src/shared/markdown.ts | 9 +-------- 3 files changed, 3 insertions(+), 25 deletions(-) delete mode 100644 src/plugins/remark-flatten-listitem.ts diff --git a/src/plugins/remark-flatten-listitem.ts b/src/plugins/remark-flatten-listitem.ts deleted file mode 100644 index 0488ebf..0000000 --- a/src/plugins/remark-flatten-listitem.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { visit } from 'unist-util-visit' - -import type { Root } from 'mdast' -import type { Plugin } from 'unified' - -const flattenListItem: Plugin<[], Root> = () => tree => { - // https://gitlab.com/staltz/mdast-flatten-listitem-paragraphs/-/blob/master/index.js - visit(tree, 'listItem', node => { - if (node.children.length === 1 && node.children[0].type === 'paragraph') { - node.children = node.children[0].children as any - } - }) -} - -export default flattenListItem diff --git a/src/plugins/remark-remove-comments.ts b/src/plugins/remark-remove-comments.ts index b35d80a..3df38de 100644 --- a/src/plugins/remark-remove-comments.ts +++ b/src/plugins/remark-remove-comments.ts @@ -1,4 +1,4 @@ -import { SKIP, visit } from 'unist-util-visit' +import { visit } from 'unist-util-visit' import type { Root } from 'mdast' import type { Plugin } from 'unified' @@ -8,7 +8,7 @@ const removeComments: Plugin<[], Root> = () => tree => { visit(tree, ['html', 'jsx'], (node: any, index, parent: any) => { if (node.value.match(//g)) { parent.children.splice(index, 1) - return [SKIP, index] // https://unifiedjs.com/learn/recipe/remove-node/ + return ['skip', index] // https://unifiedjs.com/learn/recipe/remove-node/ } }) } diff --git a/src/shared/markdown.ts b/src/shared/markdown.ts index b92f3a9..69e95cb 100644 --- a/src/shared/markdown.ts +++ b/src/shared/markdown.ts @@ -8,7 +8,6 @@ import { z } from 'zod' import rehypeCopyLinkedFiles from '../plugins/rehype-copy-linked-files' import remarkFlattenImage from '../plugins/remark-flatten-image' -import remarkFlattenListItem from '../plugins/remark-flatten-listitem' import remarkRemoveComments from '../plugins/remark-remove-comments' import type { PluggableList } from 'unified' @@ -29,11 +28,6 @@ export interface MarkdownOptions { * @default true */ flattenImage?: boolean - /** - * Flatten list item paragraph. - * @default true - */ - flattenListItem?: boolean /** * Remark plugins. */ @@ -44,13 +38,12 @@ export interface MarkdownOptions { rehypePlugins?: PluggableList } -export const markdown = ({ gfm = true, removeComments = true, flattenImage = true, flattenListItem = true, remarkPlugins, rehypePlugins }: MarkdownOptions = {}) => +export const markdown = ({ gfm = true, removeComments = true, flattenImage = true, remarkPlugins, rehypePlugins }: MarkdownOptions = {}) => z.string().transform(async (value, ctx) => { const file = unified().use(remarkParse) // parse markdown content to a syntax tree if (gfm) file.use(remarkGfm) // support gfm (autolink literals, footnotes, strikethrough, tables, tasklists). if (removeComments) file.use(remarkRemoveComments) // remove html comments if (flattenImage) file.use(remarkFlattenImage) // flatten image paragraph - if (flattenListItem) file.use(remarkFlattenListItem) // flatten list item paragraph if (remarkPlugins != null) file.use(remarkPlugins) // apply remark plugins file.use(remarkRehype, { allowDangerousHtml: true }).use(rehypeRaw) // turn markdown syntax tree to html syntax tree, with raw html support if (rehypePlugins != null) file.use(rehypePlugins) // apply rehype plugins