Reduce highlights further.

This commit is contained in:
John-David Dalton
2016-09-05 10:11:45 -07:00
parent d1abde7d1a
commit bc13666222

View File

@@ -11,28 +11,23 @@ const basePath = path.join(__dirname, '..', '..');
const docPath = path.join(basePath, 'doc');
const readmePath = path.join(docPath, 'README.md');
const highlights = [
'comment',
'constant',
'delimiter',
'method',
'modifier',
'numeric',
'string',
'type'
];
const hlSources = [
'highlight',
'source',
'text'
];
const hlTypes = [
'html',
'js',
'shell'
];
const highlights = {
'html': [
'string'
],
'js': [
'comment',
'console',
'delimiter',
'method',
'modifier',
'name',
'numeric',
'string',
'support',
'type'
]
};
/**
* Converts Lodash method references into documentation links.
@@ -120,30 +115,45 @@ function repairMarkyHeaders($) {
* @param {Object} $ The Cheerio object.
*/
function tidyHighlights($) {
// Remove extraneous class names.
$('.highlight [class]').each(function() {
const $element = $(this);
const classes = $element.attr('class').split(' ');
if (!_.isEmpty(_.intersection(classes, hlSources)) &&
!_.isEmpty(_.intersection(classes, hlTypes))) {
return;
$('.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.
$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());
});
}