项目作者: dweidner

项目描述 :
Add attribution to your quotations
高级语言: JavaScript
项目地址: git://github.com/dweidner/markdown-it-attribution.git
创建时间: 2019-09-19T15:10:27Z
项目社区:https://github.com/dweidner/markdown-it-attribution

开源协议:MIT License

下载


markdown-it-attribution

NPM version
Build Status
Coverage Status

A plugin for markdown-it that
generates accessible markup for block quotes with attribution line. The
generated output follows the suggestions in the living standard of the
WHATWG.

Requires markdown-it v5.+

The plugin allows to provide an attribution for a quotation:

  1. > That's one small step for [a] man, one giant leap for mankind.
  2. > — Neil Armstrong (1969, July 21)

The generated markup is not only accessible for screen readers but allows you to style the attribution line however you like:

  1. <figure class="c-blockquote">
  2. <blockquote>
  3. <p>
  4. That's one small step for [a] man, one giant leap for mankind.
  5. </p>
  6. </blockquote>
  7. <figcaption class="c-blockquote__attribution">
  8. Neil Armstrong (1969, July 21)
  9. </figcaption>
  10. </figure>

By default an em dash is used as the marker for an attribution line. Note that the em dash has to follow a soft break or has to be the first character of a new paragraph. This restriction should avoid situation in which an em dash is used within the body of a quotation. You can customize the characters used as an attribution marker. Have a look at the available plugin options for more details.

Install

node:

  1. npm install --save markdown-it markdown-it-attribution

Usage

  1. var md = require('markdown-it')()
  2. .use(require('markdown-it-attribution'));
  3. var blockquote = [
  4. '> That\'s one small step for [a] man, one giant leap for mankind.',
  5. '> — Neil Armstrong (1969, July 21)'
  6. ];
  7. md.render(blockquote.join('\n'));

Options

You can customize the plugin behavior by providing custom options when you register the parser plugin:

  1. var md = require('markdown-it')()
  2. .use(require('markdown-it-attribution'), {
  3. classNameContainer: 'c-quote',
  4. classNameAttribution: 'c-quote__attribution',
  5. marker: '--',
  6. removeMarker: false,
  7. });
  8. md.render('…');

List of available options:

  • [classNameContainer='c-blockquote'] - Select the HTML class added to the container of the blockquote.
  • [classNameAttribution='c-blockquote__attribution'] - Select the HTML class added to the container of an attribution line.
  • [marker='—'] - Select the characters used to identify the beginning of an attribution line.
  • [removeMarker=true] - Determines whether the attribution marker will be included in the generated markup.

Customization

Like always you can customize the output of all the elements generated by markdown-it. If you want to change the HTML element used for the container and the attribution you can provide your own template functions:

  1. // Setup the markdown it parser.
  2. var md = require('markdown-it')()
  3. .use(require('markdown-it-attribution'));
  4. /**
  5. * A utility function used to generate custom template functions which returns
  6. * the markup for an HTML tag.
  7. *
  8. * @param {string} name The name of the HTML tag.
  9. * @param {Boolean} open Whether an opening or closing tag should be generated.
  10. * @return {Function}
  11. */
  12. function tag (name, open) {
  13. return function () {
  14. return open ? '<' + name + '>' : '</' + name + '>';
  15. }
  16. }
  17. // Overwrite the template functions for the various elements.
  18. md.renderer.rules.blockquote_container_open = tag('aside', true);
  19. md.renderer.rules.blockquote_attribution_open = tag('div', true);
  20. md.renderer.rules.blockquote_attribution_close = tag('div', false);
  21. md.renderer.rules.blockquote_container_close = tag('aside', false);

Motivation

I wanted to add an attribution line to the generated markup of some of my block quotes (including the cite attribute) and searched for an unobtrusive way. The current solution still looks good in applications that do not support the custom syntax and provides accessible markup otherwise.

License

MIT