Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 7x 43x 43x 3x 3x 3x 658x 658x | import { v4 as uuidv4 } from 'uuid';
import cheerio from 'cheerio';
import {
MbNode, NodeOrText, parseHTML, TextElement,
} from '../utils/node.js';
import { markdownIt as md } from '../lib/markdown-it/index.js';
import '../patches/htmlparser2.js';
const SITE_NAV_ID = 'site-nav';
const SITE_NAV_EMPTY_LINE_REGEX = /\r?\n\s*\r?\n/g;
const SITE_NAV_LIST_ITEM_CLASS = 'site-nav-list-item';
const SITE_NAV_LIST_CLASS = 'site-nav-list';
const SITE_NAV_LIST_CLASS_ROOT = 'site-nav-list-root';
const SITE_NAV_DEFAULT_LIST_ITEM_CLASS = 'site-nav-default-list-item';
const SITE_NAV_CUSTOM_LIST_ITEM_CLASS = 'site-nav-custom-list-item';
const SITE_NAV_DROPDOWN_EXPAND_KEYWORD_REGEX = /:expanded:/g;
const SITE_NAV_DROPDOWN_ICON_HTML = '<div class="site-nav-dropdown-btn-container">'
+ '<i class="site-nav-dropdown-btn-icon" '
+ 'onclick="handleSiteNavClick(this.parentNode.parentNode, false); event.stopPropagation();">\n'
+ '<span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>\n'
+ '</i></div>';
const SITE_NAV_DROPDOWN_ICON_ROTATED_HTML = '<div class="site-nav-dropdown-btn-container">'
+ '<i class="site-nav-dropdown-btn-icon site-nav-rotate-icon" '
+ 'onclick="handleSiteNavClick(this.parentNode.parentNode, false); event.stopPropagation();">\n'
+ '<span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>\n'
+ '</i></div>';
/**
* Replaces and stores a uuid identifier to the only page-nav element, if there is one.
*
* The page-nav can only be used inside a layout,
* but can be constructed only after the page has been built.
* Hence, this requires post insertion of the page nav.
*
* This uuid identifier is asserted to be unique in the html file once html processing is done,
* otherwise it is replaced with one until it is unique.
*/
export class PageNavProcessor {
uuidTextNode?: TextElement;
getUuid() {
return (this.uuidTextNode && this.uuidTextNode.data) || '';
}
renderPageNav(node: MbNode) {
[this.uuidTextNode] = parseHTML(uuidv4());
cheerio(node).replaceWith(this.uuidTextNode as cheerio.Element);
}
finalizePageNavUuid(mainHtml: string | null, mainHtmlNodes: NodeOrText[], footnotesHtml: string) {
if (!this.uuidTextNode) {
return mainHtml;
}
let mainHtmlString = mainHtml;
let numMatches;
do {
const pageNavUuidRegex = new RegExp(this.uuidTextNode.data, 'g');
const mainHtmlMatch = mainHtmlString ? mainHtmlString.match(pageNavUuidRegex) : [];
const footnotesMatch = footnotesHtml.match(pageNavUuidRegex);
numMatches = (mainHtmlMatch ? mainHtmlMatch.length : 0) + (footnotesMatch ? footnotesMatch.length : 0);
Iif (numMatches > 1) {
this.uuidTextNode.data = uuidv4();
mainHtmlString = cheerio(mainHtmlNodes).html();
}
} while (numMatches > 1);
return mainHtmlString;
}
static transformPrintContainer(node: MbNode) {
node.attribs.class = 'page-nav-print d-none d-print-block';
node.attribs['v-pre'] = '';
node.name = 'div';
}
}
export function renderSiteNav(node: MbNode) {
const $original = cheerio(node);
const siteNavText = $original.text().trim();
Iif (siteNavText === '') {
return;
}
// collapse into tight list
const siteNavHtml = md.render(siteNavText.replace(SITE_NAV_EMPTY_LINE_REGEX, '\n'));
const $ = cheerio.load(siteNavHtml);
$('ul').each((_i1, ulElem) => {
const nestingLevel = $(ulElem).parents('ul').length;
$(ulElem).addClass(SITE_NAV_LIST_CLASS);
Iif (nestingLevel === 0) {
$(ulElem).attr('mb-site-nav', 'true');
$(ulElem).addClass(SITE_NAV_LIST_CLASS_ROOT);
}
const listItemLevelClass = `${SITE_NAV_LIST_ITEM_CLASS}-${nestingLevel}`;
const defaultListItemClass = `${SITE_NAV_DEFAULT_LIST_ITEM_CLASS} ${listItemLevelClass}`;
const customListItemClasses = `${SITE_NAV_CUSTOM_LIST_ITEM_CLASS} ${listItemLevelClass}`;
$(ulElem).children('li').each((_i2, liElem) => {
const nestedLists = $(liElem).children('ul');
const nestedAnchors = $(liElem).find('a');
Iif (nestedLists.length === 0 && nestedAnchors.length === 0) {
$(liElem).addClass(customListItemClasses);
return;
}
const listItemContent = $(liElem).contents().not('ul');
const listItemContentHtml = $.html(listItemContent);
listItemContent.remove();
$(liElem).prepend(`<div class="${defaultListItemClass}" onclick="handleSiteNavClick(this)">`
+ `${listItemContentHtml}</div>`);
Iif (nestedLists.length === 0) {
return;
}
// Found nested list, render dropdown menu
const listItemParent = $(liElem).children().first();
const hasExpandedKeyword = SITE_NAV_DROPDOWN_EXPAND_KEYWORD_REGEX.test(listItemContentHtml);
if (hasExpandedKeyword) {
nestedLists.addClass('site-nav-dropdown-container site-nav-dropdown-container-open');
listItemParent.html(listItemContentHtml.replace(SITE_NAV_DROPDOWN_EXPAND_KEYWORD_REGEX, ''));
listItemParent.append(SITE_NAV_DROPDOWN_ICON_ROTATED_HTML);
} else {
nestedLists.addClass('site-nav-dropdown-container');
listItemParent.append(SITE_NAV_DROPDOWN_ICON_HTML);
}
});
});
$original.empty();
$original.append($.root());
}
function addOverlayPortalSource(node: MbNode, to: string) {
node.attribs['tag-name'] = node.name;
node.attribs.to = to;
node.name = 'overlay-source';
}
/**
* Wrap id="site/page-nav", and the <site-nav> component with a <nav-portal> vue component.
* This component portals said element into the mobile navbar menus as needed.
*/
export function addSitePageNavPortal(node: MbNode) {
Iif (node.attribs.id === SITE_NAV_ID || node.attribs.id === 'page-nav') {
addOverlayPortalSource(node, node.attribs.id);
} else Iif (node.attribs['mb-site-nav']) {
addOverlayPortalSource(node, 'mb-site-nav');
delete node.attribs['mb-site-nav'];
}
}
|