Socket
Socket
Sign inDemoInstall

retext-smartypants

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

retext-smartypants - npm Package Compare versions

Comparing version 5.2.0 to 6.0.0

lib/index.d.ts

90

index.d.ts

@@ -1,87 +0,3 @@

/**
* Plugin to replace dumb/straight/typewriter punctuation marks with smart/curly
* punctuation marks.
*
* @type {import('unified').Plugin<[Options?]|[], Root>}
*/
export default function retextSmartypants(
options?: Options | undefined
):
| void
| import('unified').Transformer<import('nlcst').Root, import('nlcst').Root>
export type Root = import('nlcst').Root
export type Sentence = import('nlcst').Sentence
export type Word = import('nlcst').Word
export type Symbol = import('nlcst').Symbol
export type Punctuation = import('nlcst').Punctuation
export type SentenceContent = import('nlcst').SentenceContent
/**
* Quote characters.
*/
export type QuoteCharacterMap = {
/**
* Character to use for double quotes.
*/
double: string
/**
* Character to use for single quotes.
*/
single: string
}
/**
* Configuration.
*/
export type Options = {
/**
* Create smart quotes.
*
* Converts straight double and single quotes to smart double or single
* quotes.
*/
quotes?: boolean | undefined
/**
* Characters to use for opening double and single quotes.
*/
openingQuotes?: QuoteCharacterMap | undefined
/**
* Characters to use for closing double and single quotes.
*/
closingQuotes?: QuoteCharacterMap | undefined
/**
* Create smart ellipses.
*
* Converts triple dot characters (with or without spaces between) into a
* single Unicode ellipsis character.
*/
ellipses?: boolean | undefined
/**
* 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'`;
*/
backticks?: boolean | 'all' | undefined
/**
* 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.
*/
dashes?: boolean | 'oldschool' | 'inverted' | undefined
}
export type Method = (
node: Punctuation | Symbol,
index: number,
parent: Word | Sentence
) => void
export { default } from "./lib/index.js";
export type Options = import('./lib/index.js').Options;
export type QuoteCharacterMap = import('./lib/index.js').QuoteCharacterMap;
/**
* @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 QuoteCharacterMap
* Quote characters.
* @property {string} double
* Character to use for double quotes.
* @property {string} single
* Character to use for single quotes.
*
* @typedef Options
* Configuration.
* @property {boolean} [quotes=true]
* Create smart quotes.
*
* Converts straight double and single quotes to smart double or single
* quotes.
* @property {QuoteCharacterMap} [openingQuotes]
* Characters to use for opening double and single quotes.
* @property {QuoteCharacterMap} [closingQuotes]
* Characters to use for closing double and 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}
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').QuoteCharacterMap} QuoteCharacterMap
*/
import {visit} from 'unist-util-visit'
import {toString} from 'nlcst-to-string'
const defaultClosingQuotes = {'"': '”', "'": '’'}
const defaultOpeningQuotes = {'"': '“', "'": '‘'}
/**
* @param {Options} options
*/
function createEducators(options) {
const closingQuotes = options.closingQuotes
? {'"': options.closingQuotes.double, "'": options.closingQuotes.single}
: defaultClosingQuotes
const openingQuotes = options.openingQuotes
? {'"': options.openingQuotes.double, "'": options.openingQuotes.single}
: defaultOpeningQuotes
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 {Array<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]
}
}
}
}
return educators
}
/**
* 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
}
const educators = createEducators(options)
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)
}
}
})
}
}
export {default} from './lib/index.js'
{
"name": "retext-smartypants",
"version": "5.2.0",
"version": "6.0.0",
"description": "retext plugin to implement SmartyPants",
"license": "MIT",
"keywords": [
"unified",
"dashes",
"ellipses",
"quotes",
"retext",

@@ -12,5 +14,3 @@ "retext-plugin",

"typography",
"quotes",
"dashes",
"ellipses"
"unified"
],

@@ -29,5 +29,5 @@ "repository": "retextjs/retext-smartypants",

"type": "module",
"main": "index.js",
"types": "index.d.ts",
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts",

@@ -37,41 +37,37 @@ "index.js"

"dependencies": {
"@types/nlcst": "^1.0.0",
"nlcst-to-string": "^3.0.0",
"unified": "^10.0.0",
"unist-util-visit": "^4.0.0"
"@types/nlcst": "^2.0.0",
"nlcst-to-string": "^4.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^10.0.0",
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"retext": "^8.0.0",
"retext": "^9.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.50.0"
"typescript": "^5.0.0",
"xo": "^0.56.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"prepack": "npm run build && npm run format",
"test": "npm run build && npm run format && npm run test-coverage",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run build && npm run format && npm run test-coverage"
"test-coverage": "c8 --100 --check-coverage --reporter lcov npm run test-api"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"xo": {
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
"remark-preset-wooorm"
]

@@ -82,5 +78,8 @@ },

"detail": true,
"strict": true,
"ignoreCatch": true
"ignoreCatch": true,
"strict": true
},
"xo": {
"prettier": true
}
}

@@ -21,2 +21,4 @@ # retext-smartypants

* [`unified().use(retextSmartypants[, options])`](#unifieduseretextsmartypants-options)
* [`Options`](#options)
* [`QuoteCharacterMap`](#quotecharactermap)
* [Types](#types)

@@ -42,3 +44,3 @@ * [Compatibility](#compatibility)

This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
In Node.js (version 16+), install with [npm][]:

@@ -52,3 +54,3 @@ ```sh

```js
import retextSmartypants from 'https://esm.sh/retext-smartypants@5'
import retextSmartypants from 'https://esm.sh/retext-smartypants@6'
```

@@ -60,3 +62,3 @@

<script type="module">
import retextSmartypants from 'https://esm.sh/retext-smartypants@5?bundle'
import retextSmartypants from 'https://esm.sh/retext-smartypants@6?bundle'
</script>

@@ -87,72 +89,76 @@ ```

This package exports no identifiers.
The default export is `retextSmartypants`.
The default export is [`retextSmartypants`][api-retext-smartypants].
### `unified().use(retextSmartypants[, options])`
Apply [SmartyPants][].
Replace straight punctuation marks with curly ones.
##### `options`
###### Parameters
Configuration (optional).
* `options` ([`Options`][api-options], optional)
— configuration
###### `options.quotes`
###### Returns
Create smart quotes (`boolean`, default: `true`).
Transform ([`Transformer`][unified-transformer]).
Converts straight double and single quotes to smart double or single quotes.
The options `options.openingQuotes` and `options.closingQuotes` affect which
quotes are considered smart.
### `Options`
###### `options.openingQuotes`
Configuration (TypeScript type).
Characters to use for opening quotes `{single: '‘', double: '“'}`.
###### Fields
###### `options.closingQuotes`
* `backticks` (`boolean` or `'all'`, default: `true`)
— transform backticks;
when `true`, turns double backticks into an opening double quote and
double straight single quotes into a closing double quote;
when `'all'`, does that and turns single backticks into an opening
single quote and a straight single quotes into a closing single smart
quote;
`quotes: false` must be used with `backticks: 'all'`
* `closingQuotes` ([`QuoteCharacterMap`][api-quote-character-map], default:
`{double: '”', single: '’'}`)
— closing quotes to use
* `dashes` (`'inverted'` or `'oldschool'` or `boolean`, default: `true`)
transform dashes;
when `true`, turns two dashes into an em dash character;
when `'oldschool'`, turns three dashes into an em dash and two into an en
dash;
when `'inverted'`, turns three dashes into an en dash and two into an em
dash
* `ellipses` (`boolean`, default: `true`)
— transform triple dots, with or without spaces between
* `openingQuotes` ([`QuoteCharacterMap`][api-quote-character-map], default:
`{double: '“', single: '‘'}`)
— opening quotes to use
* `quotes` (`boolean`, default: `true`)
— Transform straight quotes into smart quotes
Characters to use for closing quotes `{single: '’', double: '”'}`.
### `QuoteCharacterMap`
###### `options.ellipses`
Quote characters (TypeScript type).
Create smart ellipses (`boolean`, default: `true`).
###### Fields
Converts triple dot characters (with or without spaces) into a single unicode
ellipsis character.
* `double` (`string`)
— character to use for double quotes
* `single` (`string`)
— character to use for single quotes
###### `options.backticks`
Create smart quotes from backticks (`boolean` or `'all'`, default: `true`).
When `true`, converts double backticks into an opening double quote, and
double straight single quotes into a closing double quote.
When `'all'`: does the what `true` does with the addition of converting single
backticks into an opening single quote, and a straight single quote into a
closing single smart quote.
> 👉 **Note**: `options.quotes` can not be `true` when `backticks` is `'all'`.
###### `options.dashes`
Create smart dashes (`boolean` or `'oldschool'`, `'inverted'`, default: `true`).
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.
## Types
This package is fully typed with [TypeScript][].
It exports the additional types `Options` and `QuoteCharacterMap`.
It exports the additional types [`Options`][api-options] and
[`QuoteCharacterMap`][api-quote-character-map].
## Compatibility
Projects maintained by the unified collective are compatible with all maintained
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `retext-smartypants@^6`,
compatible with Node.js 16.
## Contribute

@@ -186,5 +192,5 @@

[size-badge]: https://img.shields.io/bundlephobia/minzip/retext-smartypants.svg
[size-badge]: https://img.shields.io/bundlejs/size/retext-smartypants
[size]: https://bundlephobia.com/result?p=retext-smartypants
[size]: https://bundlejs.com/?q=retext-smartypants

@@ -221,6 +227,14 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[unified]: https://github.com/unifiedjs/unified
[smartypants]: https://daringfireball.net/projects/smartypants
[retext]: https://github.com/retextjs/retext
[smartypants]: https://daringfireball.net/projects/smartypants
[unified]: https://github.com/unifiedjs/unified
[unified-transformer]: https://github.com/unifiedjs/unified#transformer
[api-options]: #options
[api-quote-character-map]: #quotecharactermap
[api-retext-smartypants]: #unifieduseretextsmartypants-options
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc