-
Notifications
You must be signed in to change notification settings - Fork 2
/
guessUnicodePunctuation.js
42 lines (36 loc) · 1.9 KB
/
guessUnicodePunctuation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { transformInputValues } from '@kellnerd/es-utils/dom/transformInputValues.js';
import {
punctuationRules,
punctuationRulesForLanguage,
} from '@kellnerd/es-utils/string/punctuation.js';
/**
* Preserves apostrophe-based markup and URLs (which are supported by annotations and edit notes)
* by temporarily changing them to characters that will not be touched by the transformation rules.
* After the punctuation guessing transformation rules were applied, URLs and markup are restored.
* @type {import('@kellnerd/es-utils').SubstitutionRule[]}
*/
export const transformationRulesToPreserveMarkup = [
/* Base64 encode URLs */
[/\[(.+?)(\|.+?)?\]/g, (_match, url, label = '') => `[${btoa(url)}${label}]`], // labeled link
[/(?<=\/\/)(\S+)/g, (_match, path) => btoa(path)], // plain text URLs
[/'''/g, '<b>'], // bold text
[/''/g, '<i>'], // italic text
...punctuationRules,
[/<b>/g, "'''"],
[/<i>/g, "''"],
/* decode Base64 URLs */
[/(?<=\/\/)([A-Za-z0-9+/=]+)/g, (_match, path) => atob(path)], // plain text URLs
[/\[([A-Za-z0-9+/=]+)(\|.+?)?\]/g, (_match, url, label = '') => `[${atob(url)}${label}]`], // labeled link
];
/**
* Searches and replaces ASCII punctuation symbols for all given input fields by their preferred Unicode counterparts.
* These can only be guessed based on context as the ASCII symbols are ambiguous.
* @param {string[]} inputSelectors CSS selectors of the input fields.
* @param {object} options
* @param {string} [options.language] Language of the input fields' text (ISO 639-1 two letter code, optional).
* @param {boolean} [options.isReactInput] Whether the input fields are manipulated by React.
* @param {Event} [options.event] Event which should be triggered for changed input fields (optional).
*/
export function guessUnicodePunctuation(inputSelectors, options = {}) {
transformInputValues(inputSelectors.join(), punctuationRulesForLanguage(options.language), options);
}