toc-and-anchor.js 835 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import uslug from 'uslug';
  2. export default class TocAndAnchorConfigurer {
  3. constructor(crowi, renderToc) {
  4. this.crowi = crowi;
  5. this.renderToc = renderToc;
  6. }
  7. configure(md) {
  8. md.use(require('markdown-it-toc-and-anchor-with-slugid').default, {
  9. anchorLinkBefore: false,
  10. anchorLinkSymbol: '',
  11. anchorLinkSymbolClassName: 'fa fa-link',
  12. anchorClassName: 'revision-head-link',
  13. slugify: this.customSlugify,
  14. })
  15. ;
  16. // set toc render function
  17. if (this.renderToc != null) {
  18. md.set({
  19. tocCallback: (tocMarkdown, tocArray, tocHtml) => {
  20. this.renderToc(tocHtml);
  21. },
  22. });
  23. }
  24. }
  25. /**
  26. * create Base64 encoded id
  27. * @param {string} header
  28. */
  29. customSlugify(header) {
  30. return encodeURIComponent(uslug(header.trim()));
  31. }
  32. }