Skip to content

Commit 909baa1

Browse files
committed
fix: definition-list直前のテキストをparagraphに包まない
Wikidotの挙動に合わせ、definition-list直前のテキスト行を paragraphコンテナに包まず裸テキスト+line-breakとして出力。 normalizeHtmlのタグ前後改行処理も改善。
1 parent cd4d3c0 commit 909baa1

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

packages/parser/src/parser/rules/block/paragraph.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ export const paragraphRule: BlockRule = {
100100
return { success: false };
101101
}
102102

103+
// Wikidot: text lines immediately before a definition list are not
104+
// wrapped in <p>. Check if next token starts a definition list.
105+
const nextPos = ctx.pos + result.consumed;
106+
const nextToken = ctx.tokens[nextPos];
107+
if (nextToken?.type === "COLON" && nextToken.lineStart) {
108+
return {
109+
success: true,
110+
elements: [...elements, { element: "line-break" }],
111+
consumed: result.consumed,
112+
};
113+
}
114+
103115
return {
104116
success: true,
105117
elements: [

tests/fixtures/definition-list/basic/expected.json

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
{
22
"elements": [
33
{
4-
"element": "container",
5-
"data": {
6-
"type": "paragraph",
7-
"attributes": {},
8-
"elements": [
9-
{
10-
"element": "text",
11-
"data": "ALPHA"
12-
}
13-
]
14-
}
4+
"element": "text",
5+
"data": "ALPHA"
6+
},
7+
{
8+
"element": "line-break"
159
},
1610
{
1711
"element": "definition-list",
@@ -75,17 +69,11 @@
7569
]
7670
},
7771
{
78-
"element": "container",
79-
"data": {
80-
"type": "paragraph",
81-
"attributes": {},
82-
"elements": [
83-
{
84-
"element": "text",
85-
"data": "BETA"
86-
}
87-
]
88-
}
72+
"element": "text",
73+
"data": "BETA"
74+
},
75+
{
76+
"element": "line-break"
8977
},
9078
{
9179
"element": "definition-list",

tests/integration/fixture-render.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ function normalizeHtml(html: string): string {
7474
.replace(/\r\n/g, "\n")
7575
// onclick属性を削除(Wikidot固有のJS)
7676
.replace(/ onclick="[^"]*"/g, "")
77-
// 連続する空白・改行を単一スペースに(HTML的に等価)
78-
.replace(/\s+/g, " ")
77+
// タグ前後の改行を削除(ブロック要素の前後の改行はHTML的に無意味)
78+
.replace(/\n\s*</g, "<")
79+
.replace(/>\s*\n/g, ">")
80+
// 残りの連続空白を単一スペースに(HTML的に等価)
81+
.replace(/[ \t]+/g, " ")
7982
// 属性順序を正規化(HTML的に等価)
8083
.replace(
8184
/<(\w+)((?:\s+[a-zA-Z_][\w-]*(?:="[^"]*")?)+)\s*(\/?)>/g,

0 commit comments

Comments
 (0)