Gérer le statut draft pour Lemmy et la météo
This commit is contained in:
71
tools/lib/publication.js
Normal file
71
tools/lib/publication.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Interprète une valeur booléenne potentiellement sérialisée.
|
||||
* @param {unknown} value Valeur brute issue du frontmatter.
|
||||
* @returns {boolean|null} true/false si interprétable, sinon null.
|
||||
*/
|
||||
function parseBoolean(value) {
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (!normalized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (normalized === "true" || normalized === "1" || normalized === "yes" || normalized === "on") {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "off") {
|
||||
return false;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Détermine si la valeur `draft` représente un brouillon.
|
||||
* @param {unknown} value Valeur brute de l'attribut `draft`.
|
||||
* @returns {boolean} true si l'article est un brouillon.
|
||||
*/
|
||||
function isDraftValue(value) {
|
||||
return parseBoolean(value) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indique si un frontmatter objet correspond à un article publié.
|
||||
* @param {Record<string, unknown>|null|undefined} frontmatterData Données frontmatter sérialisées.
|
||||
* @returns {boolean} true si l'article est considéré comme publié.
|
||||
*/
|
||||
function isEffectivelyPublished(frontmatterData) {
|
||||
if (!frontmatterData || typeof frontmatterData !== "object") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isDraftValue(frontmatterData.draft) === false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indique si un document YAML frontmatter correspond à un article publié.
|
||||
* @param {{ get: (key: string) => unknown }|null|undefined} doc Document YAML.
|
||||
* @returns {boolean} true si l'article est considéré comme publié.
|
||||
*/
|
||||
function isEffectivelyPublishedDocument(doc) {
|
||||
if (!doc || typeof doc.get !== "function") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isDraftValue(doc.get("draft")) === false;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseBoolean,
|
||||
isDraftValue,
|
||||
isEffectivelyPublished,
|
||||
isEffectivelyPublishedDocument,
|
||||
};
|
||||
Reference in New Issue
Block a user