Skip to content

Commit

Permalink
remark-breaks breaks those badges lines, bad. just make the github al…
Browse files Browse the repository at this point in the history
…ert rewriting simple
  • Loading branch information
Fallen-Breath committed Sep 18, 2024
1 parent 13b2c61 commit 8b4862a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 76 deletions.
28 changes: 0 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions src/components/markdown/gfm-markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -41,7 +40,6 @@ export default function GfmMarkdown({children, className, allowEmbedHtml, allowA

const remarkPlugins: PluggableList = [
remarkGfm,
remarkBreaks,
]
const rehypePlugins: PluggableList = []
if (allowEmbedHtml) {
Expand Down
54 changes: 9 additions & 45 deletions src/components/markdown/rehype-plugin-github-alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 8b4862a

Please sign in to comment.