diff --git a/src/Page.js b/src/Page.js index ef8680e7cc..8ec1d80dbd 100644 --- a/src/Page.js +++ b/src/Page.js @@ -52,1104 +52,1189 @@ const { cheerio.prototype.options.xmlMode = true; // Enable xml mode for self-closing tag cheerio.prototype.options.decodeEntities = false; // Don't escape HTML entities -function Page(pageConfig) { - this.asset = pageConfig.asset; - this.baseUrl = pageConfig.baseUrl; - this.baseUrlMap = pageConfig.baseUrlMap; - this.content = pageConfig.content || ''; - this.faviconUrl = pageConfig.faviconUrl; - this.frontmatterOverride = pageConfig.frontmatter || {}; - this.layout = pageConfig.layout; - this.layoutsAssetPath = pageConfig.layoutsAssetPath; - this.rootPath = pageConfig.rootPath; - this.enableSearch = pageConfig.enableSearch; - this.globalOverride = pageConfig.globalOverride; - this.plugins = pageConfig.plugins; - this.pluginsContext = pageConfig.pluginsContext; - this.searchable = pageConfig.searchable; - this.src = pageConfig.src; - this.template = pageConfig.pageTemplate; - this.title = pageConfig.title || ''; - this.titlePrefix = pageConfig.titlePrefix; - this.userDefinedVariablesMap = pageConfig.userDefinedVariablesMap; - - // the source file for rendering this page - this.sourcePath = pageConfig.sourcePath; - // the temp path for writing intermediate result - this.tempPath = pageConfig.tempPath; - // the output path of this page - this.resultPath = pageConfig.resultPath; - - this.frontMatter = {}; - this.headFileBottomContent = ''; - this.headFileTopContent = ''; - this.headings = {}; - this.headingIndexingLevel = pageConfig.headingIndexingLevel; - this.includedFiles = new Set(); - this.pluginSourceFiles = new Set(); - this.keywords = {}; - this.navigableHeadings = {}; - this.pageSectionsHtml = {}; - - // Flag to indicate whether this page has a site nav - this.hasSiteNav = false; -} - -/** - * Util Methods - */ - -function addContentWrapper(pageData) { - const $ = cheerio.load(pageData); - $(`#${CONTENT_WRAPPER_ID}`).removeAttr('id'); - return `
\n\n` - + `${$.html()}\n` - + '
'; -} - -function calculateNewBaseUrl(filePath, root, lookUp) { - function calculate(file, result) { - if (file === root || !pathIsInside(file, root)) { - return undefined; - } - const parent = path.dirname(file); - if (lookUp.has(parent) && result.length === 1) { - return path.relative(root, result[0]); - } else if (lookUp.has(parent)) { - return calculate(parent, [parent]); - } - return calculate(parent, result); - } - - return calculate(filePath, []); -} +class Page { + constructor(pageConfig) { + this.asset = pageConfig.asset; + this.baseUrl = pageConfig.baseUrl; + this.baseUrlMap = pageConfig.baseUrlMap; + this.content = pageConfig.content || ''; + this.faviconUrl = pageConfig.faviconUrl; + this.frontmatterOverride = pageConfig.frontmatter || {}; + this.layout = pageConfig.layout; + this.layoutsAssetPath = pageConfig.layoutsAssetPath; + this.rootPath = pageConfig.rootPath; + this.enableSearch = pageConfig.enableSearch; + this.globalOverride = pageConfig.globalOverride; + this.plugins = pageConfig.plugins; + this.pluginsContext = pageConfig.pluginsContext; + this.searchable = pageConfig.searchable; + this.src = pageConfig.src; + this.template = pageConfig.pageTemplate; + this.title = pageConfig.title || ''; + this.titlePrefix = pageConfig.titlePrefix; + this.userDefinedVariablesMap = pageConfig.userDefinedVariablesMap; + + // the source file for rendering this page + this.sourcePath = pageConfig.sourcePath; + // the temp path for writing intermediate result + this.tempPath = pageConfig.tempPath; + // the output path of this page + this.resultPath = pageConfig.resultPath; + + this.frontMatter = {}; + this.headFileBottomContent = ''; + this.headFileTopContent = ''; + this.headings = {}; + this.headingIndexingLevel = pageConfig.headingIndexingLevel; + this.includedFiles = new Set(); + this.pluginSourceFiles = new Set(); + this.keywords = {}; + this.navigableHeadings = {}; + this.pageSectionsHtml = {}; -function removePageHeaderAndFooter(pageData) { - const $ = cheerio.load(pageData); - const pageHeaderAndFooter = $('header', 'footer'); - if (pageHeaderAndFooter.length === 0) { - return pageData; + // Flag to indicate whether this page has a site nav + this.hasSiteNav = false; } - // Remove preceding footers - pageHeaderAndFooter.remove(); - return $.html(); -} -function formatSiteNav(renderedSiteNav, src) { - const $ = cheerio.load(renderedSiteNav); - const listItems = $.root().find('ul').first().children(); - if (listItems.length === 0) { - return renderedSiteNav; + prepareTemplateData() { + const prefixedTitle = this.titlePrefix + ? this.titlePrefix + (this.title ? TITLE_PREFIX_SEPARATOR + this.title : '') + : this.title; + // construct temporary asset object with only POSIX-style paths + const asset = {}; + Object.entries(this.asset) + .forEach(([key, value]) => { + asset[key] = _.isString(value) ? ensurePosix(value) : value; + }); + return { + asset, + baseUrl: this.baseUrl, + content: this.content, + faviconUrl: this.faviconUrl, + footerHtml: this.pageSectionsHtml.footer || '', + headerHtml: this.pageSectionsHtml.header || '', + headFileBottomContent: this.headFileBottomContent, + headFileTopContent: this.headFileTopContent, + markBindVersion: `MarkBind ${CLI_VERSION}`, + pageNav: this.isPageNavigationSpecifierValid(), + pageNavHtml: this.pageSectionsHtml[`#${PAGE_NAV_ID}`] || '', + siteNav: this.hasSiteNav, + siteNavHtml: this.pageSectionsHtml[`#${SITE_NAV_ID}`] || '', + title: prefixedTitle, + enableSearch: this.enableSearch, + }; } - // Tidy up the style of the unordered list