Uh oh!
There was an error while loading. Please reload this page.
Support variables to be defined in JSON - #1117
Conversation
openorclose
commented
Mar 13, 2020
Is there any other place where Trying to read <variabletype="json">
{
"front": "<div> front",
"back": "back </div>"
}
</variable>sounds like "I'm creating a variable of type 'json' " to me, and not "I'm creating multiple variables whose names and values are defined in this json object". |
damithc
commented
Mar 13, 2020
How about this? <variables>
{
"front": "<div> front",
"back": "back </div>"
}
</variables>
<variables>
"front": "<div> front",
"back": "back </div>"
</variables>
|
acjh
commented
Mar 13, 2020
How about introducing a variables.json file for arbitrary strings? I suspect why we had variables.md was to support valid HTML. <variabletype="json">
" { "front": ""
<div> front", "back": "back </div>
"" } "
</variable>A limitation is that we have to define an order for which file is processed first (likely variables.json), which restricts which file can use variables in the other. |
damithc
commented
Mar 13, 2020
variables.json would have the global scope right? While having one would be good, it doesn't serve cases where the variable needs to be in local scope. e.g., an ugly partial html fragment that needs to be repeated several times in a particular page but is not used anywhere else. |
acjh
commented
Mar 13, 2020
(Maybe I forgot something) but isn't variables.md scoped similarly? |
damithc
commented
Mar 13, 2020
It is. I thought this new feature is meant to be used in both variables.md and as page variables. @crphang does it work like that? |
True. Perhaps renaming the attribute might help such as
Yes that was the intention. However, since the file is still regarded as a |
ang-zeyu
commented
Mar 13, 2020
If the aim is to allow any content inside
|
acjh
commented
Mar 13, 2020
@crphang The test that you have added for HTML happens to close itself. |
crphang
commented
Mar 18, 2020
Letting variables be defined as Json within the page will cause issue if the HTML fragments are not self-closing as @acjh suggests. I took another approach to allow user to json variables define in separate json file.
We could further add in user guide in tips and tricks section about allowing malformed html fragments in json variables if user requires. |
damithc
commented
Mar 18, 2020
Remember to update user docs too. |
crphang
commented
Mar 18, 2020
Updated user guide. Ready for review. |
crphang
commented
Mar 21, 2020
@yamgent could you help with the review of this? 🙇 |
8b30e5e to
91f2949Comparecrphang
commented
Mar 23, 2020
@marvinchin could you help review this? 🙇♂️ |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
@marvinchin thanks for reviewing! Updated with the suggested changes 😄 |
| console.warn(`Missing 'name' for variable in ${fileName}\n`); | ||
| return; | ||
| } | ||
| const variableValue = nunjuckUtils.renderEscaped( |
There was a problem hiding this comment.
Do you feel it makes sense to add this into the abstracted function too?
const variableValue = nunjuckUtils.renderEscaped(
nunjucks, md.renderInline(variableElement.html()), {
...importedVariables, ...pageVariables, ...userDefinedVariables, ...includedVariables,
});
There was a problem hiding this comment.
Or perhaps, would it make sense to define an inline function? This way we can get the benefits of abstraction while still being able to access the variables we need directly. We could do something like:
const processVariable = (variableName, rawVariableValue) => {
const otherVariables = { ...importedVariables, ...pageVariables, ...userDefinedVariables, ...includedVariables };
const variableValue = nunjckUtils.renderEscaped(nunjucks, rawVariableValue, otherVariables);
if (!pageVariables[variableName]) {
pageVariables[variableName] = variableValue;
Parser.VARIABLE_LOOKUP.get(fileName).set(variableName, variableValue);
}
}
crphang
commented
Mar 28, 2020
@marvinchin Thanks for the suggestion, I refactored to make the logic cleaner as suggested. 👍 |
…bind into remove-fixed-bugs * 'remove-fixed-bugs' of https://github.com/Tejas2805/markbind: Docs: Add contributing.md (MarkBind#1139) Show pointer and use underline dashed for click trigger (MarkBind#1111) Support variables to be defined as by JSON (MarkBind#1117) Allow an array for specifying page src or glob (MarkBind#1118) Code blocks: Implement inline markdown support for heading (MarkBind#1137) Fix lazy reload for urls without index (MarkBind#1110) Changes remaining references from filterTags to tags (MarkBind#1149)
What is the purpose of this pull request? (put "X" next to an item, remove the rest)
Resolves#319
• [X] Enhancement to an existing feature
What is the rationale for this request?
Allow variable to be defined in JSON. This allows us to define variable in different HTML fragments such as:
What changes did you make? (Give an overview)
Added an additional logic to handle variable defined in JSON.
Provide some example code that this change will affect:
Is there anything you'd like reviewers to focus on?
One of the attempts before relying on a common syntax like JSON was to enable nunjucks variables. However, there is no easy way to retrieve variables from nunjucks because of it's design: mozilla/nunjucks#329.
Testing instructions:
npm run testProposed commit message: (wrap lines at 72 characters)
Support variables to be defined in JSON.