Repository navigation
fix(#173): do not HTML-escape template variables in markdown notes - #186
Open
muditagrawal2007 wants to merge 1 commit into
Open
muditagrawal2007 wants to merge 1 commit into
muditagrawal2007 wants to merge 1 commit into
Conversation
…ariables
Templates render into markdown notes, not HTML. With no options,
Handlebars.compile runs every {{ var }} through escapeExpression and
encodes ' " & < > into HTML entities. That mangles:
* the note body (template_title / template_tags users hit it everywhere)
* template_title (so the title in the note list carries the entities)
* template_tags
Joplin's markdown viewer renders the entities correctly so the bug is
silent. It only surfaces when the user edits the note, searches it,
exports it, or syncs it elsewhere.
Pass { noEscape: true } to both compile calls (parser.ts:137 and
parser.ts:286). Under noEscape, {{ var }} behaves like {{{ var }}},
so existing templates that already use the triple-stache workaround
keep working unchanged.
Three regression tests added in tests/parser.spec.ts:
* apostrophes, quotes, ampersands, <markup> in text/number/dropdown
* template_title and template_tags round-trip
* {{{ var }}} still works identically under noEscape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Templates render into markdown notes, not HTML. Both
Handlebars.compilecalls insrc/parser.tswere called with default options, which madeescapeExpressionrun on every{{ var }}and encode' " & < >to HTML entities.That mangles:
text,number,boolean,date,time, anddropdownvariable.template_title— so the entry in the notebook list readsit's a "test".template_tags— same.The bug is silent: Joplin's markdown viewer renders the entities correctly, so the rendered note looks fine. The corruption lives in the source. It only surfaces when the user edits, searches, exports, or syncs the note elsewhere.
Fix
Pass
{ noEscape: true }to bothHandlebars.compilecalls:Under
noEscape,{{ var }}behaves exactly like{{{ var }}}. So:{{{ var }}}) — both forms produce the same output.noEscapealone is sufficient.Reproduction (before the fix)
Enter
it's a "test"in the dialog.it's a "test"it's a "test"Same with
template_title: {{ note_text }}— the title becomesit's a "test".Risk
None visible:
template_title, and the tag list (which already runs throughsplit(",").map(trim).filter(...)ingetNoteMetadata).noEscapeonly turns off escaping that was wrong for this medium.Tests
Three regression tests added to
tests/parser.spec.ts:should not HTML-escape text, number and dropdown variables in the body (#173)—text_var = "it's a \"test\" with & <markup>"renders verbatim; explicitnot.toContain("'")/"/</>assertions on the body.should not HTML-escape template_title and template_tags (#173)— verifies the title and tag list round-trip, withnot.toMatch(/&(amp|quot|#x27|lt|gt);/)on all three.should still support the {{{ var }}} triple-stache under noEscape (#173)— confirms{{and{{{produce identical output, so existing templates that already use the workaround are unaffected.Verification
npm test— 104/104 pass (101 existing + 3 new).npm run lint— 0 errors introduced (only pre-existing warnings on unrelated files).npm run dist— plugin archive builds cleanly.Related
$sequences in special variable values are mangled by preProcessTemplateBody #184 — independentpreProcessTemplateBodyfrontmatter-quoting bugs. Not related.