Skip to content

Commit 2fe2024

Browse files
committed
WIP
1 parent e00becb commit 2fe2024

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/language-markdown/print/preprocess.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { htmlBlockNames, htmlRawNames } from "micromark-util-html-tag-name";
12
import htmlWhitespace from "../../utilities/html-whitespace.js";
23
import { getOrderedListItemInfo, mapAst, splitText } from "../utilities.js";
34

@@ -25,6 +26,9 @@ function preprocess(ast, options) {
2526
} else {
2627
ast = markAlignedList(ast, options);
2728
}
29+
if (options.parser !== "mdx") {
30+
ast = transformInlineHtml(ast);
31+
}
2832
if (options.parser === "mdx") {
2933
ast = splitTextIntoSentencesLegacy(ast);
3034
} else {
@@ -599,4 +603,59 @@ function markAlignedListLegacy(ast, options) {
599603
}
600604
}
601605

606+
function transformInlineHtml(ast) {
607+
return mapAst(ast, (node) => {
608+
if (!node.children) {
609+
return node;
610+
}
611+
612+
const { children } = node;
613+
for (let i = 0; i < children.length; i++) {
614+
const prev = children[i - 1];
615+
const child = children[i];
616+
if (prev?.type !== "paragraph" || child.type !== "html") {
617+
continue;
618+
}
619+
const tagName = child.value
620+
.match(/^<\/?([a-z0-9-]+)/iu)?.[1]
621+
.toLowerCase();
622+
if (!tagName) {
623+
continue;
624+
}
625+
if (
626+
htmlBlockNames.slice().includes(tagName) ||
627+
htmlRawNames.includes(tagName)
628+
) {
629+
continue;
630+
}
631+
const lineDifference = child.position.start.line - prev.position.end.line;
632+
if (lineDifference > 1) {
633+
continue;
634+
}
635+
if (lineDifference === 1) {
636+
prev.children.push({
637+
type: "text",
638+
value: "\n",
639+
position: {
640+
start: {
641+
line: prev.position.end.line,
642+
column: prev.position.end.column,
643+
offset: prev.position.end.offset,
644+
},
645+
end: {
646+
line: prev.position.end.line + 1,
647+
column: 1,
648+
offset: prev.position.end.offset + 1,
649+
},
650+
},
651+
});
652+
}
653+
prev.children.push(child);
654+
children.splice(i, 1);
655+
i--;
656+
}
657+
return node;
658+
});
659+
}
660+
602661
export default preprocess;

0 commit comments

Comments
 (0)