|
| 1 | +import { htmlBlockNames, htmlRawNames } from "micromark-util-html-tag-name"; |
1 | 2 | import htmlWhitespace from "../../utilities/html-whitespace.js"; |
2 | 3 | import { getOrderedListItemInfo, mapAst, splitText } from "../utilities.js"; |
3 | 4 |
|
@@ -25,6 +26,9 @@ function preprocess(ast, options) { |
25 | 26 | } else { |
26 | 27 | ast = markAlignedList(ast, options); |
27 | 28 | } |
| 29 | + if (options.parser !== "mdx") { |
| 30 | + ast = transformInlineHtml(ast); |
| 31 | + } |
28 | 32 | if (options.parser === "mdx") { |
29 | 33 | ast = splitTextIntoSentencesLegacy(ast); |
30 | 34 | } else { |
@@ -599,4 +603,59 @@ function markAlignedListLegacy(ast, options) { |
599 | 603 | } |
600 | 604 | } |
601 | 605 |
|
| 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 | + |
602 | 661 | export default preprocess; |
0 commit comments