MyST-Parserが&をエスケープしすぎる問題
ある日のSlackより
takanory
myst-parser で URL に & が入っているとそこがエスケープされるっぽい?困った
markdown-it-py ではそういう変換はされてないっぽいなぁ
code:md
これが
code:html
<ul class="simple">
</ul>
こうなっちゃう
shimizukawa
二重に変換されてるっぽい?
linkifyを有効にしてたら試しに切ってみるとか
takanory
linkifyはいれてないです。
新環境試しに作ってみたけど同じだった
shimizukawa
なるほど
takanory
あとでバグレポートしてみるかなぁ。どこが原因かまで調べたかったんだがsphinx力が足りない
shimizukawa
code:md
とりあえずこれで逃げられるだろうか
takanory
だめどした
code:html
shimizukawa
なるほどー。markdown-it-py の変換後に myst-parserがURLもエスケープしちゃってるんだろうな
このissueかな
#760 Unable to link with multiple query params Describe the bug
context
expectation
I expected that the rendered link would contain all of the query params.
bug
But instead the & becomes encoded as & and breaks the link. I thought I'd be able to escape the & or that it would work natively, but neither seem to be true.
takanory
issueあるのか
Update: I ended up manually using sed to find and remove the amp;.
悲しみ...
私もとりあえずそれやるかー...
shimizukawa
markdown-it-py parserで生成されるtokenでは、 & のまま。
code:python
Token(type='inline',
tag='',
nesting=0,
attrs={},
level=1,
children=[Token(type='link_open',
tag='a',
nesting=1,
map=None,
level=0,
children=None,
content='',
markup='',
info='',
meta={},
block=False,
hidden=False),
link記法をdocutils nodeに変換するときに1回目のescapeをしている
code:python
/workspaces/MyST-Parser/myst_parser/mdit_to_docutils/base.py(1005)render_link_url()
(Pdb) uri
そしてHTML出力では2回escapeされている
code:html
上記のescapeHtmlをやめてみると、1回エスケープになる
code:html
これはsphinx or docutilsでもescapeしているのだろうとアタリを付けて、試してみる
code:console
やっぱりescapeされてた。
docutilsを追ってみて、escapeしている箇所は以下だった
code:python
/usr/local/python/3.10.13/lib/python3.10/site-packages/docutils/writers/_html_base.py(1446)visit_reference()->None
-> self.body.append(self.starttag(node, 'a', '', **atts))
(Pdb) atts
(Pdb) self.starttag(node,'a','',**atts)
実際のエスケープはここ
code:python
このコードは8年間変わってなさそう?
なので、myst-parserのコードから単純に escapeHtml を消せば良さそう
code:diff
@shimizukawa ➜ /workspaces/MyST-Parser (master) $ git diff
diff --git a/myst_parser/mdit_to_docutils/base.py b/myst_parser/mdit_to_docutils/base.py
index 4a02459..cee67c9 100644
--- a/myst_parser/mdit_to_docutils/base.py
+++ b/myst_parser/mdit_to_docutils/base.py
@@ -996,7 +996,7 @@ class DocutilsRenderer(RendererProtocol):
if "classes" in conversion:
if implicit_text is not None:
with self.current_node_context(ref_node, append=True):
takanory
おーーー