Coder Social home page Coder Social logo

Comments (9)

leesei avatar leesei commented on May 12, 2024 1

Noted that the goal of this library is not for frontmatter processing.
Closing this issue.

from vfile-matter.

wooorm avatar wooorm commented on May 12, 2024

hi!
I don’t understand how this feature helps your goal?
Perhaps you can expand on your actual problem some more?

I’d think you want to check if the entire file is changed or not. For that, I would hash the original file, and hash the current file, when those differ the file is changed.

import crypto from 'node:crypto'

const before = crypto.createHash('md5').update(String(originalFile)).digest('hex')
const after = crypto.createHash('md5').update(String(newFile)).digest('hex')
const changed = after !== before

from vfile-matter.

leesei avatar leesei commented on May 12, 2024

Here's how I would like to use it:

matter(vfile, { strip: true });
// I do sorting and transform of frontmatter here
vfile.data.matter = normalize_frontmatter(frontmatter);

// if I have the source yaml as string
// I can do something like (stripping and edge cases aside)
// file.data.should_write = (vfile.data.yaml_raw == yaml.stringify(vfile.data.matter));

// writing file
await writeFile(
  out_path,
  "---\n" +
    yaml.stringify(vfile.data.matter) +
    "---\n" +
    vfile.toString()
);

You suggestion would work, but:

  • it requires reading originalFile again
  • it requires writing out the newFile for comparison

Frankly speaking I've yet to study how it's done in Prettier and my thought might be a premature optimization 😛 .

Again, since vfile-matter is just a small library I could have just forked it and use my fork in my project.
Just wondering if my use case makes sense to be merged upstream.

from vfile-matter.

wooorm avatar wooorm commented on May 12, 2024

it requires reading originalFile again

I don’t think it does. The file you have when you call matter, is the original file. Which you can pass here.

it requires writing out the newFile for comparison

I don’t think it does. You can do that when changed is true, in which case it is changed.

I've yet to study how it's done in Prettier

I am pretty sure that it is done the way I propose. I can’t see how else it would work.

from vfile-matter.

leesei avatar leesei commented on May 12, 2024

The file you have when you call matter, is the original file. Which you can pass here.

Since I called matter(vfile, { strip: true }), the YAML header is stripped.
If I don't use strip, I would have to search and replace the YAML on my own.
This why I thought of returning match in the first place.

What I meant is the construction of file in memory, not writing it out.
Indeed your example can be done in memory and not involving file I/O (except for the aforementioned issue that I don't have the original YAML or whole file).
Since I'm only modifying the frontmatter it seems more strict forward and efficient to just compare the YAML.

from vfile-matter.

wooorm avatar wooorm commented on May 12, 2024

I really don‘t understand what you want apparently?

Why doesn’t your normalize_frontmatter track whether it changes things?
Why don’t you diff objects?

matter(vfile, { strip: true });
const nextMatter = normalize_frontmatter(frontmatter);

if (someDiffFunction(vfile.data.matter, nextMatter) {
  // Write
}

I don’t understand where prettier works on frontmatter but ignores the rest of the file?

Why do you even want to diff? It’s probably fine to just write the file?

from vfile-matter.

leesei avatar leesei commented on May 12, 2024

Diffing the JavaScript object parsed from frontmatter doesn't work:

Input:

tags: ["github"]

Formatted:

tags:
  - github

These two correspond to the same JS object.
It is my requirement to only work on the frontmatter, not Prettier's.
Prettier doesn't format the frontmatter so I'm writing my own.

Yes, as I memtioned before, it's probably just fine to overwrite the files irregardless of whether there's a change or not.

from vfile-matter.

wooorm avatar wooorm commented on May 12, 2024

It is my requirement to only work on the frontmatter, not Prettier's.

Why is it that you only care about frontmatter?
Most of the times, our tools are about the AST. VFile was specifically made to work alongside those.
That doesn’t mean it’s the only use case for it, but it remains the primary focus.

What does Prettier have to do with all this? Are you making a prettier plugin?

from vfile-matter.

github-actions avatar github-actions commented on May 12, 2024

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

from vfile-matter.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.