toc-and-anchor.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export default class TocAndAnchorConfigurer {
  2. constructor(crowi) {
  3. this.crowi = crowi;
  4. }
  5. configure(md) {
  6. md.use(require('markdown-it-toc-and-anchor').default, {
  7. anchorLinkBefore: false,
  8. anchorLinkSymbol: '',
  9. anchorLinkSymbolClassName: 'fa fa-link',
  10. anchorClassName: 'revision-head-link',
  11. })
  12. .use(require('markdown-it-named-headers'), { // overwrite id defined by markdown-it-toc-and-anchor
  13. slugify: this.customSlugify,
  14. })
  15. ;
  16. md.set({
  17. tocCallback: (tocMarkdown, tocArray, tocHtml) => {
  18. // TODO impl
  19. // $('#revision-toc').append(`
  20. // <div id="revision-toc-content" class="revision-toc-content collapse in">
  21. // ${tocHtml}
  22. // </div>`);
  23. },
  24. });
  25. }
  26. /**
  27. * create Base64 encoded id
  28. * @see https://qiita.com/satokaz/items/64582da4640898c4bf42
  29. * @param {string} header
  30. */
  31. customSlugify(header) {
  32. return encodeURIComponent(header.trim()
  33. .toLowerCase()
  34. .replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~]/g, '')
  35. .replace(/\s+/g, '-')) // Replace spaces with hyphens
  36. .replace(/\-+$/, ''); // Replace trailing hyphen
  37. }
  38. }