cristianvasquez / obsidian-prettify

A markdown prettifier for obsidian
GNU General Public License v3.0
129 stars 12 forks source link

Extra newline in YAML frontmatter that does not have to be there #70

Closed jorgb closed 2 years ago

jorgb commented 3 years ago

Hi,

Thank you for making this plugin. I wanted to ask if it is possible to get rid of the extra newline in the YAML frontmatter update. For example, my header looks like this

---
updated: '2021-08-03T06:59:25'
---

After "Markdown: Prettifier Run" I get:

---
updated: '2021-08-03T07:01:10'

---

And I do not know why it forces a newline in the YAML block, as there should not need to be. I hope this can be fixed so that the block stays compact.

Thanks!

fukunee commented 2 years ago

Got it. Fix this tweak by adding .replace(/\n$/, "") to line 10331 and 10339 in compiled main.js

/**
 * Returns the transformer which acts on the MDAST tree and given VFile.
 *
 * @link https://github.com/unifiedjs/unified#function-transformernode-file-next
 * @link https://github.com/syntax-tree/mdast
 * @link https://github.com/vfile/vfile
 * @return {function}
 */
function metadataWriter(options, input) {
    //@ts-ignore
    var transformer = function(ast, vFile, next) {
        var metadataNode = getMetadataNode(ast);
        var hasMetadata = !(metadataNode == null);
        // If we don't have a Matter node in the AST, put it in.
        if (!hasMetadata && options.createHeaderIfNotPresent) {
            metadataNode = {
                type: "yaml",
==>           value: jsYaml.dump(newHeaderTemplateYAML(options.newHeaderTemplate, input)).replace(/\n$/, ""), 
            };
            ast.children.unshift(metadataNode);
            hasMetadata = true;
        } else {
            // Only updates if frontmatter already created
            if (options.updateHeader && hasMetadata) {
                // Write metadata (by reference)
==>            metadataNode.value = mergeValues(metadataNode.value, newHeaderTemplateYAML(options.updateHeaderTemplate, input)).replace(/\n$/, "");
                console.log(metadataNode.value);
            }
        }
        if (typeof next === "function") {
            return next(null, ast, vFile);
        }
        return ast;
    };
    return transformer;
}
cristianvasquez commented 2 years ago

Hi @fukunee, @jorgb thanks for the report. I've updated a new version

Cheers!