GithubHelp home page GithubHelp logo

gerhobbelt / markdown-it-named-headers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leff/markdown-it-named-headers

0.0 2.0 0.0 10 KB

Plugin for markdown-it markdown parser. Adds name attributes to header tags in output.

License: The Unlicense

JavaScript 100.00%

markdown-it-named-headers's Introduction

Markdown-it Named Headers

A plugin for markdown-it. Makes header elments have identifer attributes.

# Example Header   -->   <h1 id="example-header">Example</h1>

By default, it uses string.js's slugify to translate header text into a url safe name. You can override this. See Options.

Cribbed heavily from https://github.com/valeriangalliat/markdown-it-anchor

Install

npm install --save-dev markdown-it-named-headers

Usage

Use with plain old node:

var md   = require('markdown-it'),
    mdnh = require('markdown-it-named-headers');

md.use(mdnh, options);

Use as part of a Gulp workflow: (Note: You don't need to require named-headers in your gulpfile. gulp-markdown-it takes care of that for you).

var gulp = require('gulp'),
    md = require('gulp-markdown-it');
gulp.task('md', [], function() {
    return gulp.src( '**/*.md' )
        .pipe(md({
            plugins: ['markdown-it-named-headers']
        }))
        .pipe(gulp.dest('dist'));
});

Options

Slugify

{
   slugify: my_slug_function
}

If string.js's slugify doesn't fit your needs, you can simply pass in your own slugify function. Basically, the API is: accept any string, return a string suitable for a name attribute. Example:

function slugify(input_string) {
    var output_string = my_transform_logic(input_string);
    return output_string;
}

Since we use IDs, we should avoid duplicating them. A second parameter is passed. It is an empty object that will persist across a single call to render. In other words, you can use it to maintain a hash of used_headers per page.

The default slugify method looks something like this:

function slugify(input_string, used_headers) {
  var slug = string(input_string).slugify().toString();
  if( used_headers[slug] ) {
    used_headers[slug]++;
    slug += used_headers[slug];
  } else {
    used_headers[slug] += '-' + 1;
  }
  return slug;
}

markdown-it-named-headers's People

Contributors

arve0 avatar crissov avatar leff avatar

Watchers

 avatar  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.