diff --git a/src/tools/index.ts b/src/tools/index.ts index 90f132c6..e8b3380b 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -28,6 +28,7 @@ import { tool as luhnValidator } from './luhn-validator'; import { tool as maliciousLinksTester } from './malicious-links-tester'; import { tool as mathFormatsConverter } from './math-formats-converter'; import { tool as mathOcr } from './math-ocr'; +import { tool as markdownTocGenerator } from './markdown-toc-generator'; import { tool as cssXpathConverter } from './css-xpath-converter'; import { tool as cssSelectorsMemo } from './css-selectors-memo'; @@ -195,6 +196,7 @@ export const toolsByCategory: ToolCategory[] = [ hddCalculator, jsonToCsharp, jsonToGo, + markdownTocGenerator, ], }, { diff --git a/src/tools/markdown-toc-generator/index.ts b/src/tools/markdown-toc-generator/index.ts new file mode 100644 index 00000000..d6ad8972 --- /dev/null +++ b/src/tools/markdown-toc-generator/index.ts @@ -0,0 +1,12 @@ +import { Table } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Markdown toc generator', + path: '/markdown-toc-generator', + description: 'Generate a TOC from a markdown file/content', + keywords: ['markdown', 'md', 'toc', 'generator'], + component: () => import('./markdown-toc-generator.vue'), + icon: Table, + createdAt: new Date('2024-05-11'), +}); diff --git a/src/tools/markdown-toc-generator/markdown-contents.d.ts b/src/tools/markdown-toc-generator/markdown-contents.d.ts new file mode 100644 index 00000000..156e4b7c --- /dev/null +++ b/src/tools/markdown-toc-generator/markdown-contents.d.ts @@ -0,0 +1,6 @@ +declare module 'markdown-contents'{ + declare class MarkdownContents { + markdown(): string; + } + export default function Create(markdown: string):MarkdownContents; +} \ No newline at end of file diff --git a/src/tools/markdown-toc-generator/markdown-toc-generator.service.test.ts b/src/tools/markdown-toc-generator/markdown-toc-generator.service.test.ts new file mode 100644 index 00000000..d1a4d778 --- /dev/null +++ b/src/tools/markdown-toc-generator/markdown-toc-generator.service.test.ts @@ -0,0 +1,380 @@ +import { describe, expect, it } from 'vitest'; +import { + getTocMarkdown, +} from './markdown-toc-generator.service'; + +describe('markdown-toc-generator', () => { + it('Generate TOC correctly', async () => { + expect(getTocMarkdown({ + markdown: '', + })).to.equal(''); + + const sourceMarkdown = `# Some main title + +[TOC] + +## First Title + +Some text + +## Second Spaced Title + +Some text + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + +### Title with code \`var\` + +Some text + +## Last Title`; + + expect(getTocMarkdown({ + markdown: sourceMarkdown, + anchorPrefix: 'h-', + })).to.equal(`# Some main title + + +- [First Title](#h-first-title) +- [Second Spaced Title](#h-second-spaced-title) + * [Title with Link TOC](#h-title-with-link-toc) + * [Title with code \`var\`](#h-title-with-code-var) +- [Last Title](#h-last-title) + + + +## First Title + +Some text + + +## Second Spaced Title + +Some text + + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + + +### Title with code \`var\` + +Some text + + +## Last Title`); + expect(getTocMarkdown({ + markdown: sourceMarkdown, + maxLevel: 2, + })).to.equal(`# Some main title + + +- [First Title](#first-title) +- [Second Spaced Title](#second-spaced-title) +- [Last Title](#last-title) + + + +## First Title + +Some text + + +## Second Spaced Title + +Some text + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + +### Title with code \`var\` + +Some text + + +## Last Title`); + expect(getTocMarkdown({ + markdown: sourceMarkdown, + commentStyle: 'liquid', + })).to.equal(`# Some main title + +{%- # TOC START -%} +- [First Title](#first-title) +- [Second Spaced Title](#second-spaced-title) + * [Title with Link TOC](#title-with-link-toc) + * [Title with code \`var\`](#title-with-code-var) +- [Last Title](#last-title) +{%- # TOC END -%} + +{%- # TOC ANCHOR -%} +## First Title + +Some text + +{%- # TOC ANCHOR -%} +## Second Spaced Title + +Some text + +{%- # TOC ANCHOR -%} +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + +{%- # TOC ANCHOR -%} +### Title with code \`var\` + +Some text + +{%- # TOC ANCHOR -%} +## Last Title`); + expect(getTocMarkdown({ + markdown: sourceMarkdown, + generateAnchors: false, + })).to.equal(`# Some main title + + +- [First Title](#first-title) +- [Second Spaced Title](#second-spaced-title) + * [Title with Link TOC](#title-with-link-toc) + * [Title with code \`var\`](#title-with-code-var) +- [Last Title](#last-title) + + +## First Title + +Some text + +## Second Spaced Title + +Some text + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + +### Title with code \`var\` + +Some text + +## Last Title`); + expect(getTocMarkdown({ + markdown: sourceMarkdown, + indentSpaces: 4, + indentChars: '-', + concatSpaces: false, + })).to.equal(`# Some main title + + +- [First Title](#first-title) +- [Second Spaced Title](#second--spaced--title) + - [Title with Link TOC](#title-with-link-toc) + - [Title with code \`var\`](#title-with-code-var) +- [Last Title](#last-title) + + + +## First Title + +Some text + + +## Second Spaced Title + +Some text + + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + + +### Title with code \`var\` + +Some text + + +## Last Title`); + }); + + it('Regenerate TOC correctly', async () => { + expect(getTocMarkdown({ + markdown: `# Some main title + + +- [First Title](#first-title) +- [Second Spaced Title](#second--spaced--title) + - [Title with Link TOC](#title-with-link-toc) + - [Title with code \`var\`](#title-with-code-var) +- [Last Title](#last-title) + + + +## First Title + +Some text + + +## Second Spaced Title + +Some text + + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + + +### Title with code \`var\` + +Some text + + +## Last Title`, + anchorPrefix: 'h-', + })).to.equal(`# Some main title + + +- [First Title](#h-first-title) +- [Second Spaced Title](#h-second-spaced-title) + * [Title with Link TOC](#h-title-with-link-toc) + * [Title with code \`var\`](#h-title-with-code-var) +- [Last Title](#h-last-title) + + + +## First Title + +Some text + + +## Second Spaced Title + +Some text + + +### Title with Link [TOC](http://it-tools.tech) + +\`\`\` +## some bash code +echo 'test'; +\`\`\` + + +### Title with code \`var\` + +Some text + + +## Last Title`); + }); + + it('Generate distinct TOC ids', async () => { + expect(getTocMarkdown({ + markdown: `# Some main title + +[TOC] + +## Same Title 1 + +Some text + +## Same Title 1 + +Some text + +### Same title 1 + +Some text`, + anchorPrefix: 'h-', + })).to.equal(`# Some main title + + +- [Same Title 1](#h-same-title-1) +- [Same Title 1](#h-same-title-1-1) + * [Same title 1](#h-same-title-1-2) + + + +## Same Title 1 + +Some text + + +## Same Title 1 + +Some text + + +### Same title 1 + +Some text`); + }); + + it('Generate ids for non latin', async () => { + expect(getTocMarkdown({ + markdown: `# Some main title + +[TOC] + +## Привет non-latin 你好 + +Some text + +## 😄 emoji + +Some text + +### Other title 1 + +Some text`, + anchorPrefix: 'h-', + })).to.equal(`# Some main title + + +- [Привет non-latin 你好](#h--non-latin-) +- [😄 emoji](#h--emoji) + * [Other title 1](#h-other-title-1) + + + +## Привет non-latin 你好 + +Some text + + +## 😄 emoji + +Some text + + +### Other title 1 + +Some text`); + }); +}); diff --git a/src/tools/markdown-toc-generator/markdown-toc-generator.service.ts b/src/tools/markdown-toc-generator/markdown-toc-generator.service.ts new file mode 100644 index 00000000..e023c6b5 --- /dev/null +++ b/src/tools/markdown-toc-generator/markdown-toc-generator.service.ts @@ -0,0 +1,178 @@ +function stripNonLatinCharacters(text: string) { + return text.replace(/[^A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u02BB\u02EE\uA78C\d\s_-]/g, ''); +}; + +function transformInlineCode(text: string, transform: (s: string) => string) { + return text.replace(/`(.*?)`/g, (_, p) => { + return `\`${transform(p)}\``; + }); +}; + +function spacesToDash(text: string) { + return text.replace(/\s/g, '-'); +}; + +function stripHtmlTags(text: string) { + return text.replace(/<.*?>/g, ''); +}; + +function stripMarkdownLinks(text: string, replacement: string = '$1') { + return text.replace(/\[([^\]]*)\]\([^\)]*\)/g, replacement); +}; + +function concatDashes(text: string) { + return text.replace(/--+/g, '-'); +}; + +function removeUnderscoreBoldAndItalics(text: string) { + const underscoreBoldAndItalicsRegexes = ['__', '_'].map((it) => { + return new RegExp(`\\b${it}([^_\\s]|[^_\\s].*?[^_\\s])${it}\\b`, 'g'); + }); + + let result = text; + + underscoreBoldAndItalicsRegexes.forEach((regex) => { + result = result.replace(regex, '$1'); + }); + return result; +}; + +function genericAnchorGenerator(text: string, concatSpaces: boolean) { + let result = text; + result = result.toLowerCase(); + result = transformInlineCode(result, (s: string) => { + return stripNonLatinCharacters(s); + }); + result = removeUnderscoreBoldAndItalics(result); + result = stripHtmlTags(result); + result = stripMarkdownLinks(result); + result = result.trim(); + result = stripNonLatinCharacters(result); + result = spacesToDash(result); + if (concatSpaces) { + result = concatDashes(result); + } + return result; +}; + +function escapeRegExp(string: string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +interface Title { + level: number + id: string + name: string + md: string +} + +function getTitles(markdown: string, idGenerator: (titleMarkdownContent: string) => string) { + const titles: Title[] = []; + + markdown = markdown.replace(/^```[\s\S]*?\n```/mg, () => { + return ''; + }); + markdown = markdown.replace(/^~~~[\s\S]*?\n~~~/mg, () => { + return ''; + }); + + [...markdown.matchAll(/^(#+)(.*$)/mg)].forEach( + ([match, levelString, titleContent]) => { + const level = levelString.length; + + titles.push({ + md: match, + level, + id: idGenerator(titleContent), + name: titleContent.trim(), + }); + }); + + return titles; +}; + +export function getTocMarkdown({ + markdown, + generateAnchors = true, + indentChars = '-*+', + indentSpaces = 2, + maxLevel = -1, + anchorPrefix = '', + concatSpaces = true, + commentStyle = 'html', +}: { + markdown: string + generateAnchors?: boolean + indentChars?: string + indentSpaces?: number + maxLevel?: number + anchorPrefix?: string + concatSpaces?: boolean + commentStyle?: 'html' | 'liquid' +}) { + const allIds: { [id: string]: number } = {}; + const getFinalId = (id: string) => { + if (typeof allIds[id] === 'undefined') { + allIds[id] = 0; + return id; + } + else { + allIds[id] += 1; + return `${id}-${allIds[id]}`; + } + }; + const titles = getTitles(markdown, titleContent => getFinalId(genericAnchorGenerator(titleContent, concatSpaces))); + + const createLink = (linkText: string, url: string) => { + return `[${linkText.replace('[', '\\[').replace(']', '\\]')}](${url.replace('(', '%28').replace(')', '%29')})`; + }; + + let markdownTOC = ''; + let resultMarkdown = markdown; + const commentOpen = commentStyle === 'html' ? '' : '-%}'; + + resultMarkdown = resultMarkdown.replace( + new RegExp(`\n${commentOpen} TOC START.*?TOC END ${commentClose}\n`, 'smg'), + '\n[TOC]\n', + ); + resultMarkdown = resultMarkdown.replace( + new RegExp(`^${commentOpen} TOC ANCHOR.*?\n`, 'mg'), + '', + ); + + titles.forEach((title) => { + if (title.level === 1) { + return; + } + + if (maxLevel > 0 && title.level > maxLevel) { + return; + } + + const level = title.level - 2; + let offset = ''; + if (level) { + offset = `${Array.from({ length: level * indentSpaces }).join(' ')} `; + } + const bulletChar = indentChars[level] ?? indentChars.slice(-1)[0]; + + const anchorName = `${anchorPrefix}${title.id}`; + + markdownTOC += `${offset}${bulletChar} ${createLink(stripMarkdownLinks(title.name), `#${anchorName}`)}\n`; + + if (generateAnchors) { + resultMarkdown = resultMarkdown.replace( + new RegExp(`(?\n${title.md}`, + ); + } + }); + + resultMarkdown = resultMarkdown.replace( + /^\[TOC\]\n/mg, + `${commentOpen} TOC START ${commentClose}\n${markdownTOC}${commentOpen} TOC END ${commentClose}\n`, + ); + + return resultMarkdown; +} diff --git a/src/tools/markdown-toc-generator/markdown-toc-generator.vue b/src/tools/markdown-toc-generator/markdown-toc-generator.vue new file mode 100644 index 00000000..a479829c --- /dev/null +++ b/src/tools/markdown-toc-generator/markdown-toc-generator.vue @@ -0,0 +1,127 @@ + + +