Skip to content

Commit

Permalink
Reorganise variable processing (part 2)
Browse files Browse the repository at this point in the history
Processing page variables involves a two step process.

The first step extracts and renders locally declared page variables in
<variable> tags, using javascript proxies to force imported variables
to re-render as themselves again, which decreases code clarity.
The second step then extracts the imported variables, and stores such
variables into an alias map, then re-rendering the page a second time.

This process can be combined into a single pass to simplify the process
greatly and remove the need for workarounds like proxies.
This also increases rendering performance slightly, as only a single
pass is made

Moreover, variable tags in <include> tags are erroneously recognised as
page variables. Let's include a simple fix for this as well.
  • Loading branch information
ang-zeyu committed Jun 9, 2020
1 parent 7040ac8 commit d6a00b0
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 276 deletions.
2 changes: 0 additions & 2 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ class Site {
const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
const uniquePaths = _.uniq(filePathArray);
logger.info('Rebuilding affected source files');
this.variablePreprocessor.resetVariables();
return new Promise((resolve, reject) => {
this.regenerateAffectedPages(uniquePaths)
.then(() => fs.removeAsync(this.tempPath))
Expand All @@ -706,7 +705,6 @@ class Site {
const uniqueUrls = _.uniq(normalizedUrlArray);
uniqueUrls.forEach(normalizedUrl => logger.info(
`Building ${normalizedUrl} as some of its dependencies were changed since the last visit`));
this.variablePreprocessor.resetVariables();

/*
Lazy loading only builds the page being viewed, but the user may be quick enough
Expand Down
5 changes: 0 additions & 5 deletions src/lib/markbind/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ module.exports = {

BOILERPLATE_FOLDER_NAME: '_markbind/boilerplates',

/* Imported global variables will be assigned a namespace.
* A prefix is appended to reduce clashes with other variables in the page.
*/
IMPORTED_VARIABLE_PREFIX: '$__MARKBIND__',

// src/lib/markbind/src/utils.js
markdownFileExts: ['md', 'mbd', 'mbdf'],
};
12 changes: 3 additions & 9 deletions src/lib/markbind/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,15 @@ class Parser {
decodeEntities: true,
});

const outerContentRendered = this.variablePreprocessor.renderOuterVariables(content, file,
additionalVariables);
this.variablePreprocessor.extractInnerVariables(outerContentRendered, context.cwf)
.forEach(src => this.staticIncludeSrc.push(src));
const innerContentRendered = this.variablePreprocessor.renderInnerVariables(outerContentRendered,
file, context.cwf,
additionalVariables);
const renderedContent = this.variablePreprocessor.renderPage(file, content, additionalVariables);

const fileExt = utils.getExt(file);
if (utils.isMarkdownFileExt(fileExt)) {
context.source = 'md';
parser.parseComplete(innerContentRendered.toString());
parser.parseComplete(renderedContent.toString());
} else if (fileExt === 'html') {
context.source = 'html';
parser.parseComplete(innerContentRendered);
parser.parseComplete(renderedContent);
} else {
const error = new Error(`Unsupported File Extension: '${fileExt}'`);
reject(error);
Expand Down
15 changes: 12 additions & 3 deletions src/lib/markbind/src/preprocessors/componentPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ function _preprocessInclude(node, context, config, parser) {
const {
renderedContent,
childContext,
includedSrces,
} = variablePreprocessor.renderIncludeFile(actualFilePath, element, context, filePath);
includedSrces.forEach(src => parser.staticIncludeSrc.push(src));

_deleteIncludeAttributes(element);

Expand Down Expand Up @@ -317,6 +315,16 @@ function _preprocessVariables() {
return utils.createEmptyNode();
}

function _preprocessImports(node, parser) {
if (node.attribs.from) {
parser.staticIncludeSrc.push({
from: node.attribs.cwf,
to: path.resolve(node.attribs.cwf, node.attribs.from),
});
}

return utils.createEmptyNode();
}

/*
* Body
Expand All @@ -343,8 +351,9 @@ function preProcessComponent(node, context, config, parser) {
case 'panel':
return _preProcessPanel(element, context, config, parser);
case 'variable':
case 'import':
return _preprocessVariables();
case 'import':
return _preprocessImports(node, parser);
case 'include':
return _preprocessInclude(element, context, config, parser);
case 'body':
Expand Down
Loading

0 comments on commit d6a00b0

Please sign in to comment.