Skip to content

fix(parser): [[include]] 末尾の余分な ] を value 側に吸収 (greedy close)#49

Merged
r74tech merged 1 commit into
developfrom
fix/include-greedy-trailing-bracket
Jun 10, 2026
Merged

fix(parser): [[include]] 末尾の余分な ] を value 側に吸収 (greedy close)#49
r74tech merged 1 commit into
developfrom
fix/include-greedy-trailing-bracket

Conversation

@r74tech

@r74tech r74tech commented Jun 10, 2026

Copy link
Copy Markdown
Owner

背景

3.1.1 の isRestOfLineBlank] 許容修正で multi-line include 自体は close できるようになったが、close を「最初の ]]」で行う実装になっており、[[include foo\n|key=val|bar=--]]] のようなケースで bar=--、残り ] が include の外に paragraph として漏れ出ていた。

実環境ではテンプレ側で次のような Wikidot コメント wrap idiom を使っており、{$a}--] を代入することで [!-- --] で comment を早期に閉じ、本来表出すべきコンテンツを出す:

[!-- {$a}

VISIBLE

[!----]

3.1.1 だと残り ]<p>]</p> として並ぶだけでなく、--] ではなく -- で value が切れていたためテンプレ側の {$a} = --] イディオムが成立しない。

変更点

scanIncludeDirectives]] を見つけて depth=0 になった時、directive に attribute セクションがある場合のみ 直後の ] を while ループで吸収して close を ]]]+ の末尾に合わせる。

属性ありの判定:

  • | を含む ([[include foo |k=v]])、または
  • page name の後ろに非空白の continuation がある (space-separated parameter、[[include foo k=v]])

= 単独での判定は不可。page name に = が含まれる場合 ([[include foo=bar]]) を誤検出するため。

副次的に 3.1.1 で導入した isRestOfLineBlank] 許容は不要になるので revert。

挙動の差分

source 3.1.0 3.1.1 3.1.2 (本 PR)
[[include tmpl|cap= --]]] 解決失敗 (raw) <<-->>] <<--]>>
[[include tmpl|cap= x]]]]] 解決失敗 <<x>>]]] <<x]]]>>
[[include tmpl]]] 解決成功 + ] 解決成功 + ] 解決成功 + ] (変化なし)
[[include foo=bar]]] 解決成功 + ] 解決成功 + ] 解決成功 + ] (page name の = を誤検出しない)
[[include foo bar=--]]] 解決成功 + ] 解決成功 + ] bar=--]、残り 0

Test plan

  • bun run lint (10 warnings, 0 errors — 既存箇所のみ)
  • bun run format
  • bun run typecheck
  • bun test (1302 pass / 0 fail)
  • 新規ユニットテスト 4 ケース (tests/unit/module/include/resolve.test.ts):
    • 終端 ]] 直後の ] を value に吸収 (multi-line)
    • inline directive + trailing ]
    • plain include で page name が ] を吸収しないこと
    • page name に = を含むケース / space-separated parameter
  • 新規 end-to-end 統合テスト 3 ケース (tests/unit/parser/resolve/include-comment-integration.test.ts):
    • テンプレ + comment-wrap idiom を direct に再現
    • a=--]]] で comment 早閉じ → 中身表出
    • a 未指定で comment swallowing → 中身非表示
    • 複数 attribute multi-line + 最後 --] のバリエーション

3.1.1 で「最後の引数値が ] で終わる multi-line include の終端を見つける」
修正を入れたが、close を「最初の ]]」で行う実装になっており、残りの `]` が
include の外に raw text として漏れ出ていた。

Wikidot 本家は greedy に「最後の ]] で close、間の ] は最終 attribute value に
吸収」を行う ([[include foo|key=val|bar=--]|]] の改行省略形
[[include foo|key=val|bar=--]]] で bar=--] となる)。これを再現するため、
]] で depth=0 になった時点で直後の ] を while ループで読み飛ばし、close 位置を
最後の ]] の終わりに合わせるよう変更。

副次的に 3.1.1 で導入した isRestOfLineBlank の ] 許容は不要になるため revert。
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wdpr-demo-v1 786eab5 Commit Preview URL

Branch Preview URL
Jun 10 2026, 03:30 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
decompiler-preview 786eab5 Commit Preview URL

Branch Preview URL
Jun 10 2026, 03:30 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wdpr-demo-v1-files 786eab5 Commit Preview URL

Branch Preview URL
Jun 10 2026, 03:30 AM

@r74tech
r74tech merged commit f74bcd2 into develop Jun 10, 2026
9 checks passed
@r74tech
r74tech deleted the fix/include-greedy-trailing-bracket branch June 10, 2026 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant