From 707124cbd6f0ae54924163b6a743506ece681617 Mon Sep 17 00:00:00 2001 From: Nate Silva Date: Tue, 2 Jan 2024 15:19:19 -0800 Subject: [PATCH] fix: allow publish property to be a string (ExplicitPublish) (#667) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: allow publish property to be a string (ExplicitPublish) Previously, the ExplicitPublish filter would publish if the `publish` property was truthy. The filter expects the `publish` property to be a boolean: ``` --- publish: true --- ``` However, Obsidian only shows the above if you are viewing a page in “Source” mode. If you are not in Source view, and you choose Three Dots Menu (...), “Add file property”, you will get a string, not a boolean. It seems likely that many users will do this and get: ``` publish: "true" ``` Notice that `"true"` is a string, not the boolean value `true`. If the user changes this to `"false"`, the page will still be published: ``` publish: "false" ``` That is because the string value `"false"` is truthy. This PR does the following: - Allows the `publish` property to be either a boolean or a string. - If it’s a string, it’s considered `true` if the string is `"true"` (not case-sensitive; it will also work if it is `"True"`, `"TRUE"`, etc.) - Guarantees that the returned value from `shouldPublish` is a `boolean` -- previously it could be any truthy value even though it was cast to `boolean` * style: use double-quotes everywhere * style: format according to project style guide --- quartz/plugins/filters/explicit.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/quartz/plugins/filters/explicit.ts b/quartz/plugins/filters/explicit.ts index 30f0b37f..48f92bdf 100644 --- a/quartz/plugins/filters/explicit.ts +++ b/quartz/plugins/filters/explicit.ts @@ -3,7 +3,11 @@ import { QuartzFilterPlugin } from "../types" export const ExplicitPublish: QuartzFilterPlugin = () => ({ name: "ExplicitPublish", shouldPublish(_ctx, [_tree, vfile]) { - const publishFlag: boolean = vfile.data?.frontmatter?.publish ?? false + const publishProperty = vfile.data?.frontmatter?.publish ?? false + const publishFlag = + typeof publishProperty === "string" + ? publishProperty.toLowerCase() === "true" + : Boolean(publishProperty) return publishFlag }, })