Skip to content

Instantly share code, notes, and snippets.

@MurakamiShinyu
Last active November 17, 2025 12:12
Show Gist options
  • Select an option

  • Save MurakamiShinyu/da24dbd6c1c2342331aa9dda23de477f to your computer and use it in GitHub Desktop.

Select an option

Save MurakamiShinyu/da24dbd6c1c2342331aa9dda23de477f to your computer and use it in GitHub Desktop.
Markdownで日本語のテキストの括弧囲みを強調するのが難しい件

Markdownで日本語のテキストの括弧囲みを強調するのが難しい件

Markdownの事実上の標準となっているCommonMark仕様には日本語テキストで括弧類で囲まれた語句を強調表示するのが難しいという問題があります。

例えば、以下のようなMarkdownテキストがあったとします。

これは**「強調表示」**の例です。

これは**「強調表示」**の例です。

この ** で囲まれた部分は太字としてレンダリングされることが期待されますが、実際はMarkdownの強調マーカーの ** がそのまま表示されてしまい、強調表示されません。また次の例では

こここは**「強調表示」**なので**「太字」**で表示したいです。

こここは**「強調表示」なので「太字」**で表示したいです。

この例では強調表示したい **「強調表示」****「太字」** の両方が強調表示されず、 あいだの「なので」が強調表示される結果になります。

この問題の原因はCommonMarkの強調表示の仕様にあります。

CommonMark仕様の6.2 Emphasis and strong emphasisに次の規則があります。

5. A double ** can open strong emphasis iff it is part of a left-flanking delimiter run.

7. A double ** can close strong emphasis iff it is part of a right-flanking delimiter run.

そして、これらの用語は次のように定義されています。

First, some definitions. A delimiter run is either a sequence of one or more * characters that is not preceded or followed by a non-backslash-escaped * character, or a sequence of one or more _ characters that is not preceded or followed by a non-backslash-escaped _ character.

left-flanking delimiter run is a delimiter run that is (1) not followed by Unicode whitespace, and either (2a) not followed by a Unicode punctuation character, or (2b) followed by a Unicode punctuation character and preceded by Unicode whitespace or a Unicode punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.

right-flanking delimiter run is a delimiter run that is (1) not preceded by Unicode whitespace, and either (2a) not preceded by a Unicode punctuation character, or (2b) preceded by a Unicode punctuation character and followed by Unicode whitespace or a Unicode punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.

この規則は、スペースで語を区切る欧文などのテキストならば引用符囲みの部分を強調できるように考慮されています。例えば

This is **"strong emphasis"** example.

この場合、最初の ** はleft-flanking delimiter runとなります。left-flanking delimiter runの条件(1)は、** の直後にwhitespaceがないため満たされます。また条件(2b)も、** の直前にwhitespaceがあるため満たされます。また、後ろの ** もright-flanking delimiter runとなります。right-flanking delimiter runの条件(1)は、** の直前にwhitespaceがないため満たされます。また条件(2b)も、** の直後にwhitespaceがあるため満たされます。したがって、強調表示として認識されます。

しかし、日本語テキストの場合、括弧類の直前や直後にwhitespaceが存在しないため、上記の条件を満たさず、強調表示として認識されません。例えば、

これは**「強調表示」**の例です。

この場合、最初の ** はleft-flanking delimiter runの条件(1)は満たされますが、条件(2a)も(2b)も満たされません。また、後ろの ** もright-flanking delimiter runの条件(1)は満たされますが、条件(2a)も(2b)も満たされません。したがって、強調表示として認識されません。

この問題を回避するためには、括弧類の外側にスペースを挿入するか、HTMLの<strong>タグを使用するなどの方法があります。しかし、スペースを挿入した場合は、日本語のテキストの表示で不自然になることがありますし、HTMLタグを使用するとMarkdownの可読性が低下します。困ったものです。

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment