-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
384 lines (348 loc) · 9.58 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* @typedef {import('nlcst').Root} Root
* @typedef {import('nlcst').Sentence} Sentence
* @typedef {import('nlcst').Word} Word
* @typedef {import('nlcst').Symbol} Symbol
* @typedef {import('nlcst').Punctuation} Punctuation
* @typedef {import('nlcst').SentenceContent} SentenceContent
*
* @typedef Options
* Configuration.
* @property {boolean} [quotes=true]
* Create smart quotes.
*
* Converts straight double and single quotes to smart double or single
* quotes.
* @property {boolean} [ellipses=true]
* Create smart ellipses.
*
* Converts triple dot characters (with or without spaces between) into a
* single Unicode ellipsis character.
* @property {boolean|'all'} [backticks=true]
* Create smart quotes from backticks.
*
* When `true`, converts double back-ticks into an opening double quote, and
* double straight single quotes into a closing double quote.
*
* When `'all'`: does the preceding and converts single back-ticks into an
* opening single quote, and a straight single quote into a closing single
* smart quote.
*
* Note: Quotes can not be `true` when `backticks` is `'all'`;
* @property {boolean|'oldschool'|'inverted'} [dashes=true]
* Create smart dashes.
*
* When `true`, converts two dashes into an em-dash character.
*
* When `'oldschool'`, converts two dashes into an en-dash, and three dashes
* into an em-dash.
*
* When `'inverted'`, converts two dashes into an em-dash, and three dashes
* into an en-dash.
*
* @callback Method
* @param {Punctuation|Symbol} node
* @param {number} index
* @param {Word|Sentence} parent
* @returns {void}
*/
import {visit} from 'unist-util-visit'
import {toString} from 'nlcst-to-string'
const closingQuotes = {'"': '”', "'": '’'}
const openingQuotes = {'"': '“', "'": '‘'}
const educators = {
dashes: {
/**
* Transform two dahes into an em-dash.
*
* @type {Method}
*/
true(node) {
if (node.value === '--') {
node.value = '—'
}
},
/**
* Transform three dahes into an em-dash, and two into an en-dash.
*
* @type {Method}
*/
oldschool(node) {
if (node.value === '---') {
node.value = '—'
} else if (node.value === '--') {
node.value = '–'
}
},
/**
* Transform three dahes into an en-dash, and two into an em-dash.
*
* @type {Method}
*/
inverted(node) {
if (node.value === '---') {
node.value = '–'
} else if (node.value === '--') {
node.value = '—'
}
}
},
backticks: {
/**
* Transform double backticks and single quotes into smart quotes.
*
* @type {Method}
*/
true(node) {
if (node.value === '``') {
node.value = '“'
} else if (node.value === "''") {
node.value = '”'
}
},
/**
* Transform single and double backticks and single quotes into smart quotes.
*
* @type {Method}
*/
all(node, index, parent) {
educators.backticks.true(node, index, parent)
if (node.value === '`') {
node.value = '‘'
} else if (node.value === "'") {
node.value = '’'
}
}
},
ellipses: {
/**
* Transform multiple dots into unicode ellipses.
*
* @type {Method}
*/
true(node, index, parent) {
const value = node.value
const siblings = parent.children
// Simple node with three dots and without white-space.
if (/^\.{3,}$/.test(node.value)) {
node.value = '…'
return
}
if (!/^\.+$/.test(value)) {
return
}
// Search for dot-nodes with white-space between.
/** @type {SentenceContent[]} */
const nodes = []
let position = index
let count = 1
// It’s possible that the node is merged with an adjacent word-node. In that
// code, we cannot transform it because there’s no reference to the
// grandparent.
while (--position > 0) {
let sibling = siblings[position]
if (sibling.type !== 'WhiteSpaceNode') {
break
}
const queue = sibling
sibling = siblings[--position]
if (
sibling &&
(sibling.type === 'PunctuationNode' ||
sibling.type === 'SymbolNode') &&
/^\.+$/.test(sibling.value)
) {
nodes.push(queue, sibling)
count++
continue
}
break
}
if (count < 3) {
return
}
siblings.splice(index - nodes.length, nodes.length)
node.value = '…'
}
},
quotes: {
/**
* Transform straight single- and double quotes into smart quotes.
*
* @type {Method}
*/
// eslint-disable-next-line complexity
true(node, index, parent) {
const siblings = parent.children
const value = node.value
if (value !== '"' && value !== "'") {
return
}
const previous = siblings[index - 1]
const next = siblings[index + 1]
const nextNext = siblings[index + 2]
const nextValue = next && toString(next)
if (
next &&
nextNext &&
(next.type === 'PunctuationNode' || next.type === 'SymbolNode') &&
nextNext.type !== 'WordNode'
) {
// Special case if the very first character is a quote followed by
// punctuation at a non-word-break. Close the quotes by brute force.
node.value = closingQuotes[value]
} else if (
nextNext &&
(nextValue === '"' || nextValue === "'") &&
nextNext.type === 'WordNode'
) {
// Special case for double sets of quotes:
// `He said, "'Quoted' words in a larger quote."`
node.value = openingQuotes[value]
// @ts-expect-error: it’s a literal.
next.value = openingQuotes[nextValue]
} else if (next && /^\d\ds$/.test(nextValue)) {
// Special case for decade abbreviations: `the '80s`
node.value = closingQuotes[value]
} else if (
previous &&
next &&
(previous.type === 'WhiteSpaceNode' ||
previous.type === 'PunctuationNode' ||
previous.type === 'SymbolNode') &&
next.type === 'WordNode'
) {
// Get most opening single quotes.
node.value = openingQuotes[value]
} else if (
previous &&
previous.type !== 'WhiteSpaceNode' &&
previous.type !== 'SymbolNode' &&
previous.type !== 'PunctuationNode'
) {
// Closing quotes.
node.value = closingQuotes[value]
} else if (
!next ||
next.type === 'WhiteSpaceNode' ||
(value === "'" && nextValue === 's')
) {
node.value = closingQuotes[value]
} else {
node.value = openingQuotes[value]
}
}
}
}
/**
* Plugin to replace dumb/straight/typewriter punctuation marks with smart/curly
* punctuation marks.
*
* @type {import('unified').Plugin<[Options?]|[], Root>}
*/
export default function retextSmartypants(options = {}) {
/** @type {Array.<Method>} */
const methods = []
/** @type {Options['quotes']} */
let quotes
/** @type {Options['ellipses']} */
let ellipses
/** @type {Options['backticks']} */
let backticks
/** @type {Options['dashes']} */
let dashes
if ('quotes' in options) {
quotes = options.quotes
if (quotes !== Boolean(quotes)) {
throw new TypeError(
'Illegal invocation: `' +
quotes +
'` ' +
'is not a valid value for `quotes` in ' +
'`smartypants`'
)
}
} else {
quotes = true
}
if ('ellipses' in options) {
ellipses = options.ellipses
if (ellipses !== Boolean(ellipses)) {
throw new TypeError(
'Illegal invocation: `' +
ellipses +
'` ' +
'is not a valid value for `ellipses` in ' +
'`smartypants`'
)
}
} else {
ellipses = true
}
if ('backticks' in options) {
backticks = options.backticks
if (backticks !== Boolean(backticks) && backticks !== 'all') {
throw new TypeError(
'Illegal invocation: `' +
backticks +
'` ' +
'is not a valid value for `backticks` in ' +
'`smartypants`'
)
}
if (backticks === 'all' && quotes === true) {
throw new TypeError(
'Illegal invocation: `backticks: ' +
backticks +
'` is not a valid value ' +
'when `quotes: ' +
quotes +
'` in ' +
'`smartypants`'
)
}
} else {
backticks = true
}
if ('dashes' in options) {
dashes = options.dashes
if (
dashes !== Boolean(dashes) &&
dashes !== 'oldschool' &&
dashes !== 'inverted'
) {
throw new TypeError(
'Illegal invocation: `' +
dashes +
'` ' +
'is not a valid value for `dahes` in ' +
'`smartypants`'
)
}
} else {
dashes = true
}
if (quotes !== false) {
methods.push(educators.quotes.true)
}
if (ellipses !== false) {
methods.push(educators.ellipses.true)
}
if (backticks !== false) {
methods.push(educators.backticks[backticks === true ? 'true' : backticks])
}
if (dashes !== false) {
methods.push(educators.dashes[dashes === true ? 'true' : dashes])
}
return (tree) => {
visit(tree, (node, position, parent) => {
let index = -1
if (node.type === 'PunctuationNode' || node.type === 'SymbolNode') {
while (++index < methods.length) {
// @ts-expect-error: they’re literals.
methods[index](node, position, parent)
}
}
})
}
}