Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expanded ellipses options #11

Merged
merged 9 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
* dash;
* when `'inverted'`, turns three dashes into an en dash and two into an em
* dash.
* @property {boolean | null | undefined} [ellipses=true]
* @property {'unspaced' | 'spaced' | boolean | null | undefined} [ellipses=true]
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
* Transform triple dots (default: `true`), with or without spaces between.
* when `true`, turns triple dots into ellipses (with or without spaces between);
* when `'unspaced'`, turns triple dots into ellipses (only for those without spaces);
* when `'spaced'`, turns triple dots into ellipses (only for those with spaces).
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
* @property {QuoteCharacterMap | null | undefined} [openingQuotes]
* Opening quotes to use (default: `{double: '“', single: '‘'}`).
* @property {boolean | null | undefined} [quotes=true]
Expand Down Expand Up @@ -91,7 +94,11 @@ export default function retextSmartypants(options) {
methods.push(quotesDefault)
}

if (settings.ellipses !== false) {
if (settings.ellipses === 'unspaced') {
methods.push(ellipsesUnspaced)
} else if (settings.ellipses === 'spaced') {
methods.push(ellipsesSpaced)
} else if (settings.ellipses !== false) {
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
methods.push(ellipsesDefault)
}

Expand Down Expand Up @@ -219,14 +226,30 @@ function dashesOldschool(_, node) {
* @type {Method}
*/
function ellipsesDefault(_, node, index, parent) {
const value = node.value
const siblings = parent.children
ellipsesUnspaced(_, node, index, parent)
ellipsesSpaced(_, node, index, parent)
}

/**
* Transform multiple dots into unicode ellipses (only for those without spaces).
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
*
* @type {Method}
*/
function ellipsesUnspaced(_, node) {
// Simple node with three dots and without whitespace.
if (/^\.{3,}$/.test(node.value)) {
node.value = '…'
return
}
}

/**
* Transform multiple dots into unicode ellipses (only for those with spaces).
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
*
* @type {Method}
*/
function ellipsesSpaced(_, node, index, parent) {
teddybradford marked this conversation as resolved.
Show resolved Hide resolved
const value = node.value
const siblings = parent.children

if (!/^\.+$/.test(value)) {
return
Expand Down
153 changes: 153 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,156 @@ test('Ellipses', async function (t) {
}
)
})

test('Ellipses (unspaced)', async function (t) {
const processor = retext().use(retextSmartypants, {ellipses: 'unspaced'})

await t.test('should replace three full stops', async function () {
assert.equal(
processor.processSync('Alfred... Bertrand.').toString(),
'Alfred\u2026 Bertrand.'
)
})

await t.test('should replace three initial full stops', async function () {
assert.equal(
processor.processSync('...Alfred Bertrand.').toString(),
'\u2026Alfred Bertrand.'
)
})

await t.test('should replace three final full stops', async function () {
assert.equal(
processor.processSync('Alfred Bertrand...').toString(),
'Alfred Bertrand\u2026'
)
})

await t.test('should replace three padded full stops', async function () {
assert.equal(
processor.processSync('Alfred ... Bertrand.').toString(),
'Alfred \u2026 Bertrand.'
)
})

await t.test(
'should replace three padded initial full stops',
async function () {
assert.equal(
processor.processSync('... Alfred Bertrand.').toString(),
'\u2026 Alfred Bertrand.'
)
}
)

await t.test(
'should replace three padded final full stops',
async function () {
assert.equal(
processor.processSync('Alfred Bertrand ...').toString(),
'Alfred Bertrand \u2026'
)
}
)

await t.test('should replace more than three full stops', async function () {
assert.equal(
processor.processSync('Alfred..... Bertrand.').toString(),
'Alfred\u2026 Bertrand.'
)

assert.equal(
processor.processSync('Alfred bertrand....').toString(),
'Alfred bertrand\u2026'
)

assert.equal(
processor.processSync('......Alfred bertrand.').toString(),
'\u2026Alfred bertrand.'
)
})

await t.test(
'should NOT replace less than three full stops',
async function () {
assert.equal(
processor.processSync('Alfred.. Bertrand.').toString(),
'Alfred.. Bertrand.'
)

assert.equal(
processor.processSync('Alfred bertrand. .').toString(),
'Alfred bertrand. .'
)

assert.equal(
processor.processSync('.Alfred bertrand.').toString(),
'.Alfred bertrand.'
)
}
)
})

test('Ellipses (spaced)', async function (t) {
const processor = retext().use(retextSmartypants, {ellipses: 'spaced'})

await t.test(
'should replace three padded full stops with spaces',
async function () {
assert.equal(
processor.processSync('Alfred . . . Bertrand.').toString(),
'Alfred \u2026 Bertrand.'
)
}
)

await t.test(
'should replace three padded initial full stops with spaces',
async function () {
assert.equal(
processor.processSync('. . . Alfred Bertrand.').toString(),
'\u2026 Alfred Bertrand.'
)
}
)

await t.test(
'should replace three padded final full stops with spaces',
async function () {
assert.equal(
processor.processSync('Alfred Bertrand . . .').toString(),
'Alfred Bertrand \u2026'
)
}
)

await t.test(
'should replace three full stops with spaces',
async function () {
assert.equal(
processor.processSync('Alfred. . . Bertrand.').toString(),
'Alfred\u2026 Bertrand.'
)
}
)

await t.test(
'should replace three initial full stops with spaces',
async function () {
assert.equal(
processor.processSync('. . .Alfred Bertrand.').toString(),
'\u2026Alfred Bertrand.'
)
}
)

await t.test(
'should replace three final full stops with spaces',
async function () {
assert.equal(
processor.processSync('Alfred Bertrand. . .').toString(),
'Alfred Bertrand\u2026'
)
}
)
})
Loading