mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Minify highlights snippets.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const marky = require('marky-markdown');
|
const marky = require('marky-markdown');
|
||||||
|
const minify = require('html-minifier').minify;
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const util = require('../common/util');
|
const util = require('../common/util');
|
||||||
|
|
||||||
@@ -14,24 +15,33 @@ const highlights = [
|
|||||||
'comment',
|
'comment',
|
||||||
'constant',
|
'constant',
|
||||||
'delimiter',
|
'delimiter',
|
||||||
'html',
|
|
||||||
'js',
|
|
||||||
'method',
|
'method',
|
||||||
'modifier',
|
'modifier',
|
||||||
'numeric',
|
'numeric',
|
||||||
'shell',
|
|
||||||
'source',
|
|
||||||
'string',
|
'string',
|
||||||
'text',
|
|
||||||
'type'
|
'type'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const hlTypes = [
|
||||||
|
'html',
|
||||||
|
'js',
|
||||||
|
'shell'
|
||||||
|
];
|
||||||
|
|
||||||
|
const hlSources = [
|
||||||
|
'highlight',
|
||||||
|
'source',
|
||||||
|
'text'
|
||||||
|
];
|
||||||
|
|
||||||
function build(type) {
|
function build(type) {
|
||||||
const markdown = fs
|
const markdown = fs
|
||||||
// Load markdown.
|
// Load markdown.
|
||||||
.readFileSync(readmePath, 'utf8')
|
.readFileSync(readmePath, 'utf8')
|
||||||
// Uncomment docdown HTML hints.
|
// Uncomment docdown HTML hints.
|
||||||
.replace(/(<)!--\s*|\s*--(>)/g, '$1$2');
|
.replace(/(<)!--\s*|\s*--(>)/g, '$1$2')
|
||||||
|
// Escape HTML markup in usage examples.
|
||||||
|
.replace(/```js[\s\S]+?```/g, m => m.replace(/</g, '<').replace(/>/g, '>'));
|
||||||
|
|
||||||
const $ = marky(markdown, { 'sanitize': false });
|
const $ = marky(markdown, { 'sanitize': false });
|
||||||
const $header = $('h1').first().remove();
|
const $header = $('h1').first().remove();
|
||||||
@@ -74,9 +84,47 @@ function build(type) {
|
|||||||
// Cleanup highlights class names.
|
// Cleanup highlights class names.
|
||||||
$('.highlight [class]').each(function() {
|
$('.highlight [class]').each(function() {
|
||||||
const $el = $(this);
|
const $el = $(this);
|
||||||
const className = _.intersection($el.attr('class').split(/\s+/), highlights).join(' ');
|
const names = $el.attr('class').split(/\s+/);
|
||||||
|
const attr = _.intersection(names, highlights).join(' ');
|
||||||
|
|
||||||
$el.attr('class', className || null);
|
if (!_.isEmpty(_.intersection(names, hlSources)) &&
|
||||||
|
!_.isEmpty(_.intersection(names, hlTypes))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$el.attr('class', attr || null);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unwrap elements containing only text.
|
||||||
|
$('.highlight :not([class])').each(function() {
|
||||||
|
const $el = $(this);
|
||||||
|
if (_.every($(el).children(), ['type', 'text'])) {
|
||||||
|
$el.replaceWith($el.text());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Consolidate hightlights comments.
|
||||||
|
$('.highlight [class~="comment"] > [class~="comment"]').each(function() {
|
||||||
|
const $parent = $(this).parent();
|
||||||
|
$parent.text($parent.text());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Minify hightlights snippets.
|
||||||
|
$('.highlight').each(function() {
|
||||||
|
const $div = $(this);
|
||||||
|
$div.html(minify($div.html(), {
|
||||||
|
'collapseBooleanAttributes': true,
|
||||||
|
'collapseInlineTagWhitespace': true,
|
||||||
|
'collapseWhitespace': true,
|
||||||
|
'removeAttributeQuotes': true,
|
||||||
|
'removeComments': true,
|
||||||
|
'removeEmptyAttributes': true,
|
||||||
|
'removeEmptyElements': true,
|
||||||
|
'removeOptionalTags': true,
|
||||||
|
'removeRedundantAttributes': true
|
||||||
|
})
|
||||||
|
.replace(/(?:<span>[^<]+<\/span>){2,}/g, match =>
|
||||||
|
match.replace(/<\/?span>/g, '')
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
const html = [
|
const html = [
|
||||||
|
|||||||
@@ -38,6 +38,7 @@
|
|||||||
"ecstatic": "^2.1.0",
|
"ecstatic": "^2.1.0",
|
||||||
"fs-extra": "~0.30.0",
|
"fs-extra": "~0.30.0",
|
||||||
"glob": "^7.0.6",
|
"glob": "^7.0.6",
|
||||||
|
"html-minifier": "^3.0.2",
|
||||||
"istanbul": "0.4.5",
|
"istanbul": "0.4.5",
|
||||||
"jquery": "^3.1.0",
|
"jquery": "^3.1.0",
|
||||||
"jscs": "^3.0.7",
|
"jscs": "^3.0.7",
|
||||||
|
|||||||
Reference in New Issue
Block a user