A util for getting data and metadata for all markdown files in a given dir. Useful for building static site generators

Overview

npm version

extract-md-data

A util for getting data and metadata for all markdown files in a given dir. Useful for building static site generators.

Usage

Given this example file structure:

.
└── src
    ├── docs
    │   ├── Day-One.md
    │   ├── Day-Two.md
    │   └── not-markdown.txt
    └── index.js

Day-One.md

---
title: day one
tags:
  - foo
  - bar
---

# On the first day

God created markdown

Day-Two.md

---
title: day two
tags:
  - baz
  - pistachio
---

# On the second day

God created another markdown

To get JSON data for all the markdown files in docs folder:

// index.js

const extract = require('extract-md-data');
const path = require('path');

/* Define project rootDir and srcDir where the markdown files live */
const rootDir = path.resolve(__dirname);
const srcDir = path.resolve(rootDir, 'docs');

const jsons = extract(rootDir, srcDir);

console.log(jsons);
[
  {
    "fm": {
      "title": "day one",
      "tags": ["foo", "bar"]
    },
    "content": "\n# On the first day\n\nGod created markdown\n",
    "relativePath": "docs/Day-One.md",
    "relativeDir": "docs",
    "filename": "Day-One.md",
    "slug": "day-one",
    "id": "be308ccd-71e6-5339-97d9-947670d116ba"
  },
  {
    "fm": {
      "title": "day two",
      "tags": ["baz", "pistachio"]
    },
    "content": "\n# On the second day\n\nGod created another markdown\n",
    "relativePath": "docs/Day-Two.md",
    "relativeDir": "docs",
    "filename": "Day-Two.md",
    "slug": "day-two",
    "id": "c115a245-d83c-5a8e-9d61-2bb40c05afdb"
  }
]

This data can be used with templating libraries like pug or handlebars to build out static websites based off the markdown files.

You can make your own routing logic - perhaps you want to leverage relativeDir? Up to you!

Features

  • gets data for all files in srcDir with .md and .markdown extensions, recursively
  • ignores non-markdown files
  • extracts any custom yaml frontmatter into an object fm
  • extracts relativeDir for use with file-based routing
  • generates a unique id for each file by hashing the relativePath
  • generates a url-safe slug for each file based on the filename

Config

You can optionally pass a config object as a third argument.

const jsons = extract(rootDir, srcDir, opts);

options.slugify

Options for slugify. See available settings. These are the default settings:

{
  slugify: {
    replacement: '-',
    remove: /[*+~.()'"!:@$%^()]/g,
    lower: true,
    strict: true,
    trim: true
  }
}

options.omitContent

Boolean indicating whether or not to return markdown body content. You can set this to true if you don't need the markdown bodies from the files and want to save memory. Defaults to false

{
  // omits markdown body in returned objects
  omitContent: true;
}

Development

Install

npm install

Testing

npm run test

You might also like...

A lightweight jQuery Ajax util library.

Just Wait Wait what? The server response. Just Wait is a lightweight jQuery utility that allows you to specify a function to be executed after a speci

Jun 22, 2022

Util for calling Prisma middleware for nested write operations.

Prisma Nested Middleware Util for calling Prisma middleware for nested write operations. Existing Prisma middleware is called once for every operation

Dec 7, 2022

Gofiber with NextJS Static HTML is a small Go program to showcase for bundling a static HTML export of a Next.js app

Gofiber and NextJS Static HTML Gofiber with NextJS Static HTML is a small Go program to showcase for bundling a static HTML export of a Next.js app. R

Jan 22, 2022

Quickly create an interactive HTML mock-up by auto sourcing lorem ipsum/images generators, with minimal html markup, and no server side code

RoughDraft.js v0.1.5 Quickly mockup / prototype HTML pages with auto-generated content, without additional JavaScript or server side code. section

Dec 21, 2022

For data quality enthousiasts (and dataview lovers): manage the metadata of your notes.

Metadata Menu This plugin is made for data quality enthousiasts: access and manage the metadata of your notes in Obsidian. Metadata Menu adds context

Dec 23, 2022

Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

May 18, 2022

Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

May 3, 2022

Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

Jan 3, 2023

🏗 Build static blog with lines of HTML and Markdown.

🏗 Build static blog with lines of HTML and Markdown.

Nimblog Nimblog requires only a few lines of HTML to deploy and is suitable for lightweight bloggers. The official guide is built with Nimblog, check

Dec 19, 2022
Owner
Claire Froelich
Claire Froelich
A set of scripts to test markdown processing speeds in various site generators/frameworks

bench-framework-markdown A set of scripts to test markdown processing speeds in various site generators/frameworks. Read the blog post: Which Generato

Zach Leatherman 24 Nov 3, 2022
An obsidian plugin for uploading local images embedded in markdown to remote store and export markdown for publishing to static site.

Obsidian Publish This plugin cloud upload all local images embedded in markdown to specified remote image store (support imgur only, currently) and ex

Addo.Zhang 7 Dec 13, 2022
✏️ A small jQuery extension to turn a static HTML table into an editable one. For quickly populating a small table with JSON data, letting the user modify it with validation, and then getting JSON data back out.

jquery-editable-table A small jQuery extension to turn an HTML table editable for fast data entry and validation Demo ?? https://jsfiddle.net/torrobin

Tor 7 Jul 31, 2022
docsQL - Getting an overview over your Markdown file in your Jamstack site

docsQL Getting an overview of your Jamstack Markdown files. Demo Play with: https://peterbe.github.io/docsql/ You're supposed to run docsQL with your

Peter Bengtsson 19 Jan 3, 2023
BookStore is a website that allows a given user to view a list of books, to add a new book and remove a given book.

Project Name : BookStore CMS BookStore is a website that allows a given user to view a list of books, to add a new book and remove a given book. In or

Chris Siku 10 Aug 22, 2022
Script to fetch all NFT owners using moralis API. This script output is a data.txt file containing all owner addresses of a given NFT and their balances.

?? Moralis NFT Snapshot Moralis NFT API will only return 500 itens at a time when its is called. For that reason, a simple logic is needed to fetch al

Phill Menezes 6 Jun 23, 2022
A GitHub Action to enable Pages and extract various metadata about a site

A GitHub Action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.

GitHub Actions 50 Jan 1, 2023
Markdown Transformer. Transform markdown files to different formats

Mdtx Inspired by generative programming and weed :). So I was learning Elm language at home usually in the evening and now I am missing all this gener

Aexol 13 Jan 2, 2023
Util for kafkajs to buffer messages and send them in batches, inspired by node-rdkafka

kafkjajs-buffer Plugin for kafkajs to buffer messages and send them in batches, inspired by node-rdkafka Overview kafkajs-buffer adds queue/buffer cap

Alberto Juan 7 Sep 7, 2022
JS/TS lightweight value-multimethod util

fp-multik ????‍♂️ Small functional utility for control flow and conditional operator for functions. Multik is value-based multimethod for Javascript/T

Kalagin Ivan 5 Dec 25, 2022