-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,5 @@ | ||
/** | ||
* @typedef {import('nlcst').Root} Root | ||
* | ||
* @typedef {0|1|2|'newline'|'space'|'double-space'|null|undefined} Preferred | ||
* | ||
* @typedef Options | ||
* Configuration. | ||
* @property {Preferred} [preferred=1] | ||
* Spaces between sentences. | ||
* Use `0` for line endings. | ||
* @typedef {import('./lib/index.js').Options} Options | ||
*/ | ||
|
||
import {toString} from 'nlcst-to-string' | ||
import {visit} from 'unist-util-visit' | ||
import {convert} from 'unist-util-is' | ||
|
||
const sentence = convert('SentenceNode') | ||
const whiteSpace = convert('WhiteSpaceNode') | ||
|
||
const source = 'retext-sentence-spacing' | ||
const url = 'https://github.com/retextjs/retext-sentence-spacing#readme' | ||
|
||
/** | ||
* Plugin to check spacing between sentences. | ||
* Emit warnings when the spacing does not adhere to the preferred style. | ||
* | ||
* @type {import('unified').Plugin<[Options?], Root>} | ||
*/ | ||
export default function retextSentenceSpacing(options = {}) { | ||
let preferred = options.preferred | ||
|
||
if (preferred === 'newline') { | ||
preferred = 0 | ||
} | ||
|
||
if (preferred === null || preferred === undefined || preferred === 'space') { | ||
preferred = 1 | ||
} | ||
|
||
if (preferred === 'double-space') { | ||
preferred = 2 | ||
} | ||
|
||
if (typeof preferred !== 'number') { | ||
throw new TypeError( | ||
"Expected `options.preferred` to be `'space'`, `'newline'`, or a `number`" | ||
) | ||
} | ||
|
||
if (preferred < 0 || preferred > 2) { | ||
throw new Error( | ||
"Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between (including) `0` and `2`" | ||
) | ||
} | ||
|
||
return (tree, file) => { | ||
visit(tree, 'ParagraphNode', (node) => { | ||
let index = -1 | ||
|
||
while (++index < node.children.length) { | ||
const child = node.children[index] | ||
|
||
if ( | ||
!sentence(node.children[index - 1]) || | ||
!whiteSpace(child) || | ||
!sentence(node.children[index + 1]) | ||
) { | ||
continue | ||
} | ||
|
||
const actual = toString(child) | ||
|
||
// We only check for whitespace that is *just* spaces: it’s OK to add | ||
// line feeds if `space` is expected. | ||
if (!/^ +$/.test(actual)) { | ||
continue | ||
} | ||
|
||
const size = actual.length | ||
/** @type {string} */ | ||
let reason | ||
|
||
// Size is never preferred if we want a line feed. | ||
if (preferred === 0) { | ||
reason = | ||
'Expected a newline between sentences, not `' + | ||
size + | ||
'` space' + | ||
(size === 1 ? '' : 's') | ||
} else if (size === preferred) { | ||
continue | ||
} else { | ||
reason = | ||
'Expected `' + | ||
preferred + | ||
'` space' + | ||
(preferred === 1 ? '' : 's') + | ||
' between sentences, not `' + | ||
size + | ||
'`' | ||
} | ||
|
||
Object.assign( | ||
file.message( | ||
reason, | ||
child, | ||
[ | ||
source, | ||
preferred === 0 | ||
? 'newline' | ||
: preferred === 1 | ||
? 'space' | ||
: 'double-space' | ||
].join(':') | ||
), | ||
{ | ||
actual, | ||
expected: [preferred === 0 ? '\n' : preferred === 1 ? ' ' : ' '], | ||
url | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
export {default} from './lib/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* @typedef {import('nlcst').Root} Root | ||
* | ||
* @typedef {0|1|2|'newline'|'space'|'double-space'|null|undefined} Preferred | ||
* | ||
* @typedef Options | ||
* Configuration. | ||
* @property {Preferred} [preferred=1] | ||
* Spaces between sentences. | ||
* Use `0` for line endings. | ||
*/ | ||
|
||
import {toString} from 'nlcst-to-string' | ||
import {visit} from 'unist-util-visit' | ||
import {convert} from 'unist-util-is' | ||
|
||
const sentence = convert('SentenceNode') | ||
const whiteSpace = convert('WhiteSpaceNode') | ||
|
||
const source = 'retext-sentence-spacing' | ||
const url = 'https://github.com/retextjs/retext-sentence-spacing#readme' | ||
|
||
/** | ||
* Plugin to check spacing between sentences. | ||
* Emit warnings when the spacing does not adhere to the preferred style. | ||
* | ||
* @type {import('unified').Plugin<[Options?], Root>} | ||
*/ | ||
export default function retextSentenceSpacing(options = {}) { | ||
let preferred = options.preferred | ||
|
||
if (preferred === 'newline') { | ||
preferred = 0 | ||
} | ||
|
||
if (preferred === null || preferred === undefined || preferred === 'space') { | ||
preferred = 1 | ||
} | ||
|
||
if (preferred === 'double-space') { | ||
preferred = 2 | ||
} | ||
|
||
if (typeof preferred !== 'number') { | ||
throw new TypeError( | ||
"Expected `options.preferred` to be `'space'`, `'newline'`, or a `number`" | ||
) | ||
} | ||
|
||
if (preferred < 0 || preferred > 2) { | ||
throw new Error( | ||
"Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between (including) `0` and `2`" | ||
) | ||
} | ||
|
||
return (tree, file) => { | ||
visit(tree, 'ParagraphNode', (node) => { | ||
let index = -1 | ||
|
||
while (++index < node.children.length) { | ||
const child = node.children[index] | ||
|
||
if ( | ||
!sentence(node.children[index - 1]) || | ||
!whiteSpace(child) || | ||
!sentence(node.children[index + 1]) | ||
) { | ||
continue | ||
} | ||
|
||
const actual = toString(child) | ||
|
||
// We only check for whitespace that is *just* spaces: it’s OK to add | ||
// line feeds if `space` is expected. | ||
if (!/^ +$/.test(actual)) { | ||
continue | ||
} | ||
|
||
const size = actual.length | ||
/** @type {string} */ | ||
let reason | ||
|
||
// Size is never preferred if we want a line feed. | ||
if (preferred === 0) { | ||
reason = | ||
'Expected a newline between sentences, not `' + | ||
size + | ||
'` space' + | ||
(size === 1 ? '' : 's') | ||
} else if (size === preferred) { | ||
continue | ||
} else { | ||
reason = | ||
'Expected `' + | ||
preferred + | ||
'` space' + | ||
(preferred === 1 ? '' : 's') + | ||
' between sentences, not `' + | ||
size + | ||
'`' | ||
} | ||
|
||
Object.assign( | ||
file.message( | ||
reason, | ||
child, | ||
[ | ||
source, | ||
preferred === 0 | ||
? 'newline' | ||
: preferred === 1 | ||
? 'space' | ||
: 'double-space' | ||
].join(':') | ||
), | ||
{ | ||
actual, | ||
expected: [preferred === 0 ? '\n' : preferred === 1 ? ' ' : ' '], | ||
url | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters