-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use posthtml-widows instead of string-remove-widows
- Loading branch information
Showing
6 changed files
with
61 additions
and
135 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,86 +1,37 @@ | ||
import posthtml from 'posthtml' | ||
import posthtmlWidows from 'posthtml-widows' | ||
import { defu as merge } from 'defu' | ||
import { removeWidows } from 'string-remove-widows' | ||
|
||
const posthtmlPlugin = (options = {}) => tree => { | ||
export default function posthtmlPlugin(options = {}) { | ||
options = merge(options, { | ||
minWordCount: 3, | ||
attrName: 'no-widows' | ||
minWords: 3 | ||
}) | ||
|
||
// Ignore defaults | ||
// Custom ignores | ||
const mappings = [ | ||
// Jinja-like | ||
{ | ||
heads: '{{', | ||
tails: '}}' | ||
}, | ||
{ | ||
heads: ['{% if', '{%- if'], | ||
tails: ['{% endif', '{%- endif'] | ||
}, | ||
{ | ||
heads: ['{% for', '{%- for'], | ||
tails: ['{% endfor', '{%- endfor'] | ||
}, | ||
{ | ||
heads: ['{%', '{%-'], | ||
tails: ['%}', '-%}'] | ||
}, | ||
{ | ||
heads: '{#', | ||
tails: '#}' | ||
}, | ||
// ASP/Hexo-like | ||
{ | ||
heads: ['<%', '<%=', '<%-'], | ||
tails: ['%>', '=%>', '-%>'] | ||
}, | ||
// MSO comments | ||
{ | ||
heads: '<!--[', | ||
tails: ']>' | ||
start: '<!--[', | ||
end: ']>' | ||
}, | ||
// <![endif]--> | ||
{ | ||
heads: '<![', | ||
tails: ']--><' | ||
start: '<![', | ||
end: ']--><' | ||
} | ||
] | ||
|
||
if (Array.isArray(options.ignore)) { | ||
options.ignore.forEach(pair => mappings.push(pair)) | ||
} | ||
|
||
if (typeof options.ignore !== 'string') { | ||
options.ignore = mappings | ||
} | ||
|
||
const process = node => { | ||
if (node.attrs && Object.keys(node.attrs).includes(options.attrName)) { | ||
const widowsRemovedString = removeWidows(tree.render(node.content), options).res | ||
node.content = tree.render(tree.parser(widowsRemovedString)) | ||
delete node.attrs[options.attrName] | ||
} | ||
|
||
return node | ||
options.ignore = options.ignore.concat(mappings) | ||
} | ||
|
||
return tree.walk(process) | ||
return posthtmlWidows(options) | ||
} | ||
|
||
export default posthtmlPlugin | ||
|
||
export async function preventWidows(html = '', options = {}, posthtmlOptions = {}) { | ||
// Apply only to elements that contain the `prevent-widows` attribute | ||
if (options.withAttributes) { | ||
return posthtml([ | ||
posthtmlPlugin(options) | ||
]) | ||
.process(html, posthtmlOptions) | ||
.then(result => result.html) | ||
} | ||
|
||
// Apply to all elements | ||
return removeWidows(html, options).res | ||
return posthtml([ | ||
posthtmlPlugin(options) | ||
]) | ||
.process(html, posthtmlOptions) | ||
.then(result => result.html) | ||
} |
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
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,58 +1,38 @@ | ||
import type { Opts } from 'string-remove-widows'; | ||
|
||
export default interface WidowWordsConfig { | ||
/** | ||
The attribute name to use. | ||
@default 'prevent-widows' | ||
@default ['prevent-widows', 'no-widows'] | ||
*/ | ||
attrName?: string; | ||
attributes?: Array<string>; | ||
|
||
/** | ||
Replace all widow word `nbsp;` instances with a single space. | ||
This is basically the opposite of preventing widow words. | ||
@default false | ||
*/ | ||
removeWidowPreventionMeasures?: Opts['removeWidowPreventionMeasures']; | ||
|
||
/** | ||
Convert the space entity to the `targetLanguage`. | ||
@default true | ||
*/ | ||
convertEntities?: Opts['convertEntities']; | ||
|
||
/** | ||
Language to encode non-breaking spaces in. | ||
@default 'html' | ||
*/ | ||
targetLanguage?: Opts['targetLanguage']; | ||
|
||
/** | ||
Should whitespace in front of dashes (-), n-dashes (–) or m-dashes (—) be replaced with a ` `. | ||
@default true | ||
*/ | ||
hyphens?: Opts['hyphens']; | ||
createWidows?: Boolean; | ||
|
||
/** | ||
The minimum amount of words in a target string, in order to trigger the transformer. | ||
@default 3 | ||
*/ | ||
minWordCount?: Opts['minWordCount']; | ||
|
||
/** | ||
The minimum amount non-whitespace characters in a target string, in order to trigger the transformer. | ||
@default 20 | ||
*/ | ||
minCharCount?: Opts['minCharCount']; | ||
minWords?: Number; | ||
|
||
/** | ||
Start/end pairs of strings that will prevent the transformer from removing widow words inside them. | ||
*/ | ||
ignore?: Opts['ignore']; | ||
@default [ | ||
{ start: '{{', end: '}}' }, // Handlebars, Liquid, Nunjucks, Twig, Jinja2, Mustache | ||
{ start: '{%', end: '%}' }, // Liquid, Nunjucks, Twig, Jinja2 | ||
{ start: '<%=', end: '%>' }, // EJS, ERB | ||
{ start: '<%', end: '%>' }, // EJS, ERB | ||
{ start: '{$', end: '}' }, // Smarty | ||
{ start: '<\\?', end: '\\?>' }, // PHP | ||
{ start: '#{', end: '}' } // Pug | ||
] | ||
*/ | ||
ignore?: Array<{ start: string; end: string }>; | ||
} |