Giter VIP home page Giter VIP logo

jdown's Introduction



jdown

Convert a directory of markdown files to structured and usable JSON

Features

๐Ÿ“„ Top level files will be turned into an object
๐Ÿ“ Files contained within top level directories will be grouped into an object
๐Ÿ—‚ Files contained within folders of the collections directory will be turned into an array of objects
๐Ÿ–ผ Assets will be automatically minified and cache busted
๐Ÿซ File names will be transformed to camelCase and used as property names
โœ๏ธ Markdown will be parsed as HTML using Metalsmith Markdown and output within a contents property
๐Ÿ•น Frontmatter will be added as properties of the generated objects.
๐Ÿ’… Custom elements can be used in the markdown parsing with renderer options
๐Ÿ”ง Markdown parsing can be disabled to just recieve structured JSON containing markdown


Install

$ npm install jdown

Usage

const jdown = require('jdown');
jdown('path/to/markdown/content').then(content => console.log(content));

Where "path/to/markdown/content" is a directory containing markdown files or folders as described under the "Features" header above. The file path will be relative to the project root so if your content was in /Users/username/project/src/content, you would use jdown('src/content').

Use the generated JSON however you like, I recommend with a static site generator like React Static! This way you can use the markdown files as a CMS and use the JSON in the build stage.

Placing assets inside the content directory

Using jdown to parse assets is completely optional, but comes with a few benefits including:

  • Ability to organise assets alongside markdown content
  • Auto minification of image files using imagemin
  • Cache busting - uses the last modified time (mtime) of the asset to change it's file name and avoid the old version of the asset being served

To get started using assets in jdown you can create a /assets folder in your contents directory, reference it in your markdown like ![](./assets/my-asset.png) then specify an existing directory for jdown to put processed assets into using an assets options object like:

jdown(
  'path/to/markdown/content',
  {assets: {output: './example/public'}}
).then(content => console.log(content));

All static assets must be placed within /assets folders. Assets folders can be placed in the top level content directory and/or it's sub directories. Within markdown the assets can then be referenced using ./assets/filename.png. A couple of examples of potential directory structures can be found below.

Single assets folder

|- contents
| |- assets 
| | |- my-asset.png
| |- collections
| | |- blog
| | | |- post.md <- can access ./assets/my-asset.png

Multiple assets folders

|- contents
| |- assets 
| | |- my-asset.png
| |- collections
| | |- blog
| | | |- assets
| | | | |- my-asset.png
| | | |- post.md <- ./assets/my-asset.png will reference the asset in the blog assets directory

The assets options object can contain the following properties:

Property Type Default Description
output string ./public Directory jdown will output processed assets to
path string / Publically accessible path jdown will prepend to output file names
png object {quality: '65-80'} Options to pass into imagemin-pngquant
jpg object undefined Options to pass into imagemin-jpegtran
svg object undefined Options to pass into imagemin-svgo
gif object undefined Options to pass into imagemin-svgo

Custom render options

By default, jdown uses the default marked render options, but you may pass in your own custom overrides to customize the built html. This can be useful for adding custom ids or CSS classes. In the example below you can see how you can make your links open in a new tab by default, by adding target="_blank" to anchor tags.

const jdown = require('jdown');
const marked = require('marked');
const renderer = new marked.Renderer();
renderer.link = (href, title, text) => `<a target="_blank" href="${href}" title="${title}">${text}</a>`;

jdown('path/to/markdown/content', {renderer}).then(content => console.log(content));

See the advanced configurations and extensibility sections of the marked documentation for the full list of possible options you may use here.

Disabling markdown parsing

In some cases you may wish to disable markdown parsing to just recieve structured JSON containing markdown instead of HTML. You can accomplish this using the parseMd option like so:

jdown('path/to/markdown/content', {parseMd: false}).then(content => console.log(content));

Examples

See the examples directory of this repository. To test it yourself clone this repo, install the dependancies with npm install, modify some content and run npm run example.

danwebb.co is built using jdown and React Static so see the static.config.js file in the websites github repo for a real world example.

There is also an example built into React Static that you can use to quickly get up and running here.

Contributing

Any pull requests are welcome and will be reviewed. Please use npm run test to test your changes are working before submitting any changes.

License

MIT

jdown's People

Contributors

danwebb avatar montezume avatar foxxxyben avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.