marquage/parsedownanchors

extends Parsedown-extra to automatically add id anchors with unique slugs

0.0.2 2019-05-15 17:35 UTC

This package is auto-updated.

Last update: 2024-04-16 04:46:47 UTC


README

Software License Quality Score

This tiny extension is part of the larger markdown-driven note-taking site. Although it is primarily aimed for building table of contents, this package accomplishes two things only:

  • It automatically generates id-anchors for headers (h1-h6), using Cocur/Slugify. It does not require writing down header slugs ahead of time.
  • It ensures that each id is unique. From the simple included test:
## The Site  
## The Site

will turn to:

<h2 id="the-site">The Site</h2>
<h2 id="the-site-1">The Site</h2>

For the larger project that relies on the excellent Tocbot, that's all that was needed but I could not find an extension that addressed these simple concerns. There are other packages for Parsedown TOC, such as these two.

Install

Via Composer

$ composer require marquage/parsedownanchors

Usage

In the unlikely case that you would only need this tiny package, you can call it instead of Parsedown since it is an extension and you would have access to all the rich features of Parsedown-extra.

$parser = new ParsedownSlugified();
$file = file_get_contents('[...]');
return $parser->parse($file);

Testing

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information.