toc-and-anchor.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. // console.log(tocHtml);
  20. },
  21. });
  22. }
  23. /**
  24. * create Base64 encoded id
  25. * @see https://qiita.com/satokaz/items/64582da4640898c4bf42
  26. * @param {string} header
  27. */
  28. customSlugify(header) {
  29. return encodeURIComponent(header.trim()
  30. .toLowerCase()
  31. .replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~]/g, '')
  32. .replace(/\s+/g, '-')) // Replace spaces with hyphens
  33. .replace(/\-+$/, ''); // Replace trailing hyphen
  34. }
  35. }