Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

FEATURE: add support for multilingual mathjax #84

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions assets/javascripts/lib/discourse-markdown/discourse-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +42,10 @@ function isSafeBoundary(character_code, delimiter_code, md) {
return true;
}

if (additionalPunctuation.includes(character_code)) {
return true;
}

return false;
}

Expand Down
24 changes: 24 additions & 0 deletions spec/pretty_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@
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 = '<p>这是一个测试,<span class="math">a^2 + b^2 = c^2</span>,这是另一个测试。</p>'
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 = '<p>これはテストです、<span class="math">a^2 + b^2 = c^2</span>、これもテストです。</p>'
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 = '<p>هذا اختبار،<span class="math">a^2 + b^2 = c^2</span>،هذا اختبار آخر.</p>'
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 = "<div class=\"math\">\na^2 + b^2 = c^2\n</div>"
expect(cooked.strip).to eq(html.strip)
end

it "can handle inline math" do
cooked = PrettyText.cook <<~MD
I like
Expand Down