From 718c0ce770c1339415b5a06a749befbe1bb4aff0 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 11 Jun 2024 18:02:45 +1000 Subject: [PATCH] FEATURE: add support for multilingual mathjax (#84) Chinese / Korean / Japanese / Thai and Arabic use different punctuations. --- .../lib/discourse-markdown/discourse-math.js | 26 +++++++++++++++++++ spec/pretty_text_spec.rb | 24 +++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/assets/javascripts/lib/discourse-markdown/discourse-math.js b/assets/javascripts/lib/discourse-markdown/discourse-math.js index 395267b..c6871e6 100644 --- a/assets/javascripts/lib/discourse-markdown/discourse-math.js +++ b/assets/javascripts/lib/discourse-markdown/discourse-math.js @@ -3,6 +3,28 @@ // // +const additionalPunctuation = [ + // Chinese and Japanese punctuation + 0x3001, // 、 + 0x3002, // 。 + + // Full-width punctuation used in East Asian languages + 0xff0c, // , + 0xff1a, // : + 0xff1b, // ; + 0xff0e, // . + 0xff1f, // ? + 0xff01, // ! + + // Arabic punctuation + 0x060c, // ، + 0x061b, // ؛ + 0x061f, // ؟ + + // Thai punctuation + 0x0e2f, // ฯ +]; + function isSafeBoundary(character_code, delimiter_code, md) { if (character_code === delimiter_code) { return false; @@ -20,6 +42,10 @@ function isSafeBoundary(character_code, delimiter_code, md) { return true; } + if (additionalPunctuation.includes(character_code)) { + return true; + } + return false; } diff --git a/spec/pretty_text_spec.rb b/spec/pretty_text_spec.rb index dee33f9..9c37a4c 100644 --- a/spec/pretty_text_spec.rb +++ b/spec/pretty_text_spec.rb @@ -38,6 +38,30 @@ describe PrettyText do expect(PrettyText.cook "($ +500 $)").to include("math") end + it "can handle inline math with Chinese punctuation" do + cooked = PrettyText.cook("这是一个测试,$a^2 + b^2 = c^2$,这是另一个测试。") + html = '

这是一个测试,a^2 + b^2 = c^2,这是另一个测试。

' + expect(cooked).to eq(html) + end + + it "can handle inline math with Japanese punctuation" do + cooked = PrettyText.cook("これはテストです、$a^2 + b^2 = c^2$、これもテストです。") + html = '

これはテストです、a^2 + b^2 = c^2、これもテストです。

' + expect(cooked).to eq(html) + end + + it "can handle inline math with Arabic punctuation" do + cooked = PrettyText.cook("هذا اختبار،$a^2 + b^2 = c^2$،هذا اختبار آخر.") + html = '

هذا اختبار،a^2 + b^2 = c^2،هذا اختبار آخر.

' + expect(cooked).to eq(html) + end + + it "can handle block math with Chinese punctuation" do + cooked = PrettyText.cook("$$\na^2 + b^2 = c^2\n$$") + html = "
\na^2 + b^2 = c^2\n
" + expect(cooked.strip).to eq(html.strip) + end + it "can handle inline math" do cooked = PrettyText.cook <<~MD I like