Skip to content

Commit f8f9683

Browse files
committed
Add support for live typing 3 dashes
When using oldschool or inverted dashes, there is handling of turning both 2 and 3 dashes into different characters. When someone types 2 dashes in, they are swapped out for another character. This commit introduces handling of *that* character, plus another dash, to be seen as if they are 3 dashes. Closes GH-13.
1 parent d139880 commit f8f9683

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

lib/index.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* Position of `node` in `parent`.
1414
* @param {Parents} parent
1515
* Parent of `node`.
16-
* @returns {undefined}
17-
* Nothing.
16+
* @returns {boolean | undefined | void}
17+
* Whether to remove the node (`true`); or not (otherwise).
1818
*
1919
* @typedef Options
2020
* Configuration.
@@ -144,7 +144,12 @@ export default function retextSmartypants(options) {
144144
(node.type === 'PunctuationNode' || node.type === 'SymbolNode')
145145
) {
146146
while (++index < methods.length) {
147-
methods[index](state, node, position, parent)
147+
const result = methods[index](state, node, position, parent)
148+
if (result === true) {
149+
console.log('drop', node)
150+
parent.children.splice(position, 1)
151+
return position
152+
}
148153
}
149154
}
150155
})
@@ -195,7 +200,19 @@ function dashesDefault(_, node) {
195200
*
196201
* @type {Method}
197202
*/
198-
function dashesInverted(_, node) {
203+
function dashesInverted(_, node, index, parent) {
204+
const next = parent.children[index + 1]
205+
206+
if (
207+
node.value === '—' &&
208+
next &&
209+
next.type === 'PunctuationNode' &&
210+
next.value === '-'
211+
) {
212+
next.value = '–'
213+
return true
214+
}
215+
199216
if (node.value === '---') {
200217
node.value = '–'
201218
} else if (node.value === '--') {
@@ -208,7 +225,19 @@ function dashesInverted(_, node) {
208225
*
209226
* @type {Method}
210227
*/
211-
function dashesOldschool(_, node) {
228+
function dashesOldschool(_, node, index, parent) {
229+
const next = parent.children[index + 1]
230+
231+
if (
232+
node.value === '–' &&
233+
next &&
234+
next.type === 'PunctuationNode' &&
235+
next.value === '-'
236+
) {
237+
next.value = '—'
238+
return true
239+
}
240+
212241
if (node.value === '---') {
213242
node.value = '—'
214243
} else if (node.value === '--') {

test.js

+26
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ test('En- and em-dashes', async function (t) {
305305
)
306306
}
307307
)
308+
309+
await t.test(
310+
'should turn an en dash + dash into an em-dash',
311+
async function () {
312+
assert.equal(
313+
retext()
314+
.use(retextSmartypants, {dashes: 'oldschool'})
315+
.processSync('Alfred–-')
316+
.toString(),
317+
'Alfred—'
318+
)
319+
}
320+
)
308321
})
309322

310323
await t.test('inverted', async function (t) {
@@ -321,6 +334,19 @@ test('En- and em-dashes', async function (t) {
321334
)
322335
}
323336
)
337+
338+
await t.test(
339+
'should turn an em dash + dash into an en-dash',
340+
async function () {
341+
assert.equal(
342+
retext()
343+
.use(retextSmartypants, {dashes: 'inverted'})
344+
.processSync('Alfred—-')
345+
.toString(),
346+
'Alfred–'
347+
)
348+
}
349+
)
324350
})
325351
})
326352

0 commit comments

Comments
 (0)