Skip to content

Commit e9f6363

Browse files
authored
Merge pull request #4 from md2docx/handle-edge-cases
Handle edge cases
2 parents 5d18c7c + cff9a88 commit e9f6363

File tree

9 files changed

+49
-27
lines changed

9 files changed

+49
-27
lines changed

examples/vite/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"rootDir": ".",
66
"outDir": "dist"
77
},
8-
"include": ["src", "vite.config.ts"]
8+
"include": ["src", "vite.config.mts"]
99
}
File renamed without changes.

lib/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @m2d/html
22

3+
## 1.1.8
4+
5+
### Patch Changes
6+
7+
- 4a811aa: Fix HTML parsing issues in case of empty tags.
8+
39
## 1.1.7
410

511
### Patch Changes

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@m2d/html",
33
"author": "Mayank Kumar Chaudhari (https://mayank-chaudhari.vercel.app)",
44
"private": false,
5-
"version": "1.1.7",
5+
"version": "1.1.8",
66
"description": "Extend MDAST by parsing embedded HTML in Markdown. Converts HTML into structured MDAST nodes compatible with @m2d/core for DOCX generation.",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",

lib/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
BlockContent,
1111
TableRow,
1212
Html,
13+
Literal,
1314
} from "@m2d/core";
1415
import { standardizeColor } from "./utils";
1516
import { AlignmentType, BorderStyle, IBorderOptions } from "docx";
@@ -69,6 +70,8 @@ const INLINE_TAGS = [
6970
"WBR",
7071
] as const;
7172

73+
const EMPTY_TAGS = ["br", "hr", "img", "input"];
74+
7275
/**
7376
* Mapping of DOM tag names to MDAST node types.
7477
*/
@@ -499,8 +502,8 @@ const preprocess = (pNode: Parent, isRoot = true) => {
499502
for (const node of pNode.children) {
500503
if ((node as Parent).children?.length) preprocess(node as Parent, false);
501504
// match only inline non-self-closing html nodes.
502-
if (node.type === "html" && /^<[^>]*[^/]>$/.test(node.value)) {
503-
const tag = node.value.split(" ")[0].replace(/^<|>$/g, "");
505+
const tag = (node as Literal).value?.split(" ")[0].replace(/^<|\/?>$/g, "");
506+
if (node.type === "html" && !EMPTY_TAGS.includes(tag) && /^<[^>]*[^/]>$/.test(node.value)) {
504507
// ending tag
505508
if (tag[0] === "/") {
506509
const hNode = htmlNodeStack.shift();
@@ -516,7 +519,9 @@ const preprocess = (pNode: Parent, isRoot = true) => {
516519
children.push(node);
517520
}
518521

519-
const isSelfClosingTag = node.type === "html" && /^<[^>]*\/>$/.test(node.value);
522+
const isSelfClosingTag =
523+
node.type === "html" && (EMPTY_TAGS.includes(tag) || /^<[^>]*\/>$/.test(node.value));
524+
520525
// self closing tags
521526
if (isSelfClosingTag && !isRoot) {
522527
// @ts-expect-error -- ok

packages/shared/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @repo/shared
22

3+
## 0.0.14
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [4a811aa]
8+
- @m2d/html@1.1.8
9+
310
## 0.0.13
411

512
### Patch Changes

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@repo/shared",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"private": true,
55
"sideEffects": false,
66
"main": "./dist/index.js",

pnpm-lock.yaml

Lines changed: 18 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
## 13. HTML Elements
22

3-
Using HTML for additional interactivity.
3+
### Table with html
4+
5+
| Left-aligned | Center-aligned | Right-aligned |
6+
| :----------------------- | :------------: | ------------: |
7+
| Text 1 <br>- second line | Text 2 | Text 3 |
8+
9+
### Using HTML for additional interactivity.
410

511
<details>
612
<summary>Click to expand</summary>

0 commit comments

Comments
 (0)