|
| 1 | +import { NotionPage } from "../NotionPage"; |
| 2 | +import { makeSamplePageObject, oneBlockToMarkdown } from "./pluginTestRun"; |
| 3 | +import { IDocuNotionContext, IPlugin } from "./pluginTypes"; |
| 4 | + |
| 5 | +test("raw url inside a mermaid codeblock gets converted to path using slug of that page", async () => { |
| 6 | + const targetPageId = "123"; |
| 7 | + const targetPage: NotionPage = makeSamplePageObject({ |
| 8 | + slug: "slug-of-target", |
| 9 | + name: "My Target Page", |
| 10 | + id: targetPageId, |
| 11 | + }); |
| 12 | + |
| 13 | + const input = { |
| 14 | + type: "code", |
| 15 | + code: { |
| 16 | + caption: [], |
| 17 | + rich_text: [ |
| 18 | + { |
| 19 | + type: "text", |
| 20 | + text: { |
| 21 | + content: `click A "https://www.notion.so/native/metapages/A-Page-${targetPageId}"`, |
| 22 | + link: null, |
| 23 | + }, |
| 24 | + annotations: { |
| 25 | + bold: false, |
| 26 | + italic: false, |
| 27 | + strikethrough: false, |
| 28 | + underline: false, |
| 29 | + code: false, |
| 30 | + color: "default", |
| 31 | + }, |
| 32 | + plain_text: `click A "https://www.notion.so/native/metapages/A-Page-${targetPageId}"`, |
| 33 | + href: null, |
| 34 | + }, |
| 35 | + ], |
| 36 | + language: "mermaid", // notion assumed javascript in my test in which I didn't specify a language |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + const mermaidLinks: IPlugin = { |
| 41 | + name: "mermaidLinks", |
| 42 | + regexMarkdownModifications: [ |
| 43 | + { |
| 44 | + regex: /```mermaid\n.*"(https:\/\/www\.notion\.so\S+)"/, |
| 45 | + includeCodeBlocks: true, |
| 46 | + getReplacement: async ( |
| 47 | + context: IDocuNotionContext, |
| 48 | + match: RegExpExecArray |
| 49 | + ) => { |
| 50 | + const url = match[1]; |
| 51 | + const docusaurusUrl = |
| 52 | + context.convertNotionLinkToLocalDocusaurusLink(url); |
| 53 | + // eslint-disable-next-line @typescript-eslint/await-thenable |
| 54 | + return await match[0].replace(url, docusaurusUrl); |
| 55 | + }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }; |
| 59 | + |
| 60 | + const config = { |
| 61 | + plugins: [ |
| 62 | + // standardInternalLinkConversion, |
| 63 | + // standardExternalLinkConversion, |
| 64 | + mermaidLinks, |
| 65 | + ], |
| 66 | + }; |
| 67 | + const results = await oneBlockToMarkdown(config, input, targetPage); |
| 68 | + expect(results.trim()).toContain(`click A "/slug-of-target"`); |
| 69 | +}); |
0 commit comments