Skip to content

Commit

Permalink
Dropped support for Node <8. Documented 'keys' option in README. Upda…
Browse files Browse the repository at this point in the history
…ted dev dependencies
  • Loading branch information
webketje committed Dec 11, 2021
1 parent 0f1cc8f commit baf3422
Show file tree
Hide file tree
Showing 4 changed files with 12,577 additions and 554 deletions.
109 changes: 64 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,28 @@
# metalsmith-markdown
# @metalsmith/markdown

[![npm version][npm-badge]][npm-url]
[![code style: prettier][prettier-badge]][prettier-url]
[![metalsmith: core plugin][metalsmith-badge]][metalsmith-url]

[![Known Vulnerabilities][snyk-badge]][synk-url]
A Metalsmith plugin to render markdown files to HTML, using [Marked](https://github.com/markedjs/marked).

A Metalsmith plugin to convert markdown files.
[![metalsmith: core plugin][metalsmith-badge]][metalsmith-url]
[![npm: version][npm-badge]][npm-url]
[![ci: build][ci-badge]][ci-url]
[![code coverage][codecov-badge]][codecov-url]
[![license: MIT][license-badge]][license-url]

## Installation

NPM:
```bash
$ npm install @metalsmith/markdown
npm install @metalsmith/markdown
```

## CLI Usage

Install via npm and then add the `@metalsmith/markdown` key to your `metalsmith.json` plugins with any [Marked](https://github.com/markedjs/marked) options you want, like so:

```json
{
"plugins": {
"@metalsmith/markdown": {
"pedantic": false,
"gfm": true,
"tables": true,
"breaks": false,
"sanitize": false,
"smartLists": true,
"smartypants": false,
"xhtml": false
}
}
}
Yarn:
```bash
yarn add @metalsmith/markdown
```

## Javascript Usage

Pass `options` to the markdown plugin and pass it to Metalsmith with the `use` method:
## Usage

```js
var markdown = require('@metalsmith/markdown');
var highlighter = require('highlighter');
const markdown = require('@metalsmith/markdown');

metalsmith.use(markdown({
highlight: function(code) {
Expand All @@ -58,9 +39,28 @@ metalsmith.use(markdown({
}));
```

## Custom Renderer
### Options

`@metalsmith/markdown` is powered by [Marked](https://github.com/markedjs/marked), and you can pass any of the [Marked options](https://marked.js.org/using_advanced#options) to it, including the ['pro' options](https://marked.js.org/using_pro#extensions): `renderer`, `tokenizer`, `walkTokens` and `extensions`.

`@metalsmith/markdown` uses `marked`, so to create a custom renderer get an instance of `marked.Renderer()`
Additionally, you can render markdown to HTML in file metadata keys by specifying the `keys` option:

```js
metalsmith.use(markdown({
keys: ['html_desc']
}))
```
A file `article.md` with front-matter:
```md
---
html_desc: A **markdown-enabled** _description_
---
```
would transform `html_desc` to `A <b>markdown-enabled</b> <i>description</i>`.

### Custom markdown rendering

You can use a custom renderer by of `marked.Renderer()`

```js
var markdown = require('@metalsmith/markdown');
Expand Down Expand Up @@ -90,19 +90,38 @@ metalsmith.use(markdown({
}));
```

## History
### CLI Usage

Add `@metalsmith/markdown` key to your `metalsmith.json` plugins key

[History](./History.md#Latest)
```json
{
"plugins": {
"@metalsmith/markdown": {
"pedantic": false,
"gfm": true,
"tables": true,
"breaks": false,
"sanitize": false,
"smartLists": true,
"smartypants": false,
"xhtml": false
}
}
}
```

## License

MIT
[MIT](LICENSE)

[npm-badge]: https://img.shields.io/npm/v/metalsmith-markdown.svg
[npm-url]: https://www.npmjs.com/package/metalsmith-markdown
[prettier-badge]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?longCache=true
[prettier-url]: https://github.com/prettier/prettier
[npm-badge]: https://img.shields.io/npm/v/@metalsmith/markdown.svg
[npm-url]: https://www.npmjs.com/package/@metalsmith/markdown
[ci-badge]: https://app.travis-ci.com/metalsmith/markdown.svg?branch=master
[ci-url]: https://app.travis-ci.com/github/metalsmith/markdown
[metalsmith-badge]: https://img.shields.io/badge/metalsmith-core_plugin-green.svg?longCache=true
[metalsmith-url]: http://metalsmith.io
[snyk-badge]: https://snyk.io/test/github/segmentio/metalsmith-markdown/badge.svg?targetFile=package.json
[synk-url]: https://snyk.io/test/github/segmentio/metalsmith-markdown?targetFile=package.json
[metalsmith-url]: https://metalsmith.io
[codecov-badge]: https://img.shields.io/coveralls/github/metalsmith/markdown
[codecov-url]: https://coveralls.io/github/metalsmith/markdown
[license-badge]: https://img.shields.io/github/license/metalsmith/markdown
[license-url]: LICENSE
12 changes: 3 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var basename = require('path').basename;
var debug = require('debug')('metalsmith-markdown');
var debug = require('debug')('@metalsmith/markdown');
var dirname = require('path').dirname;
var extname = require('path').extname;
var join = require('path').join;
Expand All @@ -19,7 +19,7 @@ var markdown = function(file) {
* Metalsmith plugin to convert markdown files.
*
* @param {Object} options (optional)
* @property {Array} keys
* @property {Array} keys
* @return {Function}
*/
var plugin = function(options) {
Expand All @@ -38,13 +38,7 @@ var plugin = function(options) {

debug('converting file: %s', file);
var str = marked(data.contents.toString(), options);
try {
// preferred
data.contents = Buffer.from(str);
} catch (err) {
// node versions < (5.10 | 6)
data.contents = new Buffer(str);
}
data.contents = Buffer.from(str);
keys.forEach(function(key) {
if (data[key]) {
data[key] = marked(data[key].toString(), options);
Expand Down
Loading

0 comments on commit baf3422

Please sign in to comment.