在客户端将 wiki 文本(维基词典风格)翻译成 HTML
我正在尝试在客户端解析维基词典维基文本(使用JavaScript)。我找到了 Wiky.js,但它的某些标记存在问题,例如 {{}}、+ 等。你知道有什么 JavaScript 库可以帮助我吗?我发现,MediaWiki API 可能将 wikitext 转换为 HTML。但我使用 查询操作 从 API 获取数据,因此这是虚拟的请求服务器两次。也许有某种方法可以使用查询操作获取 HTML 而不是 Wikitext?我还找到了 渲染操作,但它向我发送了整个页面,而不是只是一篇文章。
//编辑
这是示例 Wikitext 的一部分:
=====Translations===== {{trans-top|on fingers and toes}} * [[Afrikaans]]: [[nael]] * Albanian: [[thua]] {{f}} * Arabic: {{Arab|[[ظفر]]}} (ẓufr) * Armenian: {{t-|hy|եղունգ|tr=eġung}} *: Old Armenian: {{tø|xcl|եղունգն|tr=ełungn|sc=Armn|xs=Old Armenian}} * [[Azeri]]: {{t+|az|dırnaq|xs=Azeri}} * Bosnian: {{t-|bs|nokat|m}} * [[Breton]]: [[ivin]] {{m}}, ivinoù {{p}} * [[Campidanese Sardinian]]: [[unga]] {{f}} * [[Catalan]]: [[ungla]] {{f}} * Chinese: {{zh-zh-p|指甲|zhǐjia}} * Croatian: {{t+|hr|nokat|m|alt=nȍkat}} * Czech: {{t+|cs|nehet|m}} * Danish: {{t+|da|negl}} * Dutch: {{t+|nl|nagel|m}} * [[Erzya]]: [[кенже]] (kenzhe) * Esperanto: {{t-|eo|ungo|xs=Esperanto}} * Estonian: [[küüs]] * Finnish: {{t+|fi|kynsi}} * French: {{t+|fr|ongle|m}} * [[Galician]]: [[unlla]] {{f}}, [[uña]] {{f}} * Georgian: {{t-|ka|ფრჩხილი|tr=p'rč'xili|sc=Geor|xs=Georgian}} * German: {{t+|de|Nagel|m}} * Greek: *: Anciemt: {{tø|grc|ὄνυξ|m|tr=onyx|xs=Ancient Greek}} *: Modern: {{t+|el|νύχι|n|tr=nýchi}} * [[Gujarati]]: [[નખ]] (nakh) {{m}} * Hindi: {{t-|hi|नाख़ुन|m|tr=nāḵẖun|xs=Hindi}}
和 Wikiy.toHtml() 输出:
<h4>Translations</h4>
<p u"="" style="{trans-top</p></td>?(c_u) <li class=">Arabic: {{t-</p>
</li>
arصرعm?(c_u)
<li class="u">Bengali;">}, {{t-bspadavica?(c_u) </li>
<li class="u">Chinese: *: Mandarin: {{t</li>
cmn癲癇sc=Hani}}, {{tcmn癫痫tr=diānxiánsc=Hani}}, {{tcmn癲癇癥sc=Hani}}, {{tcmn癫痫症tr=diānxiánzhèng?(c_u)
<li class="u">Croatian: {{t-</li>
hrepilepsijafalt=epilèpsija}}, {{t-hrpadavicaf?(c_u)
<li class="u">Czech: {{t-</li>
csepilepsie?(c_u)
<li class="u">Estonian: {{t+</li>
etepilepsia}}, {{t+et?(c_u)
<li class="u">Finnish: {{t+</li>
fi?(c_u)
<li class="u">French: {{t+</li>
frépilepsie?(c_u)
<li class="u">German: {{t+</li>
deEpilepsief}}, {{t-deFallsucht?(c_u)
<li class="u">Greek: {{t+</li>
elεπιληψία?(c_u)
<li class="u">Hindi: {{t-</li>
hiअपस्मारtr=apasmārxs=Hindi}}, {{thiमिर्गीtr=mirgī?(c_u)
I'm trying to parse Wiktionary Wikitext on the client side (with JavaScript). I found Wiky.js, but it has problems with some markups like {{}}, + etc. Do you know any JavaScript library which may help me with that? I found, that MediaWiki API may translate wikitext into HTML. but I get data from API using the query action, so it would be dummy to request server twice. Maybe there is some way to get HTML instead of Wikitext using the query action? I also found render action, but it sends me the entire page, not only an article.
//edit
Here is part of sample Wikitext:
=====Translations===== {{trans-top|on fingers and toes}} * [[Afrikaans]]: [[nael]] * Albanian: [[thua]] {{f}} * Arabic: {{Arab|[[ظفر]]}} (ẓufr) * Armenian: {{t-|hy|եղունգ|tr=eġung}} *: Old Armenian: {{tø|xcl|եղունգն|tr=ełungn|sc=Armn|xs=Old Armenian}} * [[Azeri]]: {{t+|az|dırnaq|xs=Azeri}} * Bosnian: {{t-|bs|nokat|m}} * [[Breton]]: [[ivin]] {{m}}, ivinoù {{p}} * [[Campidanese Sardinian]]: [[unga]] {{f}} * [[Catalan]]: [[ungla]] {{f}} * Chinese: {{zh-zh-p|指甲|zhǐjia}} * Croatian: {{t+|hr|nokat|m|alt=nȍkat}} * Czech: {{t+|cs|nehet|m}} * Danish: {{t+|da|negl}} * Dutch: {{t+|nl|nagel|m}} * [[Erzya]]: [[кенже]] (kenzhe) * Esperanto: {{t-|eo|ungo|xs=Esperanto}} * Estonian: [[küüs]] * Finnish: {{t+|fi|kynsi}} * French: {{t+|fr|ongle|m}} * [[Galician]]: [[unlla]] {{f}}, [[uña]] {{f}} * Georgian: {{t-|ka|ფრჩხილი|tr=p'rč'xili|sc=Geor|xs=Georgian}} * German: {{t+|de|Nagel|m}} * Greek: *: Anciemt: {{tø|grc|ὄνυξ|m|tr=onyx|xs=Ancient Greek}} *: Modern: {{t+|el|νύχι|n|tr=nýchi}} * [[Gujarati]]: [[નખ]] (nakh) {{m}} * Hindi: {{t-|hi|नाख़ुन|m|tr=nāḵẖun|xs=Hindi}}
and Wiky.toHtml() output:
<h4>Translations</h4>
<p u"="" style="{trans-top</p></td>?(c_u) <li class=">Arabic: {{t-</p>
</li>
arصرعm?(c_u)
<li class="u">Bengali;">}, {{t-bspadavica?(c_u) </li>
<li class="u">Chinese: *: Mandarin: {{t</li>
cmn癲癇sc=Hani}}, {{tcmn癫痫tr=diānxiánsc=Hani}}, {{tcmn癲癇癥sc=Hani}}, {{tcmn癫痫症tr=diānxiánzhèng?(c_u)
<li class="u">Croatian: {{t-</li>
hrepilepsijafalt=epilèpsija}}, {{t-hrpadavicaf?(c_u)
<li class="u">Czech: {{t-</li>
csepilepsie?(c_u)
<li class="u">Estonian: {{t+</li>
etepilepsia}}, {{t+et?(c_u)
<li class="u">Finnish: {{t+</li>
fi?(c_u)
<li class="u">French: {{t+</li>
frépilepsie?(c_u)
<li class="u">German: {{t+</li>
deEpilepsief}}, {{t-deFallsucht?(c_u)
<li class="u">Greek: {{t+</li>
elεπιληψία?(c_u)
<li class="u">Hindi: {{t-</li>
hiअपस्मारtr=apasmārxs=Hindi}}, {{thiमिर्गीtr=mirgī?(c_u)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Wikitext 有非常复杂的边缘情况,你不能指望 javascript 库能够可靠地解析它(尽管它应该可以比 Wikiky 做得更好)。最好的方法是使用 action=render,然后从响应中删除相关部分(我不确定您所说的整个页面与文章是什么意思)。
Wikitext has very complicated edge cases, you cannot expect a javascript library to reliably parse it (though it should be possible to do a much better job than Wiky does). The best is to use action=render, then strip the relevant part from the response (I'm not sure what you mean by entire page vs. article).