Last active
February 14, 2023 18:13
-
-
Save manabuyasuda/49f857be90607cbb8fae361bd2e70d23 to your computer and use it in GitHub Desktop.
Googleの「budoux」で文字列に適切な改行を挿入します。
This file contains hidden or 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
| import { loadDefaultJapaneseParser } from 'budoux' | |
| const parser = loadDefaultJapaneseParser() | |
| /** | |
| * 文字列を解析して、適切に改行されるように`wbr`タグを挿入します。 | |
| * IEは`wbr`タグに対応していません。 | |
| * @see https://caniuse.com/wbr-element | |
| * @see https://github.com/google/budoux/tree/main/javascript | |
| * @param {HTMLElement} element 最適化する要素 | |
| * @return {void} | |
| * @example | |
| * const items = document.getElementsByClassName('js-budou') | |
| * Array.from(items).forEach(item => adjustLineBreaks(item)) | |
| */ | |
| const adjustLineBreaks = element => { | |
| const target = element | |
| parser.applyElement(target) | |
| } | |
| export { adjustLineBreaks } |
This file contains hidden or 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
| import { adjustLineBreaks } from '../../utils/adjustLineBreaks' | |
| /** | |
| * 文字列を解析して、適切に改行されるように要素を挿入します。 | |
| * 指定した要素の子孫要素すべてに適用されるので注意してください。 | |
| * @example | |
| * <span class="js-budou">Googleの使命は、世界中の情報を<strong>整理</strong>し、<em>世界中の人がアクセス</em>できて使えるようにすることです。</span> | |
| * | |
| * <span class="js-budou" style="word-break: keep-all; overflow-wrap: break-word;">Googleの<wbr>使命は、<wbr>世界中の<wbr>情報を<wbr><strong>整理</strong>し、<wbr><em>世界中の<wbr>人が<wbr>アクセス</em>できて<wbr>使えるように<wbr>する<wbr>ことです。</span> | |
| */ | |
| export default function jsBudou() { | |
| const items = document.getElementsByClassName('js-budou') | |
| Array.from(items).forEach(item => adjustLineBreaks(item)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment