How to convert Markdown into Org-Mode

I have migrated this blog multiple times and is hard to describe how many problems I solve hover and hover.

Just to give you some background of these migrations I started from a WordPress that I run in a simple DigitalOcean Droplet. It became too sluggish and somehow didn't feel to leave my editor for writing an Article. So I move everything to files, because in the end who need a database just for few articles.

But here I am with a bunch of Markdown and an Emacs, which I play setup with Nuxt.

But wait for a second, Emacs as Org-mode which already use for a bunch of stuff, and yes why not just move the entire website and note-taking into Org-mode? I could build live demos, components, research papers all from one side and don't leave my editor.

But enough background, let's move into the meet greedy stuff, I'm here with a bunch of markdowns; but I want them to be org-mode now.

So first of all I create use pandoc to do the conversion for me

pandoc -i ${file_from} -f markdown -t org -o ${file_to}

That would allow us to convert the file from markdown to org-mode, but since I've been doing a bit of magic with the help of frontmatter (eg: render my cv)

I need to have a way that allows bringing that information back to the org-mode file.

For that purpose, I would just use the frontmatter package in npm.

In the beginning, I try to use the only cli but I stumble in a couple of issues

  • I couldn't imagine that sed was so hard with multiline.
  • I initially try to find a package that is cli-ready, I ended with yaml-front-matter which look ideal to run with npx. I finally realize that they provide the content on the root level, with the option to change it I soon realize that this was not suitable for me, I prefer the way that frontmatter does so { data: TheYaml, content: "The Markdown" }

So I will just continue with frontmatteer.

npm install -S frontmatter

To give you an idea of the data structure that I want to use the bellow is an example:

  title: 'Artista e designer',
  link: '',
  author: 'Ideabile',
  description: null,
  post_id: 5,
  created: '2012/04/15 17:32:41',
  created_gmt: '2012/04/15 17:32:41',
  comment_status: 'open',
  post_name: 'artista-e-designer',
  status: 'publish',
  post_type: 'post'

What I want to do is a script that grabs the stdin of a markdown and returns an org file.

During my migration, I only care about a couple of fields, more specifically the create, title. With those fields, I want to add them in my org-mode file so that I can query my content.

#!/usr/bin/env node
const frontmatter = require('frontmatter');
const fs = require('fs');
const { execSync } = require('child_process');
const stdin = fs.readFileSync(0).toString();
const { data } = frontmatter(stdin);

if (stdin && data) {
    org = execSync(`pandoc -f markdown -t org <<< "${stdin.replace(/\x22/g, '\\\x22')}"`).toString();
    const day = new Date(data.created);
    const _date = [
        day.getMonth() < 10 ? `0${day.getMonth()}` : day.getMonth(),
        day.getDate() < 10 ? `0${day.getDate()}` : day.getDate(),
    console.log(`#+TITLE: ${data.title}
#+DATE: <${_date}>


Then we need to make the file executable

chmod +x ../../tangled/convert-md-to-org.js

Then we can call it like this

cat ${file_from} | ../../tangled/convert-md-to-org.js

But let's assume we want to convert a full directory.

shopt -s nullglob;
cd content/articles;
for f in *.md; do
    cat f | ../../tangled/convert-md-to-org.js > ../articles-org/"${}.org";
shopt -u nullglob;