Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions packages/core/src/Page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const MarkBind = require('../Parser');
const md = require('../lib/markdown-it');

const FsUtil = require('../utils/fsUtil');
const njUtil = require('../utils/nunjuckUtils');
const utils = require('../utils');
const logger = require('../utils/logger');

Expand Down Expand Up @@ -84,7 +83,7 @@ class Page {
* @property {string} pageTemplate template used for this page
* @property {string} title
* @property {string} titlePrefix https://markbind.org/userGuide/siteConfiguration.html#titleprefix
* @property {VariablePreprocessor} variablePreprocessor
* @property {VariableProcessor} variableProcessor
* @property {string} sourcePath the source file for rendering this page
* @property {string} tempPath the temp path for writing intermediate result
* @property {string} resultPath the output path of this page
Expand Down Expand Up @@ -178,9 +177,9 @@ class Page {
*/
this.titlePrefix = pageConfig.titlePrefix;
/**
* @type {VariablePreprocessor}
* @type {VariableProcessor}
*/
this.variablePreprocessor = pageConfig.variablePreprocessor;
this.variableProcessor = pageConfig.variableProcessor;

/**
* The source file for rendering this page
Expand Down Expand Up @@ -573,7 +572,7 @@ class Page {
}

/**
* Produces expressive layouts by inserting page data into pre-specified layout
* Renders expressive layouts by inserting page data into pre-specified layout
* @param pageData a page with its front matter collected
* @param {FileConfig} fileConfig
* @param {Parser} markbinder instance from the caller, for adding the seen sources.
Expand All @@ -590,16 +589,20 @@ class Page {
// Set expressive layout file as an includedFile
this.includedFiles.add(layoutPagePath);
return fs.readFileAsync(layoutPagePath, 'utf8')
// Include file but with altered cwf (the layout page)
// Also render MAIN_CONTENT_BODY back to itself
/*
Render {{ MAIN_CONTENT_BODY }} and {% raw/endraw %} back to itself first,
which is then dealt with in the call below to {@link renderSiteVariables}.
*/
.then(result => this.variableProcessor.renderPage(layoutPagePath, result, {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable rendering is moved out of includeFile, also in page.generate() / resolveDependency()

[LAYOUT_PAGE_BODY_VARIABLE]: `{{${LAYOUT_PAGE_BODY_VARIABLE}}}`,
}, true))
// Include file with the cwf set to the layout page path
.then(result => markbinder.includeFile(layoutPagePath, result, {
...fileConfig,
cwf: layoutPagePath,
}, {
[LAYOUT_PAGE_BODY_VARIABLE]: `{{${LAYOUT_PAGE_BODY_VARIABLE}}}`,
}))
// Insert content
.then(result => njUtil.renderRaw(result, {
// Note: The {% raw/endraw %}s previously kept are removed here.
.then(result => this.variableProcessor.renderSiteVariables(this.rootPath, result, {
[LAYOUT_PAGE_BODY_VARIABLE]: pageData,
}));
}
Expand Down Expand Up @@ -638,7 +641,7 @@ class Page {
// Set header file as an includedFile
this.includedFiles.add(headerPath);

const renderedHeader = this.variablePreprocessor.renderSiteVariables(this.sourcePath, headerContent);
const renderedHeader = this.variableProcessor.renderSiteVariables(this.sourcePath, headerContent);
return `${renderedHeader}\n${pageData}`;
}

Expand Down Expand Up @@ -667,7 +670,7 @@ class Page {
// Set footer file as an includedFile
this.includedFiles.add(footerPath);

const renderedFooter = this.variablePreprocessor.renderSiteVariables(this.sourcePath, footerContent);
const renderedFooter = this.variableProcessor.renderSiteVariables(this.sourcePath, footerContent);
return `${pageData}\n${renderedFooter}`;
}

Expand Down Expand Up @@ -699,7 +702,7 @@ class Page {
}
this.includedFiles.add(siteNavPath);

const siteNavMappedData = this.variablePreprocessor.renderSiteVariables(this.sourcePath, siteNavContent);
const siteNavMappedData = this.variableProcessor.renderSiteVariables(this.sourcePath, siteNavContent);

// Check navigation elements
const $ = cheerio.load(siteNavMappedData);
Expand Down Expand Up @@ -890,8 +893,8 @@ class Page {
// Set head file as an includedFile
this.includedFiles.add(headFilePath);

const headFileMappedData = this.variablePreprocessor.renderSiteVariables(this.sourcePath,
headFileContent).trim();
const headFileMappedData = this.variableProcessor.renderSiteVariables(this.sourcePath,
headFileContent).trim();
// Split top and bottom contents
const $ = cheerio.load(headFileMappedData);
if ($('head-top').length) {
Expand Down Expand Up @@ -944,7 +947,7 @@ class Page {
* @typedef {Object<string, any>} FileConfig
* @property {Set<string>} baseUrlMap the set of urls representing the sites' base directories
* @property {string} rootPath
* @property {VariablePreprocessor} variablePreprocessor
* @property {VariableProcessor} variableProcessor
* @property {Object<string, number>} headerIdMap
* @property {boolean} fixedHeader indicates whether the header of the page is fixed
*/
Expand All @@ -953,7 +956,7 @@ class Page {
this.includedFiles = new Set([this.sourcePath]);
this.headerIdMap = {}; // Reset for live reload
const markbinder = new MarkBind({
variablePreprocessor: this.variablePreprocessor,
variableProcessor: this.variableProcessor,
});
/**
* @type {FileConfig}
Expand All @@ -966,6 +969,7 @@ class Page {
fixedHeader: this.fixedHeader,
};
return fs.readFileAsync(this.sourcePath, 'utf-8')
.then(result => this.variableProcessor.renderPage(this.sourcePath, result))
.then(result => markbinder.includeFile(this.sourcePath, result, fileConfig))
.then((result) => {
this.collectFrontMatter(result);
Expand Down Expand Up @@ -1259,7 +1263,7 @@ class Page {
* so that we only recursively rebuild the file's included content
*/
const markbinder = new MarkBind({
variablePreprocessor: this.variablePreprocessor,
variableProcessor: this.variableProcessor,
});
/**
* @type {FileConfig}
Expand All @@ -1272,6 +1276,7 @@ class Page {
cwf: file,
};
return fs.readFileAsync(dependency.to, 'utf-8')
.then(result => this.variableProcessor.renderPage(dependency.to, result))
.then(result => markbinder.includeFile(dependency.to, result, fileConfig))
.then(result => Page.removeFrontMatter(result))
.then(result => this.collectPluginSources(result))
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cheerio.prototype.options.decodeEntities = false; // Don't escape HTML entities

class Parser {
constructor(config) {
this.variablePreprocessor = config.variablePreprocessor;
this.variableProcessor = config.variableProcessor;
this.dynamicIncludeSrc = [];
this.staticIncludeSrc = [];
this.missingIncludeSrc = [];
Expand Down Expand Up @@ -169,12 +169,12 @@ class Parser {
}
}

includeFile(file, content, config, additionalVariables = {}) {
includeFile(file, content, config) {
const context = {};
context.cwf = config.cwf || file; // current working file
context.callStack = [];
// TODO make componentPreprocessor a class to avoid this
config.variablePreprocessor = this.variablePreprocessor;
config.variableProcessor = this.variableProcessor;
return new Promise((resolve, reject) => {
const handler = new htmlparser.DomHandler((error, dom) => {
if (error) {
Expand All @@ -196,15 +196,13 @@ class Parser {
});
const parser = new htmlparser.Parser(handler);

const renderedContent = this.variablePreprocessor.renderPage(file, content, additionalVariables);

const fileExt = utils.getExt(file);
if (utils.isMarkdownFileExt(fileExt)) {
context.source = 'md';
parser.parseComplete(renderedContent.toString());
parser.parseComplete(content);
} else if (fileExt === 'html') {
context.source = 'html';
parser.parseComplete(renderedContent);
parser.parseComplete(content);
} else {
const error = new Error(`Unsupported File Extension: '${fileExt}'`);
reject(error);
Expand Down
26 changes: 13 additions & 13 deletions packages/core/src/Site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const walkSync = require('walk-sync');

const SiteConfig = require('./SiteConfig');
const Page = require('../Page');
const VariableProcessor = require('../variables/VariableProcessor');
const VariableRenderer = require('../variables/VariableRenderer');
const { ignoreTags } = require('../patches');
const VariablePreprocessor = require('../preprocessors/VariablePreprocessor');
const Template = require('../../template/template');

const FsUtil = require('../utils/fsUtil');
const njUtil = require('../utils/nunjuckUtils');
const delay = require('../utils/delay');
const logger = require('../utils/logger');
const utils = require('../utils');
Expand Down Expand Up @@ -125,7 +125,7 @@ class Site {

// Page template path
this.pageTemplatePath = path.join(__dirname, '../Page', PAGE_TEMPLATE_NAME);
this.pageTemplate = njUtil.compile(fs.readFileSync(this.pageTemplatePath, 'utf8'));
this.pageTemplate = VariableRenderer.compile(fs.readFileSync(this.pageTemplatePath, 'utf8'));
this.pages = [];

// Other properties
Expand All @@ -139,8 +139,8 @@ class Site {
this.siteConfig = undefined;
this.siteConfigPath = siteConfigPath;

// Site wide variable preprocessor
this.variablePreprocessor = undefined;
// Site wide variable processor
this.variableProcessor = undefined;

// Lazy reload properties
this.onePagePath = onePagePath;
Expand Down Expand Up @@ -278,7 +278,7 @@ class Site {
title: config.title || '',
titlePrefix: this.siteConfig.titlePrefix,
headingIndexingLevel: this.siteConfig.headingIndexingLevel,
variablePreprocessor: this.variablePreprocessor,
variableProcessor: this.variableProcessor,
sourcePath,
resultPath,
asset: {
Expand Down Expand Up @@ -520,7 +520,7 @@ class Site {
.map(x => path.resolve(this.rootPath, x));

this.baseUrlMap = new Set(candidates.map(candidate => path.dirname(candidate)));
this.variablePreprocessor = new VariablePreprocessor(this.rootPath, this.baseUrlMap);
this.variableProcessor = new VariableProcessor(this.rootPath, this.baseUrlMap);

return Promise.resolve();
}
Expand All @@ -529,7 +529,7 @@ class Site {
* Collects the user defined variables map in the site/subsites
*/
collectUserDefinedVariablesMap() {
this.variablePreprocessor.resetUserDefinedVariablesMap();
this.variableProcessor.resetUserDefinedVariablesMap();

this.baseUrlMap.forEach((base) => {
const userDefinedVariablesPath = path.resolve(base, USER_VARIABLES_PATH);
Expand All @@ -550,8 +550,8 @@ class Site {
const siteBaseUrl = siteRelativePathFromRoot === ''
? this.siteConfig.baseUrl
: path.posix.join(this.siteConfig.baseUrl || '/', siteRelativePathFromRoot);
this.variablePreprocessor.addUserDefinedVariable(base, 'baseUrl', siteBaseUrl);
this.variablePreprocessor.addUserDefinedVariable(base, 'MarkBind', MARKBIND_LINK_HTML);
this.variableProcessor.addUserDefinedVariable(base, 'baseUrl', siteBaseUrl);
this.variableProcessor.addUserDefinedVariable(base, 'MarkBind', MARKBIND_LINK_HTML);

const $ = cheerio.load(content, { decodeEntities: false });
$('variable,span').each((index, element) => {
Expand All @@ -564,13 +564,13 @@ class Site {
const jsonData = fs.readFileSync(variableFilePath);
const varData = JSON.parse(jsonData);
Object.entries(varData).forEach(([varName, varValue]) => {
this.variablePreprocessor.renderAndAddUserDefinedVariable(base, varName, varValue);
this.variableProcessor.renderAndAddUserDefinedVariable(base, varName, varValue);
});
} catch (err) {
logger.warn(`Error ${err.message}`);
}
} else {
this.variablePreprocessor.renderAndAddUserDefinedVariable(base, name, $(element).html());
this.variableProcessor.renderAndAddUserDefinedVariable(base, name, $(element).html());
}
});
});
Expand Down Expand Up @@ -1311,7 +1311,7 @@ class Site {
timeZoneName: 'short',
};
const time = new Date().toLocaleTimeString(this.siteConfig.locale, options);
this.variablePreprocessor.addUserDefinedVariableForAllSites('timestamp', time);
this.variableProcessor.addUserDefinedVariableForAllSites('timestamp', time);
return Promise.resolve();
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/preprocessors/componentPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ function _preprocessInclude(node, context, config, parser) {

const isIncludeSrcMd = _isHtmlIncludingMarkdown(element, context, filePath);

const { variablePreprocessor } = config;
const { variableProcessor } = config;
const {
renderedContent,
childContext,
} = variablePreprocessor.renderIncludeFile(actualFilePath, element, context, filePath);
} = variableProcessor.renderIncludeFile(actualFilePath, element, context, filePath);

_deleteIncludeAttributes(element);

Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/utils/nunjuckUtils.js

This file was deleted.

Loading