From 8db28db16ab58e060ca3aa2c1c3327511de61007 Mon Sep 17 00:00:00 2001 From: Jamos Tay Date: Sat, 5 Jan 2019 20:19:19 +0800 Subject: [PATCH 01/10] Allow specifying variables in include --- docs/userGuide/reusingContents.md | 21 ++++++++++++++++ src/lib/markbind/src/parser.js | 25 +++++++++++++++++++ test/test_site/_markbind/variables.md | 2 ++ test/test_site/expected/index.html | 25 +++++++++++++++++++ test/test_site/expected/siteData.json | 7 ++++++ test/test_site/index.md | 11 ++++++++ test/test_site/testIncludeVariables.md | 18 +++++++++++++ .../testIncludeVariablesIncludedFile.md | 1 + 8 files changed, 110 insertions(+) create mode 100644 test/test_site/testIncludeVariables.md create mode 100644 test/test_site/testIncludeVariablesIncludedFile.md diff --git a/docs/userGuide/reusingContents.md b/docs/userGuide/reusingContents.md index c76e4c8534..c6a5e91044 100644 --- a/docs/userGuide/reusingContents.md +++ b/docs/userGuide/reusingContents.md @@ -259,6 +259,27 @@ In other words, **`` interprets the reused code relative to the origina
+#### Specifying Variables in the Include Tag + +**It is possible to include variables in the include tag.** For example, + +```html + + My Title + John Doe + +``` + +In `article.md`: + + +# {{ title }} + +Author: {{ author }} + + +These variables work the same way as variables in `_markbind/variables.md`, except they only apply to the included file. This can be helpful for creating a template for multiple files. + ## Using Boilerplate Files **If you find duplicating a _boilerplate code_ fragment in multiple places of your code base, you can use a `boilerplate` file to avoid such duplication.** Note that you cannot use a normal `` in this case because the code included using a normal `` stays relative to the original location while boilerplate code needs to be interpreted relative to the location it is being used. diff --git a/src/lib/markbind/src/parser.js b/src/lib/markbind/src/parser.js index b627688961..6eff9aa3af 100644 --- a/src/lib/markbind/src/parser.js +++ b/src/lib/markbind/src/parser.js @@ -205,6 +205,30 @@ Parser.prototype._preprocess = function (node, context, config) { let fileContent = self._fileCache[actualFilePath]; // cache the file contents to save some I/O const { parent, relative } = calculateNewBaseUrls(filePath, config.rootPath, config.baseUrlMap); const userDefinedVariables = config.userDefinedVariablesMap[path.resolve(parent, relative)]; + const includedVariables = {}; + + if (element.children) { + element.children.forEach((child) => { + if (child.name !== 'span') { + return; + } + if (!child.attribs.id) { + // eslint-disable-next-line no-console + console.warn(`Missing 'id' in variable for ${element.attribs.src} include.`); + return; + } + const variableValue = cheerio.html(child.children); + userDefinedVariables[child.attribs.id] = variableValue; + includedVariables[child.attribs.id] = variableValue; + }); + } + + if (context.includedVariables) { + Object.keys(context.includedVariables).forEach((key) => { + userDefinedVariables[key] = context.includedVariables[key]; + }); + } + fileContent = nunjucks.renderString(fileContent, userDefinedVariables); delete element.attribs.boilerplate; delete element.attribs.src; @@ -265,6 +289,7 @@ Parser.prototype._preprocess = function (node, context, config) { const childContext = _.cloneDeep(context); childContext.cwf = filePath; childContext.source = isIncludeSrcMd ? 'md' : 'html'; + childContext.includedVariables = includedVariables; if (element.children && element.children.length > 0) { element.children = element.children.map(e => self._preprocess(e, childContext, config)); } diff --git a/test/test_site/_markbind/variables.md b/test/test_site/_markbind/variables.md index e8c5806bbe..c19098e5f2 100644 --- a/test/test_site/_markbind/variables.md +++ b/test/test_site/_markbind/variables.md @@ -7,3 +7,5 @@ {{reference_level_1}} {{reference_level_2}} {{reference_level_3}} + +Should be overriden diff --git a/test/test_site/expected/index.html b/test/test_site/expected/index.html index ebca9c40ac..fbd0a11e33 100644 --- a/test/test_site/expected/index.html +++ b/test/test_site/expected/index.html @@ -69,6 +69,13 @@ + Include with custom variables‎ + Test included variable‎ + Test included variable with markdown‎ + Test included variable as attribute‎ + Test included variable as html element‎ + Test included variable overriding variables.md‎ + Test included variable in included file‎ Panel without src‎