mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +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 readmePath = path.join(docPath, 'README.md');
|
||||
|
||||
const highlights = [
|
||||
const highlights = {
|
||||
'html': [
|
||||
'string'
|
||||
],
|
||||
'js': [
|
||||
'comment',
|
||||
'constant',
|
||||
'console',
|
||||
'delimiter',
|
||||
'method',
|
||||
'modifier',
|
||||
'name',
|
||||
'numeric',
|
||||
'string',
|
||||
'support',
|
||||
'type'
|
||||
];
|
||||
|
||||
const hlSources = [
|
||||
'highlight',
|
||||
'source',
|
||||
'text'
|
||||
];
|
||||
|
||||
const hlTypes = [
|
||||
'html',
|
||||
'js',
|
||||
'shell'
|
||||
];
|
||||
]
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts Lodash method references into documentation links.
|
||||
@@ -120,31 +115,46 @@ function repairMarkyHeaders($) {
|
||||
* @param {Object} $ The Cheerio object.
|
||||
*/
|
||||
function tidyHighlights($) {
|
||||
$('.highlight').each(function() {
|
||||
let $spans;
|
||||
const $parent = $(this);
|
||||
const ext = $parent.find('.source,.text').first().attr('class').split(' ').pop();
|
||||
|
||||
$parent.addClass(ext);
|
||||
|
||||
// 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.
|
||||
$('.highlight [class]').each(function() {
|
||||
$parent.find('[class]').each(function() {
|
||||
const $element = $(this);
|
||||
const classes = $element.attr('class').split(' ');
|
||||
if (!_.isEmpty(_.intersection(classes, hlSources)) &&
|
||||
!_.isEmpty(_.intersection(classes, hlTypes))) {
|
||||
return;
|
||||
}
|
||||
const attr = _.intersection(classes, highlights).join(' ');
|
||||
const attr = _.intersection(classes, highlights[ext]).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() {
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user