-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpunctuation.js
29 lines (23 loc) · 1.03 KB
/
punctuation.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
module.exports = (text) => {
// N Dash
// https://en.wikipedia.org/wiki/Dash#En_dash
// This is before M Dash because we want to handle number ranges as a special case before the more general fixes below.
text = text.replace(/(\d+)\s?-\s?(\d+)/g, "$1 – $2");
text = text.replace(/(\d+)\s?–\s?(\d+)/g, "$1 – $2");
text = text.replace(/(\d+)\s?—\s?(\d+)/g, "$1 – $2");
// M Dash
// https://en.wikipedia.org/wiki/Dash#Em_dash
text = text.replace(/--/g, "–");
text = text.replace(/ – /g, " — ");
// Ellipsis
// https://en.wikipedia.org/wiki/Ellipsis
text = text.replace(/\.\.\./g, "…");
// Non-breaking space
// https://en.wikipedia.org/wiki/Non-breaking_space
const NBSP = " ";
const NBSP_PUNCTUATION_START = /([«¿¡]) /g;
const NBSP_PUNCTUATION_END = / ([\!\?:;\.,‽»])/g;
text = text.replace(NBSP_PUNCTUATION_START, "$1" + NBSP);
text = text.replace(NBSP_PUNCTUATION_END, NBSP + "$1");
return text;
};