diff --git a/packages/mermaid/src/docs.mts b/packages/mermaid/src/docs.mts index 22d64faf8..d08d7c858 100644 --- a/packages/mermaid/src/docs.mts +++ b/packages/mermaid/src/docs.mts @@ -35,7 +35,7 @@ import { exec } from 'child_process'; import { globby } from 'globby'; import { JSDOM } from 'jsdom'; import type { Code, Root } from 'mdast'; -import { posix, dirname } from 'path'; +import { posix, dirname, relative } from 'path'; import prettier from 'prettier'; import { remark } from 'remark'; // @ts-ignore No typescript declaration file @@ -55,10 +55,6 @@ const noHeader: boolean = process.argv.includes('--noHeader') || vitepress; const SOURCE_DOCS_DIR = 'src/docs'; const FINAL_DOCS_DIR = vitepress ? 'src/vitepress' : '../../docs'; -const AUTOGENERATED_TEXT = `> **Warning** -> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. -> ## Please edit the corresponding file in package/mermaid/src/docs.`; - const LOGMSG_TRANSFORMED = 'transformed'; const LOGMSG_TO_BE_TRANSFORMED = 'to be transformed'; const LOGMSG_COPIED = `, and copied to ${FINAL_DOCS_DIR}`; @@ -76,6 +72,17 @@ const prettierConfig: prettier.Config = { let filesWereTransformed = false; +const generateHeader = (file: string): string => { + // path from file in docs/* to repo root, e.g ../ or ../../ */ + const relativePath = relative(file, SOURCE_DOCS_DIR); + const filePathFromRoot = posix.join('/packages/mermaid', file); + const sourcePathRelativeToGenerated = posix.join(relativePath, filePathFromRoot); + return ` +> **Warning** +> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. +> ## Please edit the corresponding file in [${filePathFromRoot}](${sourcePathRelativeToGenerated}).`; +}; + /** * Given a source file name and path, return the documentation destination full path and file name * Create the destination path if it does not already exist. @@ -180,8 +187,8 @@ const transformMarkdown = (file: string) => { let transformed = remark.stringify(out); if (!noHeader) { - // Add the AUTOGENERATED_TEXT to the start of the file - transformed = `${AUTOGENERATED_TEXT}\n${transformed}`; + // Add the header to the start of the file + transformed = `${generateHeader(file)}\n${transformed}`; } if (vitepress && file === 'src/docs/index.md') { @@ -224,7 +231,7 @@ const transformHtml = (filename: string) => { const jsdom = new JSDOM(fileContents); const htmlDoc = jsdom.window.document; - const autoGeneratedComment = jsdom.window.document.createComment(AUTOGENERATED_TEXT); + const autoGeneratedComment = jsdom.window.document.createComment(generateHeader(fileName)); const rootElement = htmlDoc.documentElement; rootElement.prepend(autoGeneratedComment);