jgm / lcmark

Flexible CommonMark converter
BSD 2-Clause "Simplified" License
55 stars 6 forks source link

Falsy values are evaluated incorrectly in templates #8

Closed RiskoZoSlovenska closed 2 years ago

RiskoZoSlovenska commented 2 years ago

According to the README, values passed to template conditionals such as $if(value)$ are considered falsy if they're nil, false or an empty table ({}). However, this is not the case:

local lcmark = require("lcmark")

local markdown = [[
---
aFalse: false
aNull: null
empty: []
---

etc
]]

local template = [[
False: $if(aFalse)$Truthy$else$Falsy$endif$
Null: $if(aNull)$Truthy$else$Falsy$endif$
Empty: $if(empty)$Truthy$else$Falsy$endif$
Nonexistent: $if(nonexistent)$Truthy$else$Falsy$endif$
]]

local body, meta, err = lcmark.convert(markdown, "html", {
    yaml_metadata = true
})

assert(body, err)

print(lcmark.render_template(template, meta))

Expected output:

False: Falsy
Null: Falsy
Empty: Falsy
Nonexistent: Falsy

Actual output:

False: Truthy
Null: Truthy
Empty: Truthy
Nonexistent: Falsy

This also affects tags such as $for(value)$.

RiskoZoSlovenska commented 2 years ago

Could you please re-open this? It isn't quite fixed yet; with https://github.com/jgm/lcmark/pull/9 merged, the output for the above sample is

False: Truthy
Null: Truthy
Empty: Falsy
Nonexistent: Falsy
RiskoZoSlovenska commented 2 years ago

With https://github.com/jgm/lcmark/pull/9, https://github.com/jgm/lcmark/pull/10 and https://github.com/jgm/lcmark/pull/11 merged, this has been resolved.