• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Parsing Metadata Inside of Markdown Using JavaScript Without Any Dependencies

Javascript · January 20, 2023

In a recent project, I worked with Markdown files that contained metadata at the top for blog posts. I needed to parse the Markdown with JavaScript and make a set of key/value pairs.

As you can see, the metadata is encapsulated in – – – with the metadata contained within.

// Function to parse metadata from a markdown file
const parseMarkdownMetadata = markdown => {
  // Regular expression to match metadata at the beginning of the file
  const metadataRegex = /^---([\s\S]*?)---/;
  const metadataMatch = markdown.match(metadataRegex);

  // If there is no metadata, return an empty object
  if (!metadataMatch) {
    return {};
  }

  // Split the metadata into lines
  const metadataLines = metadataMatch[1].split("\n");

  // Use reduce to accumulate the metadata as an object
  const metadata = metadataLines.reduce((acc, line) => {
    // Split the line into key-value pairs
    const [key, value] = line.split(":").map(part => part.trim());
    // If the line is not empty add the key-value pair to the metadata object
    if(key) acc[key] = value;
    return acc;
  }, {});

  // Return the metadata object
  return metadata;
};

You can call this function and pass in the markdown file content. It will return an object with the metadata.

const markdown = `---
title: My Blog Post
author: John Doe
date: 2021-01-01
---
# My Blog Post
This is the content of my blog post.`;
var metadata = parseMarkdownMetadata(markdown);
console.log(metadata);
// Output: { title: "My Blog Post", author: "John Doe", date: "2021-01-01" }

The great thing about this solution is it requires no additional libraries. It’s all plain Javascript.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Waiting for an Element to Exist With JavaScript
  • Reliably waiting for network responses in Playwright
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Calculate A Javascript Date X Months Ago With Vanilla Javascript

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz