question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preprocessing of frontmatter

See original GitHub issue

It would be cool to run some processing on front matter before it’s delivered thru layout props/and potentially stuff.

For example, I define my page descriptions in the front matter and i want to smartypants these descriptions before they’re used in <meta> tags and whatnot.

---
description: It's my description--I hope you enjoy the contents.
---

Should ultimately be transformed to

It’s my description—I hope you enjoy the contents.

If you pull a smartypants implementation into your code that’s extra that needs to be shipped to the browser when it could’ve been processed away at compile time.

There are a few other use cases for this, @pngwn noted that some want to put actual markdown inside the frontmatter too which would require similar considerations.

My proposed solution

Add a new mdsvex option that allows to to run custom transforms on the frontmatter before it’s delivered elsewhere.

mdsvex({
    preprocessFrontMatter: ({key, value}) => { ... }
})

Open questions

  • You might want different behavior for different kinds of files, so knowing which file is being preprocessed might be useful. How should this be passed?
  • You also might want to reuse the parser you have defined with all of its plugins etc, to be run that over the value. This does not currently fit into the proposed solution.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
artemkovalyovcommented, Dec 4, 2022

You can also write a custom remark plugin, the frontmatter data exists on the vFile and can be modified by plugins. Would still be nice to have a clean API for this in v1 (there will be no remark then).

Oh Man, I should have read this earlier. Would save me a couple of hours. I was updating yaml in the tree but didn’t see any changes on the imported modules. Adding this data to vFile solved it.

import { visit } from 'unist-util-visit';
import getReadingTime from 'reading-time';

export default function readingTime(options = { wordsPerMinute: 200 }) {
  return (tree, file) => {
    let text = '';
    visit(tree, ['text', 'code'], (node) => {
      text += node.value;
    });

    const readingStats = getReadingTime(text, options);
    file.data.fm = { ...file.data.fm, ...readingStats };
    console.log(file.data);
    // visit(tree, ['yaml'], (node) => {
    //   node.value +=
    //     `\nreadingTime: ${readingStats.minutes}\n` + `wordsCount: ${readingStats.words}\n`;
    //   console.log(node);
    // });
    visit(tree, ['yaml', 'toml'], (node) => {
      node.value +=
        `\nreadingTime: ${readingStats.minutes}\n` + `wordsCount: ${readingStats.words}`;
      // console.log(node);
    });
  };
}

I can add docs somewhere if you’d like. This is a kinda implicit convention/API that I couldn’t grasp.

1reaction
pngwncommented, Jun 1, 2022

You can also write a custom remark plugin, the frontmatter data exists on the vFile and can be modified by plugins. Would still be nice to have a clean API for this in v1 (there will be no remark then).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Front Matter Data — Eleventy
Note also that layouts can contain front matter variables that you can use in your local template. Leaf template front matter takes precedence...
Read more >
Supply front matter metadata to preprocess functions - Drupal
Problem/Motivation [#3064854] introduces the ability for Twig templates to contain front matter metadata. It would be very helpful to themes ...
Read more >
Preprocessor Extension Example - Asciidoctor Docs
Skim off front matter from the top of the document that gets used by site generators like Jekyll and Awestruct. sample-with-front-matter.adoc. tags: [ ......
Read more >
Section 6. Preprocessing options - XMLmind
Frontmatter means at the beginning of the document. Backmatter means at the end of the document. This option, like List of Figures, List...
Read more >
Marketing_Fragment 6 x 10.5.T65
978-0-521-83657-9 - The Text Mining Handbook: Advanced Approaches in Analyzing Unstructured Data. Ronen Feldman and James Sanger. Frontmatter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found