Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply a class to <em>'s containing Japanese characters (#94)
* Add Gatsby plugin and CSS rule to fix emphasis issue with Japanese characters * Fix code style issue * Fix hasJapanese unicode codepoints * Fix typo, comment, function rename
- Loading branch information
Showing
5 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const toString = require('mdast-util-to-string'); | ||
const visit = require('unist-util-visit'); | ||
|
||
const hasJapaneseCharacter = str => { | ||
// Detects katakana, hiragana, iteration marks, and CJK unified ideographs | ||
return /[\u30a0-\u30ff\u3040-\u309f\u3005-\u3006\u4e00-\u9fea。、]/.test(str); | ||
}; | ||
|
||
/** | ||
* Iterates over emphases (<em>'s) within the AST created by remark. | ||
* Applies 'em-ja' class only when it contains a Japanese character, | ||
* so that it can be displayed with a different style. | ||
*/ | ||
|
||
module.exports = ({markdownAST}, options) => { | ||
visit(markdownAST, 'emphasis', node => { | ||
const nodeStr = toString(node); | ||
if (hasJapaneseCharacter(nodeStr)) { | ||
// Patch AST | ||
node.data = node.data || {}; | ||
node.data.hProperties = node.data.hProperties || {}; | ||
node.data.hProperties.class = 'em-ja'; | ||
} | ||
}); | ||
return markdownAST; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "gatsby-remark-japanese-fix", | ||
"version": "0.0.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.em-ja { | ||
font-weight: bolder; | ||
font-style: normal; | ||
} |