0.1.14 • Published 2 months ago

transpile-md-to-json v0.1.14

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

transpile-md-to-json

Transpile multiple markdown files to a single JSON file.

This project was developed to transpile all markdown files in a given folder (recursively) to a single JSON file which can be imported in React and fed to markdown-to-jsx.

Features

  • Super simple to use
  • Uses trusted dependencies (chalk, chokidar, commander, etc...)
  • Actively maintained and used by the Lickstats team
  • Very light codebase to audit
  • Written in TypeScript

Installation

npm install transpile-md-to-json -g

Usage

$ transpile-md-to-json -h
Usage: transpile-md-to-json [options]

Options:
  --src <source>        path to content folder
  --dest <destination>  path to JSON file
  --ignore <ignore...>  paths to ignore
  --slugify             slugify folder and file names
  --flatten             flatten nested properties
  --blogify             enables slugify and flatten and includes metadata
  --watch               watch source for changes
  -h, --help            output usage information

For CRA projects, consider using concurrently to run both start and transpile scripts concurrently using npm run code.

{
  "scripts": {
    "start": "react-scripts start",
    "transpile": "transpile-md-to-json --src src/content --dest src/content.json --watch",
    "code": "concurrently -n start,transpile npm:start npm:transpile"
  }
}

Notice the --watch argument? This runs transpile-md-to-json in the background, transpiling markdown files as they are created, edited and deleted.

Examples

Transpile markdown files in examples/content to JSON and output result to stdout

$ transpile-md-to-json --src examples/content
{
  "fr": {
    "foo": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n"
  },
  "es": {
    "foo": "<!--\nTitle: Esto es una prueba\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Esto es una prueba\n"
  },
  "en": {
    "foo bar": "<!--\nTitle: This is another file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is another file name test\n",
    "foo": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n",
    "a": {
      "b": {
        "Hello world!": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
      }
    }
  }
}

Transpile markdown files in examples/content to JSON ignoring es and en/foo bar.md paths and output result to stdout

$ transpile-md-to-json --src examples/content --ignore es --ignore "en/foo bar.md"
{
  "fr": {
    "foo": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n"
  },
  "en": {
    "foo": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n",
    "a": {
      "b": {
        "Hello world!": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
      }
    }
  }
}

Transpile markdown files in examples/content to JSON and write result to examples/content.json

$ transpile-md-to-json --src examples/content --dest examples/content.json

$ cat examples/content.json
{
  "fr": {
    "foo": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n"
  },
  "es": {
    "foo": "<!--\nTitle: Esto es una prueba\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Esto es una prueba\n"
  },
  "en": {
    "foo bar": "<!--\nTitle: This is another file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is another file name test\n",
    "foo": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n",
    "a": {
      "b": {
        "Hello world!": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
      }
    }
  }
}

Transpile and slugify markdown files in examples/content to JSON and write result to examples/content-slugify.json

$ transpile-md-to-json --src examples/content --dest examples/content-slugify.json --slugify

$ cat examples/content-slugify.json
{
  "fr": {
    "foo": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n"
  },
  "es": {
    "foo": "<!--\nTitle: Esto es una prueba\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Esto es una prueba\n"
  },
  "en": {
    "foo-bar": "<!--\nTitle: This is another file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is another file name test\n",
    "foo": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n",
    "a": {
      "b": {
        "hello-world": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
      }
    }
  }
}

Transpile and flatten markdown files in examples/content to JSON and write result to examples/content-flatten.json

$ transpile-md-to-json --src examples/content --dest examples/content-flatten.json --flatten

$ cat examples/content-flatten.json
{
  "fr.foo": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n",
  "es.foo": "<!--\nTitle: Esto es una prueba\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Esto es una prueba\n",
  "en.foo-bar": "<!--\nTitle: This is another file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is another file name test\n",
  "en.foo": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n",
  "en.a.b.hello-world": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
}

Transpile and blogify markdown files in examples/content to JSON and write result to examples/content-blogify.json

The id property is derived from the dot path (fr.foo for example) using a MD5 hash function.

The metadata property is derived from the comment block (if present, see examples/fr/foo.md). Each line is parsed using /([^:]+): ?(.+)/ and keys are slugified and converted to camel case.

$ transpile-md-to-json --src examples/content --dest examples/content-blogify.json --blogify

$ cat examples/content-blogify.json
{
  "fr.foo": {
    "id": "f5fdbc126cb1a123fe8d60297803ea4f",
    "path": "fr/foo.md",
    "basename": "foo.md",
    "createdOn": "2020-02-08T22:44:57.899Z",
    "modifiedOn": "2020-03-04T01:04:39.328Z",
    "metadata": {
      "title": "Ceci est un test",
      "publicationDate": "2020-03-03T14:15:23.676Z"
    },
    "content": "<!--\nTitle: Ceci est un test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Ceci est un test\n"
  },
  "es.foo": {
    "id": "94ced16505d73afe6cb2f7528b81f351",
    "path": "es/foo.md",
    "basename": "foo.md",
    "createdOn": "2020-09-14T09:59:37.395Z",
    "modifiedOn": "2020-09-14T10:03:59.062Z",
    "metadata": {
      "title": "Esto es una prueba",
      "publicationDate": "2020-03-03T14:15:23.676Z"
    },
    "content": "<!--\nTitle: Esto es una prueba\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# Esto es una prueba\n"
  },
  "en.foo-bar": {
    "id": "648d2a8192ed79e49ab9d460a94587af",
    "path": "en/foo bar.md",
    "basename": "foo bar.md",
    "createdOn": "2020-09-14T09:58:54.169Z",
    "modifiedOn": "2020-09-14T11:16:33.004Z",
    "metadata": {
      "title": "This is another file name test",
      "publicationDate": "2020-03-03T14:15:23.676Z"
    },
    "content": "<!--\nTitle: This is another file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is another file name test\n"
  },
  "en.foo": {
    "id": "08e72796bf9fe05dabdc6131a620deaa",
    "path": "en/foo.md",
    "basename": "foo.md",
    "createdOn": "2020-09-09T19:19:37.548Z",
    "modifiedOn": "2020-09-14T11:15:19.101Z",
    "metadata": {
      "title": "This is a test",
      "publicationDate": "2020-03-03T14:15:23.676Z"
    },
    "content": "<!--\nTitle: This is a test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a test\n"
  },
  "en.a.b.hello-world": {
    "id": "44989b6900829b8bfe748c4bca408761",
    "path": "en/a/b/Hello world!.md",
    "basename": "Hello world!.md",
    "createdOn": "2020-02-08T22:44:57.899Z",
    "modifiedOn": "2020-09-14T11:13:15.995Z",
    "metadata": {
      "title": "This is a file name test",
      "publicationDate": "2020-03-03T14:15:23.676Z"
    },
    "content": "<!--\nTitle: This is a file name test\nPublication date: 2020-03-03T14:15:23.676Z\n-->\n\n# This is a file name test\n"
  }
}

Contributors

Sun Knudsen

Licence

MIT

0.1.14

2 months ago

0.1.13

4 months ago

0.1.12

4 months ago

0.1.11

5 months ago

0.1.10

6 months ago

0.1.9

7 months ago

0.1.8

7 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

11 months ago

0.1.2

11 months ago

0.1.3

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago