toc-and-anchor.js 859 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. tocLastLevel: 3,
  10. anchorLinkBefore: false,
  11. anchorLinkSymbol: '',
  12. anchorLinkSymbolClassName: 'icon-link',
  13. anchorClassName: 'revision-head-link',
  14. slugify: this.customSlugify,
  15. })
  16. ;
  17. // set toc render function
  18. if (this.renderToc != null) {
  19. md.set({
  20. tocCallback: (tocMarkdown, tocArray, tocHtml) => {
  21. this.renderToc(tocHtml);
  22. },
  23. });
  24. }
  25. }
  26. /**
  27. * create Base64 encoded id
  28. * @param {string} header
  29. */
  30. customSlugify(header) {
  31. return encodeURIComponent(uslug(header.trim()));
  32. }
  33. }