Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #788 from maizzle/refactor-widow-words
- Loading branch information
Showing
5 changed files
with
156 additions
and
22 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,13 +1,80 @@ | ||
const {get} = require('lodash') | ||
const posthtml = require('posthtml') | ||
const preventWidows = require('prevent-widows') | ||
const {get, isEmpty} = require('lodash') | ||
const {removeWidows} = require('string-remove-widows') | ||
|
||
module.exports = async (html, config = {}, direct = false) => { | ||
const posthtmlOptions = get(config, 'build.posthtml.options', {}) | ||
module.exports = async (html, config = {}) => { | ||
if (isEmpty(config)) { | ||
return removeWidows(html).res | ||
} | ||
|
||
const posthtmlOptions = get(config, 'build.posthtml.options', {recognizeNoValueAttribute: true}) | ||
|
||
return posthtml([removeWidowsPlugin(config)]).process(html, posthtmlOptions).then(result => result.html) | ||
} | ||
|
||
const removeWidowsPlugin = options => tree => { | ||
const {attrName = 'prevent-widows', ...removeWidowsOptions} = get(options, 'widowWords', options) | ||
|
||
removeWidowsOptions.minWordCount = removeWidowsOptions.minWordCount || 3 | ||
|
||
// Ignore defaults | ||
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: ']>' | ||
}, | ||
// <![endif]--> | ||
{ | ||
heads: '<![', | ||
tails: ']--><' | ||
} | ||
] | ||
|
||
if (Array.isArray(removeWidowsOptions.ignore)) { | ||
removeWidowsOptions.ignore.forEach(pair => mappings.push(pair)) | ||
} | ||
|
||
if (typeof removeWidowsOptions.ignore !== 'string') { | ||
removeWidowsOptions.ignore = mappings | ||
} | ||
|
||
const process = node => { | ||
if (node.attrs && Object.keys(node.attrs).includes(attrName)) { | ||
const widowsRemovedString = removeWidows(tree.render(node.content), removeWidowsOptions).res | ||
|
||
node.content = tree.render(tree.parser(widowsRemovedString)) | ||
node.attrs[attrName] = false | ||
} | ||
|
||
if (direct) { | ||
return preventWidows(html) | ||
return node | ||
} | ||
|
||
return posthtml([preventWidows.posthtml()]).process(html, posthtmlOptions).then(result => result.html) | ||
return tree.walk(process) | ||
} |
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