-
Notifications
You must be signed in to change notification settings - Fork 142
Support variables to be defined in JSON #1117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
alyip98
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
Is there any other place where Trying to read <variable type="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". |
|
How about this? <variables>
{
"front": "<div> front",
"back": "back </div>"
}
</variables>
<variables>
"front": "<div> front",
"back": "back </div>"
</variables>
|
|
How about introducing a variables.json file for arbitrary strings? I suspect why we had variables.md was to support valid HTML. <variable type="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. |
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. |
|
(Maybe I forgot something) but isn't variables.md scoped similarly? |
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 |
|
If the aim is to allow any content inside
|
@crphang The test that you have added for HTML happens to close itself. |
|
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. |
|
Remember to update user docs too. |
|
Updated user guide. Ready for review. |
|
@yamgent could you help with the review of this? 🙇 |
8b30e5e to
91f2949
Compare
|
@marvinchin could you help review this? 🙇♂️ |
marvinchin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of suggestions!
|
@marvinchin thanks for reviewing! Updated with the suggested changes 😄 |
src/lib/markbind/src/parser.js
Outdated
| console.warn(`Missing 'name' for variable in ${fileName}\n`); | ||
| return; | ||
| } | ||
| const variableValue = nunjuckUtils.renderEscaped( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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);
}
}
|
@marvinchin Thanks for the suggestion, I refactored to make the logic cleaner as suggested. 👍 |
marvinchin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
…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.