mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
Reduce highlights further.
This commit is contained in:
@@ -11,28 +11,23 @@ const basePath = path.join(__dirname, '..', '..');
|
|||||||
const docPath = path.join(basePath, 'doc');
|
const docPath = path.join(basePath, 'doc');
|
||||||
const readmePath = path.join(docPath, 'README.md');
|
const readmePath = path.join(docPath, 'README.md');
|
||||||
|
|
||||||
const highlights = [
|
const highlights = {
|
||||||
'comment',
|
'html': [
|
||||||
'constant',
|
'string'
|
||||||
'delimiter',
|
],
|
||||||
'method',
|
'js': [
|
||||||
'modifier',
|
'comment',
|
||||||
'numeric',
|
'console',
|
||||||
'string',
|
'delimiter',
|
||||||
'type'
|
'method',
|
||||||
];
|
'modifier',
|
||||||
|
'name',
|
||||||
const hlSources = [
|
'numeric',
|
||||||
'highlight',
|
'string',
|
||||||
'source',
|
'support',
|
||||||
'text'
|
'type'
|
||||||
];
|
]
|
||||||
|
};
|
||||||
const hlTypes = [
|
|
||||||
'html',
|
|
||||||
'js',
|
|
||||||
'shell'
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts Lodash method references into documentation links.
|
* Converts Lodash method references into documentation links.
|
||||||
@@ -120,30 +115,45 @@ function repairMarkyHeaders($) {
|
|||||||
* @param {Object} $ The Cheerio object.
|
* @param {Object} $ The Cheerio object.
|
||||||
*/
|
*/
|
||||||
function tidyHighlights($) {
|
function tidyHighlights($) {
|
||||||
// Remove extraneous class names.
|
$('.highlight').each(function() {
|
||||||
$('.highlight [class]').each(function() {
|
let $spans;
|
||||||
const $element = $(this);
|
const $parent = $(this);
|
||||||
const classes = $element.attr('class').split(' ');
|
const ext = $parent.find('.source,.text').first().attr('class').split(' ').pop();
|
||||||
if (!_.isEmpty(_.intersection(classes, hlSources)) &&
|
|
||||||
!_.isEmpty(_.intersection(classes, hlTypes))) {
|
$parent.addClass(ext);
|
||||||
return;
|
|
||||||
|
// Remove line indicators for single line snippets.
|
||||||
|
$parent.children('pre').each(function() {
|
||||||
|
const $divs = $(this).children('div');
|
||||||
|
if ($divs.length == 1) {
|
||||||
|
$divs.replaceWith($divs.html());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Remove extraneous class names.
|
||||||
|
$parent.find('[class]').each(function() {
|
||||||
|
const $element = $(this);
|
||||||
|
const classes = $element.attr('class').split(' ');
|
||||||
|
const attr = _.intersection(classes, highlights[ext]).join(' ');
|
||||||
|
$element.attr('class', attr || null);
|
||||||
|
});
|
||||||
|
// Collapse nested highlights.
|
||||||
|
_.each(['comment', 'string'], function(cls) {
|
||||||
|
$parent.find(`[class~="${ cls }"] > [class~="${ cls }"]`).each(function() {
|
||||||
|
const $parent = $(this).parent();
|
||||||
|
$parent.text($parent.text());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// Collapse nested spans.
|
||||||
|
while (($spans = $parent.find('span:not([class])')).length) {
|
||||||
|
$spans.each(function() {
|
||||||
|
let $span = $(this);
|
||||||
|
while ($span[0] && $span[0].name == 'span' && !$span.attr('class')) {
|
||||||
|
const $parent = $span.parent();
|
||||||
|
$span.replaceWith($span.html());
|
||||||
|
$span = $parent;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const attr = _.intersection(classes, highlights).join(' ');
|
|
||||||
$element.attr('class', attr || null);
|
|
||||||
});
|
|
||||||
// Unwrap elements containing only text.
|
|
||||||
$('.highlight :not([class])').each(function() {
|
|
||||||
let element = $(this);
|
|
||||||
while (element && !element.children.length) {
|
|
||||||
const $element = $(element);
|
|
||||||
$element.replaceWith($element.text());
|
|
||||||
element = element.parent;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Collapse comments.
|
|
||||||
$('.highlight [class~="comment"] > [class~="comment"]').each(function() {
|
|
||||||
const $parent = $(this).parent();
|
|
||||||
$parent.text($parent.text());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user