UNPKG

@kitmodule/kityaml

Version:

Convert JavaScript objects to YAML front matter and Markdown in vanilla JavaScript lightweight, dependency-free, and easy to use.

8 lines 1.94 kB
(function(global){const kitmodule=global.kitmodule||(global.kitmodule={});const $=Symbol("$");function KitYAML(obj={}){if(typeof obj!=="object"||obj===null||Array.isArray(obj)){throw new Error("KitYAML: input must be a plain object.")} this[$]={obj,inline:!0}} KitYAML.prototype.inline=function(enable=!0){this[$].inline=enable;return this};KitYAML.prototype.block=function(enable=!0){this[$].inline=!enable;return this};KitYAML.prototype.convert=function(){const{obj,inline}=this[$];return encodeYAML(obj,0,inline)};KitYAML.prototype.frontMatter=function(body=""){return `---\n${this.convert()}\n---\n\n${body}`};function encodeYAML(value,indent=0,inline=!1){const space=" ".repeat(indent);if(value===null||value===undefined)return'""';if(typeof value==="boolean"||typeof value==="number")return value;if(typeof value==="string")return JSON.stringify(value);if(Array.isArray(value)){if(inline){return `[${value.map(v => encodeYAML(v, indent, inline)).join(", ")}]`}else{return value.map(v=>{if(typeof v==="object"&&v!==null){return `\n${space}- ${encodeYAML(v, indent + 1, inline).replace(/^\s+/, "")}`}else{return `${space}- ${encodeYAML(v, indent + 1, inline)}`}}).join("\n")}} if(typeof value==="object"){return Object.entries(value).map(([key,val])=>{if(Array.isArray(val)){if(inline){return `${space}${key}: [${val.map(i => encodeYAML(i, indent, inline)).join(", ")}]`}else{return `${space}${key}:\n${encodeYAML(val, indent + 1, inline)}`}} if(typeof val==="object"&&val!==null){return `${space}${key}:\n${encodeYAML(val, indent + 1, inline)}`} return `${space}${key}: ${encodeYAML(val, indent + 1, inline)}`}).join("\n")} return JSON.stringify(value)} global.KitYAML=KitYAML;global.yamlFrontMatter=(obj,body)=>new KitYAML(obj).frontMatter(body);kitmodule.YAML=KitYAML;kitmodule.yaml=(obj)=>new KitYAML(obj);if(typeof module!=="undefined"&&module.exports){module.exports={KitYAML}}})(typeof window!=="undefined"?window:globalThis)