-
Notifications
You must be signed in to change notification settings - Fork 866
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add <wbr> tags to word boundaries
- Loading branch information
Showing
12 changed files
with
7,203 additions
and
2,457 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { | ||
tsconfig: { | ||
allowJs: true | ||
} | ||
}] | ||
}, | ||
transformIgnorePatterns: [ | ||
'<rootDir>/node_modules/(?!lit-html)' | ||
] | ||
} |
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,11 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
import { breakWord } from './helper' | ||
|
||
test('break-text', () => { | ||
expect(breakWord('Other APIs')).toEqual(['Other APIs']) | ||
expect(breakWord('System.CodeDom')).toEqual(['System.', 'Code', 'Dom']) | ||
expect(breakWord('System.Collections.Dictionary<string, object>')).toEqual(['System.', 'Collections.', 'Dictionary<', 'string, object>']) | ||
expect(breakWord('https://github.com/dotnet/docfx')).toEqual(['https://github.', 'com/', 'dotnet/', 'docfx']) | ||
}) |
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 |
---|---|---|
@@ -1,29 +1,47 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
import { html, TemplateResult } from 'lit-html' | ||
|
||
/** | ||
* Get the value of an HTML meta tag. | ||
*/ | ||
export function meta(name: string): string { | ||
return (document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement)?.content | ||
} | ||
|
||
/** | ||
* Add <wbr> into long word. | ||
* @param {String} text - The word to break. It should be in plain text without HTML tags. | ||
*/ | ||
function breakPlainText(text) { | ||
if (!text) return text | ||
return text.replace(/([a-z])([A-Z])|(\.)(\w)/g, '$1$3<wbr>$2$4') | ||
export function breakWord(text: string): string[] { | ||
const regex = /([a-z0-9])([A-Z]+[a-z])|([a-zA-Z0-9][./<>\-_])/g | ||
const result = [] | ||
let start = 0 | ||
while (true) { | ||
const match = regex.exec(text) | ||
if (!match) { | ||
break | ||
} | ||
const index = match.index + (match[1] || match[3]).length | ||
result.push(text.slice(start, index)) | ||
start = index | ||
} | ||
if (start < text.length) { | ||
result.push(text.slice(start)) | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* Add <wbr> into long word. | ||
*/ | ||
function breakWord(e: Element) { | ||
if (!e.innerHTML.match(/(<\w*)((\s\/>)|(.*<\/\w*>))/g)) { | ||
e.innerHTML = breakPlainText(e.innerHTML) | ||
} | ||
} | ||
|
||
export function breakText() { | ||
document.querySelectorAll('.xref').forEach(e => e.classList.add('text-break')) | ||
document.querySelectorAll('.text-break').forEach(e => breakWord(e)) | ||
export function breakWordLit(text: string): TemplateResult { | ||
const result = [] | ||
breakWord(text).forEach(word => { | ||
if (result.length > 0) { | ||
result.push(html`<wbr>`) | ||
} | ||
result.push(html`${word}`) | ||
}) | ||
return html`${result}` | ||
} |
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
Oops, something went wrong.