From 8b4862a97eb920fbbbab9bf4a8bab6e25f26d3bf Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Thu, 19 Sep 2024 03:10:57 +0800 Subject: [PATCH] remark-breaks breaks those badges lines, bad. just make the github alert rewriting simple --- package-lock.json | 28 ---------- package.json | 1 - src/components/markdown/gfm-markdown.tsx | 2 - .../markdown/rehype-plugin-github-alerts.ts | 54 ++++--------------- 4 files changed, 9 insertions(+), 76 deletions(-) diff --git a/package-lock.json b/package-lock.json index 02fc961..f2a9b85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,6 @@ "rehype-raw": "^7.0.0", "rehype-sanitize": "^6.0.0", "rehype-slug": "^6.0.0", - "remark-breaks": "^4.0.0", "remark-directive": "^3.0.0", "remark-gfm": "^4.0.0", "unist-util-visit": "^5.0.0" @@ -5594,19 +5593,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/mdast-util-newline-to-break": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-newline-to-break/-/mdast-util-newline-to-break-2.0.0.tgz", - "integrity": "sha512-MbgeFca0hLYIEx/2zGsszCSEJJ1JSCdiY5xQxRcLDDGa8EPvlLPupJ4DSajbMPAnC0je8jfb9TiUATnxxrHUog==", - "dependencies": { - "@types/mdast": "^4.0.0", - "mdast-util-find-and-replace": "^3.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/mdast-util-phrasing": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mdast-util-phrasing/-/mdast-util-phrasing-4.0.0.tgz", @@ -7429,20 +7415,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/remark-breaks": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/remark-breaks/-/remark-breaks-4.0.0.tgz", - "integrity": "sha512-IjEjJOkH4FuJvHZVIW0QCDWxcG96kCq7An/KVH2NfJe6rKZU2AsHeB3OEjPNRxi4QC34Xdx7I2KGYn6IpT7gxQ==", - "dependencies": { - "@types/mdast": "^4.0.0", - "mdast-util-newline-to-break": "^2.0.0", - "unified": "^11.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/remark-directive": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/remark-directive/-/remark-directive-3.0.0.tgz", diff --git a/package.json b/package.json index 3afcd2d..56831c8 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "rehype-raw": "^7.0.0", "rehype-sanitize": "^6.0.0", "rehype-slug": "^6.0.0", - "remark-breaks": "^4.0.0", "remark-directive": "^3.0.0", "remark-gfm": "^4.0.0", "unist-util-visit": "^5.0.0" diff --git a/src/components/markdown/gfm-markdown.tsx b/src/components/markdown/gfm-markdown.tsx index 4630c08..a5efe74 100644 --- a/src/components/markdown/gfm-markdown.tsx +++ b/src/components/markdown/gfm-markdown.tsx @@ -3,7 +3,6 @@ import Markdown from "react-markdown"; import rehypeRaw from "rehype-raw"; import rehypeSanitize from "rehype-sanitize"; import rehypeSlug from "rehype-slug"; -import remarkBreaks from "remark-breaks"; import remarkGfm from "remark-gfm"; import { PluggableList } from "unified"; import { AnchorIdSanitizeFixer } from "./anchor-id-sanitize-fixer"; @@ -41,7 +40,6 @@ export default function GfmMarkdown({children, className, allowEmbedHtml, allowA const remarkPlugins: PluggableList = [ remarkGfm, - remarkBreaks, ] const rehypePlugins: PluggableList = [] if (allowEmbedHtml) { diff --git a/src/components/markdown/rehype-plugin-github-alerts.ts b/src/components/markdown/rehype-plugin-github-alerts.ts index 0edad8b..2e20b3d 100644 --- a/src/components/markdown/rehype-plugin-github-alerts.ts +++ b/src/components/markdown/rehype-plugin-github-alerts.ts @@ -45,7 +45,7 @@ export interface IOptions { build?: DefaultBuildType } -// fallen's modification, simple implementation for 1 less dependency +// [fallen's modification] simple implementation for 1 less dependency function isElement(value: any): value is Element { return ( value !== null && @@ -165,51 +165,15 @@ const create = (node: Element, index: number | undefined, parent: Parent | undef const newFirstParagraphChildren: ElementContent[] = [] - if (remainingFirstParagraphChildren.length > 0) { - // if the alert type has a hardline break we remove it - // to not start the alert with a blank line - // meaning we start the slice at 2 to not take - // the br element and new line text nodes - if (remainingFirstParagraphChildren[0].type === 'element' && - remainingFirstParagraphChildren[0].tagName === 'br') { - // fallen's modification start - const remainingChildrenWithoutLineBreak = remainingFirstParagraphChildren.slice(1, firstParagraph.children.length); - if (remainingChildrenWithoutLineBreak.length > 0 && remainingChildrenWithoutLineBreak[0].type === "text") { - if (/^[\r\n]*$/.test(remainingChildrenWithoutLineBreak[0].value)) { - // erase the 2nd child only if it contains \r\n only - remainingFirstParagraphChildren.slice(1, remainingChildrenWithoutLineBreak.length); - } else { - // otherwise, left-trim \r\n for child[0].value - remainingChildrenWithoutLineBreak[0].value = remainingChildrenWithoutLineBreak[0].value.replace(/^[\r\n]+/, "") - } - } - // fallen's modification end - newFirstParagraphChildren.push(...remainingChildrenWithoutLineBreak) - } else { - // if the first line of the blockquote has no hard line break - // after the alert type but some text, then both the type - // and the text will be in a single text node - // headerData rest contains the remaining text without the alert type - if (headerData.rest.trim() !== '') { - const restAsTextNode: Text = { - type: 'text', - value: headerData.rest - } - remainingFirstParagraphChildren.unshift(restAsTextNode) - } - // if no hard line break (br) take all the remaining - // and add them to new paragraph to mimick the initial structure - newFirstParagraphChildren.push(...remainingFirstParagraphChildren) - } - } else { - if (headerData.rest.trim() !== '') { - const restAsTextNode: Text = { - type: 'text', - value: headerData.rest - } - newFirstParagraphChildren.push(restAsTextNode) - } + // [fallen's modification start] make it simple + if (headerData.rest.trim() !== '') { + newFirstParagraphChildren.push({ + type: 'text', + value: headerData.rest.replace(/^[\r\n]+/, ""), + }) } + newFirstParagraphChildren.push(...remainingFirstParagraphChildren) + // [fallen's modification end] if (newFirstParagraphChildren.length > 0) { const lineBreak: Text = {