项目作者: arobase-che

项目描述 :
Remark plugin to add support for custom attributes
高级语言: JavaScript
项目地址: git://github.com/arobase-che/remark-attr.git
创建时间: 2018-05-06T17:24:47Z
项目社区:https://github.com/arobase-che/remark-attr

开源协议:Other

下载


remark-attr

This plugin adds support for custom attributes to Markdown syntax.

For security reasons, this plugin uses html-element-attributes.
The use of JavaScript attributes (onload for example) is not allowed by default.

Default Syntax

Images :

  1. ![alt](img){attrs} / ![alt](img){ height=50 }

Links :

  1. [rms with a computer](https://rms.sexy){rel="external"}

Autolink :

  1. Email me at : <mailto:falseEmail@example.org>

Header (Atx) :

  1. ### This is a title
  2. {style="color:red;"}
  3. or
  4. ### This is a title {style="color:yellow;"}
  5. If option enableAtxHeaderInline is set to `true` (default value).

Header :

  1. This is a title
  2. ---------------
  3. {style="color: pink;"}

Emphasis :

  1. Npm stand for *node*{style="color:red"} packet manager.

Strong :

  1. This is a **Unicorn**{awesome} !

Delete :

  1. Your problem is ~~at line 18~~{style="color: grey"}. My mistake, it's at line 14.
  2. Code :
  3. ~~~markdown
  4. You can use the `fprintf`{language=c} function to format the output to a file.

Footnote (using remark-footnotes) :

  1. This is a footnote[^ref]{style="opacity: 0.8;"}
  2. [^ref]: And the reference.

rehype

At the moment it aims is to be used with rehype only, using remark-rehype.

  1. [rms with a computer](https://rms.sexy){rel=external}

produces:

  1. <a href="https://rms.sexy" rel="external">rms with a computer</a>

Installation

npm:

  1. npm install remark-attr

Dependencies:

  1. const unified = require('unified')
  2. const remarkParse = require('remark-parse')
  3. const stringify = require('rehype-stringify')
  4. const remark2rehype = require('remark-rehype')
  5. const remarkAttr = require('remark-attr')

Usage:

  1. const testFile = `
  2. Here a test :
  3. ![ache avatar](https://ache.one/res/ache.svg){ height=100 }
  4. `
  5. unified()
  6. .use(remarkParse)
  7. .use(remarkAttr)
  8. .use(remark2rehype)
  9. .use(stringify)
  10. .process( testFile, (err, file) => {
  11. console.log(String(file))
  12. } )

Output :

  1. $ node index.js
  2. <p>Here a test :</p>
  3. <p><img src="https://ache.one/res/ache.svg" alt="ache avatar" height="100"></p>

API

remarkAttr([options])

Parse attributes of markdown elements.

remarkAttr.SUPPORTED_ELEMENTS

The list of currently supported elements.

['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading']

['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading', 'fencedCode', 'reference', 'footnoteCall', 'autoLink']

Options
options.allowDangerousDOMEventHandlers

Whether to allow the use of on-* attributes. They are depreciated and disabled by default for security reasons. Its a boolean (default: false).
If allowed, DOM event handlers will be added to the global scope.

options.elements

The list of elements which the attributes should be parsed.
It’s a list of string, a sub-list of SUPPORTED_ELEMENTS.
If you are confident enough you can add the name of a tokenizer that isn’t officialy supported but remember that it will not have been tested.

options.extend

An object that extends the list of attributes supported for some elements.

Example : extend: {heading: ['original', 'quality', 'format', 'toc']}

With this configuration, if the scope permits it, 4 mores attributes will be supported for atxHeading elements.

options.scope

A string with the value global or specific or extented or none or every.

  • none will disable the plugin.
  • global will activate only the global attributes.
  • specific will activate global and specific attributes.
  • extended will add personalized tags for some elements.
  • permissive or every will allow every attributes (except dangerous one) on every elements supported.
options.enableAtxHeaderInline

Whether to allow atx headers with attributes on the same line.

  1. ### This is a title {style="color:yellow;"}

How does it works ?

This plugin extend the syntax of [remark-parse][remark-parse] by replacing old tokenizers by new one.
The new tokenizers functions re-use the old tokenizers and md-attr-parser to parse the extended syntax.

So option.SUPPORTED_ELEMENTS are the names of the tokenizers and neither arbitrary names nor HTML tag names.
Here is the related documentation.

License

Distributed under a MIT license.