From 32a7e792804d5537d33dd7180e438a8854d391da Mon Sep 17 00:00:00 2001 From: jamos-tay Date: Fri, 8 Feb 2019 17:51:36 +0800 Subject: [PATCH] Inline include variables --- docs/userGuide/syntax/includes.mbdf | 10 ++++++++++ src/lib/markbind/src/parser.js | 16 ++++++++++++++++ test/functional/test_site/expected/index.html | 3 +++ test/functional/test_site/expected/siteData.json | 1 + test/functional/test_site/index.md | 2 +- .../functional/test_site/testIncludeVariables.md | 4 ++++ 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/userGuide/syntax/includes.mbdf b/docs/userGuide/syntax/includes.mbdf index c1bcca0698..e79fc9db45 100644 --- a/docs/userGuide/syntax/includes.mbdf +++ b/docs/userGuide/syntax/includes.mbdf @@ -138,6 +138,16 @@ Author: {{ author }} These variables work the same way as variables in `_markbind/variables.md`, except that they only apply to the included file. They allow the included file to be reused as a template, for different source files using different variable values. +You can also specify include variables within the `` tag itself by adding a `var-` prefix. + +
+ +{{ icon_example }} Specifying `title` and `author` variables inline: +```html + +``` +
+ If the same variable is defined in a chain of ``s (e.g. `a.md` includes `b.md` includes `c.md`...), variables defined in the top-most `` will take precedence. Global variables (`_markbind/variables.md`) will take precedence over any `` variables. ### Using Boilerplate Files diff --git a/src/lib/markbind/src/parser.js b/src/lib/markbind/src/parser.js index c59a79bbb8..4ee00a843b 100644 --- a/src/lib/markbind/src/parser.js +++ b/src/lib/markbind/src/parser.js @@ -94,6 +94,15 @@ function Parser(options) { */ function extractIncludeVariables(includeElement, contextVariables) { const includedVariables = { ...contextVariables }; + Object.keys(includeElement.attribs).forEach((attribute) => { + if (!attribute.startsWith('var-')) { + return; + } + const variableName = attribute.replace(/^var-/, ''); + if (!includedVariables[variableName]) { + includedVariables[variableName] = includeElement.attribs[attribute]; + } + }); if (includeElement.children) { includeElement.children.forEach((child) => { if (child.name !== 'span') { @@ -267,6 +276,13 @@ Parser.prototype._preprocess = function (node, context, config) { // Render inner file content fileContent = nunjucks.renderString(fileContent, { ...allVariables, ...userDefinedVariables }); + // Delete variable attributes in include + Object.keys(element.attribs).forEach((attribute) => { + if (attribute.startsWith('var-')) { + delete element.attribs[attribute]; + } + }); + delete element.attribs.boilerplate; delete element.attribs.src; delete element.attribs.inline; diff --git a/test/functional/test_site/expected/index.html b/test/functional/test_site/expected/index.html index 7f7ea81beb..5a75309fc1 100644 --- a/test/functional/test_site/expected/index.html +++ b/test/functional/test_site/expected/index.html @@ -94,6 +94,7 @@ Fragment with leading spaces and newline‎ Include with custom variables‎ + Test included variable as include attribute‎ Test included variable‎ Test included variable with markdown‎ Test included variable as attribute‎ @@ -433,6 +434,8 @@

Trimmed includeFragment with leading spaces and newline

Include with custom variables

+

Test included variable as include attribute

+

Included variable as include attribute

Test included variable

Included variable

Test included variable with markdown

diff --git a/test/functional/test_site/expected/siteData.json b/test/functional/test_site/expected/siteData.json index e125636b7d..8cf092671a 100644 --- a/test/functional/test_site/expected/siteData.json +++ b/test/functional/test_site/expected/siteData.json @@ -81,6 +81,7 @@ "trimmed-include": "Trimmed include", "fragment-with-leading-spaces-and-newline": "Fragment with leading spaces and newline", "include-with-custom-variables": "Include with custom variables", + "test-included-variable-as-include-attribute": "Test included variable as include attribute", "test-included-variable": "Test included variable", "test-included-variable-with-markdown": "Test included variable with markdown", "test-included-variable-as-attribute": "Test included variable as attribute", diff --git a/test/functional/test_site/index.md b/test/functional/test_site/index.md index 5dd5bc3a84..fcdc3f6717 100644 --- a/test/functional/test_site/index.md +++ b/test/functional/test_site/index.md @@ -133,7 +133,7 @@ head: myCustomHead.md, myCustomHead2.md # Include with custom variables - + Included variable __**Included variable with markdown**__ color: blue diff --git a/test/functional/test_site/testIncludeVariables.md b/test/functional/test_site/testIncludeVariables.md index 6dfda12238..e7a52ba2bf 100644 --- a/test/functional/test_site/testIncludeVariables.md +++ b/test/functional/test_site/testIncludeVariables.md @@ -1,3 +1,7 @@ + +# Test included variable as include attribute +{{ included_variable_as_include_attribute }} + # Test included variable {{ included_variable }}