Skip to content

Commit

Permalink
Add support for live typing 3 dashes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed Oct 10, 2024
1 parent d139880 commit f8f9683
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
39 changes: 34 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* Position of `node` in `parent`.
* @param {Parents} parent
* Parent of `node`.
* @returns {undefined}
* Nothing.
* @returns {boolean | undefined | void}
* Whether to remove the node (`true`); or not (otherwise).
*
* @typedef Options
* Configuration.
Expand Down Expand Up @@ -144,7 +144,12 @@ export default function retextSmartypants(options) {
(node.type === 'PunctuationNode' || node.type === 'SymbolNode')
) {
while (++index < methods.length) {
methods[index](state, node, position, parent)
const result = methods[index](state, node, position, parent)
if (result === true) {
console.log('drop', node)
parent.children.splice(position, 1)
return position
}
}
}
})
Expand Down Expand Up @@ -195,7 +200,19 @@ function dashesDefault(_, node) {
*
* @type {Method}
*/
function dashesInverted(_, node) {
function dashesInverted(_, node, index, parent) {
const next = parent.children[index + 1]

if (
node.value === '—' &&
next &&
next.type === 'PunctuationNode' &&
next.value === '-'
) {
next.value = '–'
return true
}

if (node.value === '---') {
node.value = '–'
} else if (node.value === '--') {
Expand All @@ -208,7 +225,19 @@ function dashesInverted(_, node) {
*
* @type {Method}
*/
function dashesOldschool(_, node) {
function dashesOldschool(_, node, index, parent) {
const next = parent.children[index + 1]

if (
node.value === '–' &&
next &&
next.type === 'PunctuationNode' &&
next.value === '-'
) {
next.value = '—'
return true
}

if (node.value === '---') {
node.value = '—'
} else if (node.value === '--') {
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ test('En- and em-dashes', async function (t) {
)
}
)

await t.test(
'should turn an en dash + dash into an em-dash',
async function () {
assert.equal(
retext()
.use(retextSmartypants, {dashes: 'oldschool'})
.processSync('Alfred–-')
.toString(),
'Alfred—'
)
}
)
})

await t.test('inverted', async function (t) {
Expand All @@ -321,6 +334,19 @@ test('En- and em-dashes', async function (t) {
)
}
)

await t.test(
'should turn an em dash + dash into an en-dash',
async function () {
assert.equal(
retext()
.use(retextSmartypants, {dashes: 'inverted'})
.processSync('Alfred—-')
.toString(),
'Alfred–'
)
}
)
})
})

Expand Down

0 comments on commit f8f9683

Please sign in to comment.