From 725789372481baef6ba20aaf37a82dc5ca126b2e Mon Sep 17 00:00:00 2001 From: Andrew McClenaghan Date: Thu, 4 May 2023 21:07:42 +1000 Subject: [PATCH] feat: ADF To Markdown --- package-lock.json | 83 +- packages/lib/jest.config.ts | 281 +++--- packages/lib/package.json | 7 +- packages/lib/src/ADFToMarkdown.test.ts | 920 ++++++++++++++++++ packages/lib/src/ADFToMarkdown.ts | 282 ++++++ packages/lib/src/AdfEqual.ts | 2 +- packages/lib/src/AdfProcessing.ts | 1 - .../__snapshots__/ADFToMarkdown.test.ts.snap | 110 +++ packages/lib/src/index.ts | 1 + packages/obsidian/src/main.ts | 33 + 10 files changed, 1549 insertions(+), 171 deletions(-) create mode 100644 packages/lib/src/ADFToMarkdown.test.ts create mode 100644 packages/lib/src/ADFToMarkdown.ts create mode 100644 packages/lib/src/__snapshots__/ADFToMarkdown.test.ts.snap diff --git a/package-lock.json b/package-lock.json index f8902188..b4763274 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3761,6 +3761,15 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/lodash-es": { + "version": "4.17.7", + "resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.7.tgz", + "integrity": "sha512-z0ptr6UI10VlU6l5MYhGwS4mC8DZyYer2mCoyysZtSF7p26zOX8UpbrV0YpNYLGS8K4PUFIyEr62IMFFjveSiQ==", + "dev": true, + "dependencies": { + "@types/lodash": "*" + } + }, "node_modules/@types/markdown-it": { "version": "12.2.3", "dev": true, @@ -11101,7 +11110,8 @@ }, "node_modules/lodash-es": { "version": "4.17.21", - "license": "MIT" + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" }, "node_modules/lodash.memoize": { "version": "4.1.2", @@ -11327,6 +11337,15 @@ "uc.micro": "^1.0.1" } }, + "node_modules/markdown-table": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.3.tgz", + "integrity": "sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, "node_modules/matcher": { "version": "3.0.0", "license": "MIT", @@ -15997,14 +16016,14 @@ }, "packages/cli": { "name": "@markdown-confluence/cli", - "version": "4.9.0", + "version": "5.0.1", "license": "Apache 2.0", "bin": { "cli": "dist/index.js" }, "devDependencies": { - "@markdown-confluence/lib": "4.9.0", - "@markdown-confluence/mermaid-puppeteer-renderer": "4.9.0", + "@markdown-confluence/lib": "5.0.1", + "@markdown-confluence/mermaid-puppeteer-renderer": "5.0.1", "boxen": "7.0.2", "chalk": "5.2.0", "confluence.js": "^1.6.3", @@ -16025,7 +16044,7 @@ }, "packages/lib": { "name": "@markdown-confluence/lib", - "version": "4.9.0", + "version": "5.0.1", "license": "Apache 2.0", "dependencies": { "@atlaskit/adf-schema": "^25.5.0", @@ -16037,8 +16056,9 @@ "glob": "^10.2.2", "gray-matter": "^4.0.3", "image-size": "^1.0.2", - "lodash": "^4.17.21", + "lodash-es": "^4.17.21", "markdown-it-table": "^2.0.4", + "markdown-table": "^3.0.3", "prosemirror-markdown": "^1.10.1", "prosemirror-model": "1.14.3", "sort-any": "^4.0.5", @@ -16047,7 +16067,7 @@ }, "devDependencies": { "@jest/globals": "^29.5.0", - "@types/lodash": "^4.14.194", + "@types/lodash-es": "^4.14.194", "@types/markdown-it": "^12.2.3", "@types/prosemirror-model": "1.16.2", "@types/prosemirror-state": "^1.3.0", @@ -16552,32 +16572,32 @@ }, "packages/mermaid-electron-renderer": { "name": "@markdown-confluence/mermaid-electron-renderer", - "version": "4.9.0", + "version": "5.0.1", "license": "Apache 2.0", "dependencies": { "@electron/remote": "^2.0.9", - "@markdown-confluence/lib": "4.9.0", + "@markdown-confluence/lib": "5.0.1", "mermaid": "^10.1.0" }, "devDependencies": {} }, "packages/mermaid-puppeteer-renderer": { "name": "@markdown-confluence/mermaid-puppeteer-renderer", - "version": "4.9.0", + "version": "5.0.1", "license": "Apache 2.0", "dependencies": { - "@markdown-confluence/lib": "4.9.0", + "@markdown-confluence/lib": "5.0.1", "mermaid": "10.1.0", "puppeteer": "20.0.0" } }, "packages/obsidian": { "name": "obsidian-confluence", - "version": "4.9.0", + "version": "5.0.1", "license": "Apache 2.0", "dependencies": { - "@markdown-confluence/lib": "4.9.0", - "@markdown-confluence/mermaid-electron-renderer": "4.9.0", + "@markdown-confluence/lib": "5.0.1", + "@markdown-confluence/mermaid-electron-renderer": "5.0.1", "confluence.js": "^1.6.3", "mime-types": "^2.1.35", "react": "^16.14.0", @@ -18908,8 +18928,8 @@ "@markdown-confluence/cli": { "version": "file:packages/cli", "requires": { - "@markdown-confluence/lib": "4.9.0", - "@markdown-confluence/mermaid-puppeteer-renderer": "4.9.0", + "@markdown-confluence/lib": "5.0.1", + "@markdown-confluence/mermaid-puppeteer-renderer": "5.0.1", "boxen": "7.0.2", "chalk": "5.2.0", "confluence.js": "^1.6.3", @@ -18932,7 +18952,7 @@ "@atlaskit/editor-common": "^72.4.0", "@atlaskit/editor-json-transformer": "^8.8.4", "@jest/globals": "^29.5.0", - "@types/lodash": "^4.14.194", + "@types/lodash-es": "^4.14.194", "@types/markdown-it": "^12.2.3", "@types/prosemirror-model": "1.16.2", "@types/prosemirror-state": "^1.3.0", @@ -18946,9 +18966,10 @@ "gray-matter": "^4.0.3", "image-size": "^1.0.2", "jest": "^29.5.0", - "lodash": "^4.17.21", + "lodash-es": "^4.17.21", "markdown-it": "^13.0.1", "markdown-it-table": "^2.0.4", + "markdown-table": "^3.0.3", "prosemirror-markdown": "^1.10.1", "prosemirror-model": "1.14.3", "sort-any": "^4.0.5", @@ -19351,14 +19372,14 @@ "version": "file:packages/mermaid-electron-renderer", "requires": { "@electron/remote": "^2.0.9", - "@markdown-confluence/lib": "4.9.0", + "@markdown-confluence/lib": "5.0.1", "mermaid": "^10.1.0" } }, "@markdown-confluence/mermaid-puppeteer-renderer": { "version": "file:packages/mermaid-puppeteer-renderer", "requires": { - "@markdown-confluence/lib": "4.9.0", + "@markdown-confluence/lib": "5.0.1", "mermaid": "10.1.0", "puppeteer": "20.0.0" } @@ -19624,6 +19645,15 @@ "version": "4.14.194", "dev": true }, + "@types/lodash-es": { + "version": "4.17.7", + "resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.7.tgz", + "integrity": "sha512-z0ptr6UI10VlU6l5MYhGwS4mC8DZyYer2mCoyysZtSF7p26zOX8UpbrV0YpNYLGS8K4PUFIyEr62IMFFjveSiQ==", + "dev": true, + "requires": { + "@types/lodash": "*" + } + }, "@types/markdown-it": { "version": "12.2.3", "dev": true, @@ -24634,7 +24664,9 @@ "version": "4.17.21" }, "lodash-es": { - "version": "4.17.21" + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" }, "lodash.memoize": { "version": "4.1.2", @@ -24796,6 +24828,11 @@ "markdown-it-table": { "version": "2.0.4" }, + "markdown-table": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.3.tgz", + "integrity": "sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==" + }, "matcher": { "version": "3.0.0", "optional": true, @@ -25275,8 +25312,8 @@ "obsidian-confluence": { "version": "file:packages/obsidian", "requires": { - "@markdown-confluence/lib": "4.9.0", - "@markdown-confluence/mermaid-electron-renderer": "4.9.0", + "@markdown-confluence/lib": "5.0.1", + "@markdown-confluence/mermaid-electron-renderer": "5.0.1", "@types/mime-types": "^2.1.1", "@types/react-dom": "^18.0.11", "confluence.js": "^1.6.3", diff --git a/packages/lib/jest.config.ts b/packages/lib/jest.config.ts index b65b668a..0927b78f 100644 --- a/packages/lib/jest.config.ts +++ b/packages/lib/jest.config.ts @@ -5,200 +5,195 @@ */ export default { - // All imported modules in your tests should be mocked automatically - // automock: false, + // All imported modules in your tests should be mocked automatically + // automock: false, - // Stop running tests after `n` failures - // bail: 0, + // Stop running tests after `n` failures + // bail: 0, - // The directory where Jest should store its cached dependency information - // cacheDirectory: "/private/var/folders/qx/1pyrtldd3nb1l0p44xbmd97h0000gn/T/jest_dx", + // The directory where Jest should store its cached dependency information + // cacheDirectory: "/private/var/folders/qx/1pyrtldd3nb1l0p44xbmd97h0000gn/T/jest_dx", - // Automatically clear mock calls, instances, contexts and results before every test - // clearMocks: false, + // Automatically clear mock calls, instances, contexts and results before every test + // clearMocks: false, - // Indicates whether the coverage information should be collected while executing the test - // collectCoverage: false, + // Indicates whether the coverage information should be collected while executing the test + // collectCoverage: false, - // An array of glob patterns indicating a set of files for which coverage information should be collected - // collectCoverageFrom: undefined, + // An array of glob patterns indicating a set of files for which coverage information should be collected + // collectCoverageFrom: undefined, - // The directory where Jest should output its coverage files - // coverageDirectory: undefined, + // The directory where Jest should output its coverage files + // coverageDirectory: undefined, - // An array of regexp pattern strings used to skip coverage collection - // coveragePathIgnorePatterns: [ - // "/node_modules/" - // ], + // An array of regexp pattern strings used to skip coverage collection + // coveragePathIgnorePatterns: [ + // "/node_modules/" + // ], - // Indicates which provider should be used to instrument code for coverage - coverageProvider: "v8", + // Indicates which provider should be used to instrument code for coverage + coverageProvider: "v8", - // A list of reporter names that Jest uses when writing coverage reports - // coverageReporters: [ - // "json", - // "text", - // "lcov", - // "clover" - // ], + // A list of reporter names that Jest uses when writing coverage reports + // coverageReporters: [ + // "json", + // "text", + // "lcov", + // "clover" + // ], - // An object that configures minimum threshold enforcement for coverage results - // coverageThreshold: undefined, + // An object that configures minimum threshold enforcement for coverage results + // coverageThreshold: undefined, - // A path to a custom dependency extractor - // dependencyExtractor: undefined, + // A path to a custom dependency extractor + // dependencyExtractor: undefined, - // Make calling deprecated APIs throw helpful error messages - // errorOnDeprecated: false, + // Make calling deprecated APIs throw helpful error messages + // errorOnDeprecated: false, - // The default configuration for fake timers - // fakeTimers: { - // "enableGlobally": false - // }, + // The default configuration for fake timers + // fakeTimers: { + // "enableGlobally": false + // }, - // Force coverage collection from ignored files using an array of glob patterns - // forceCoverageMatch: [], + // Force coverage collection from ignored files using an array of glob patterns + // forceCoverageMatch: [], - // A path to a module which exports an async function that is triggered once before all test suites - // globalSetup: undefined, + // A path to a module which exports an async function that is triggered once before all test suites + // globalSetup: undefined, - // A path to a module which exports an async function that is triggered once after all test suites - // globalTeardown: undefined, + // A path to a module which exports an async function that is triggered once after all test suites + // globalTeardown: undefined, - // A set of global variables that need to be available in all test environments - // globals: {}, + // A set of global variables that need to be available in all test environments + // globals: {}, - // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. - // maxWorkers: "50%", + // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. + // maxWorkers: "50%", - // An array of directory names to be searched recursively up from the requiring module's location - // moduleDirectories: [ - // "node_modules" - // ], + // An array of directory names to be searched recursively up from the requiring module's location + // moduleDirectories: [ + // "node_modules" + // ], - // An array of file extensions your modules use - // moduleFileExtensions: [ - // "js", - // "mjs", - // "cjs", - // "jsx", - // "ts", - // "tsx", - // "json", - // "node" - // ], + // An array of file extensions your modules use + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], - // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module - // moduleNameMapper: {}, + // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module + // moduleNameMapper: {}, - // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader - // modulePathIgnorePatterns: [], + // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader + // modulePathIgnorePatterns: [], - // Activates notifications for test results - // notify: false, + // Activates notifications for test results + // notify: false, - // An enum that specifies notification mode. Requires { notify: true } - // notifyMode: "failure-change", + // An enum that specifies notification mode. Requires { notify: true } + // notifyMode: "failure-change", - // A preset that is used as a base for Jest's configuration - // preset: undefined, - preset: 'ts-jest/presets/js-with-ts', + // A preset that is used as a base for Jest's configuration + // preset: undefined, + preset: "ts-jest/presets/js-with-ts-esm", - // Run tests from one or more projects - // projects: undefined, + // Run tests from one or more projects + // projects: undefined, - // Use this configuration option to add custom reporters to Jest - reporters: [ - 'default', - 'github-actions', - ], + // Use this configuration option to add custom reporters to Jest + reporters: ["default", "github-actions"], - // Automatically reset mock state before every test - // resetMocks: false, + // Automatically reset mock state before every test + // resetMocks: false, - // Reset the module registry before running each individual test - // resetModules: false, + // Reset the module registry before running each individual test + // resetModules: false, - // A path to a custom resolver - // resolver: undefined, + // A path to a custom resolver + // resolver: undefined, - // Automatically restore mock state and implementation before every test - // restoreMocks: false, + // Automatically restore mock state and implementation before every test + // restoreMocks: false, - // The root directory that Jest should scan for tests and modules within - // rootDir: undefined, + // The root directory that Jest should scan for tests and modules within + // rootDir: undefined, - // A list of paths to directories that Jest should use to search for files in - // roots: [ - // "" - // ], + // A list of paths to directories that Jest should use to search for files in + // roots: [ + // "" + // ], - // Allows you to use a custom runner instead of Jest's default test runner - // runner: "jest-runner", + // Allows you to use a custom runner instead of Jest's default test runner + // runner: "jest-runner", - // The paths to modules that run some code to configure or set up the testing environment before each test - // setupFiles: [], + // The paths to modules that run some code to configure or set up the testing environment before each test + // setupFiles: [], - // A list of paths to modules that run some code to configure or set up the testing framework before each test - // setupFilesAfterEnv: [], + // A list of paths to modules that run some code to configure or set up the testing framework before each test + // setupFilesAfterEnv: [], - // The number of seconds after which a test is considered as slow and reported as such in the results. - // slowTestThreshold: 5, + // The number of seconds after which a test is considered as slow and reported as such in the results. + // slowTestThreshold: 5, - // A list of paths to snapshot serializer modules Jest should use for snapshot testing - // snapshotSerializers: [], + // A list of paths to snapshot serializer modules Jest should use for snapshot testing + // snapshotSerializers: [], - // The test environment that will be used for testing - // testEnvironment: "jest-environment-node", + // The test environment that will be used for testing + // testEnvironment: "jest-environment-node", - // Options that will be passed to the testEnvironment - // testEnvironmentOptions: {}, + // Options that will be passed to the testEnvironment + // testEnvironmentOptions: {}, - // Adds a location field to test results - // testLocationInResults: false, + // Adds a location field to test results + // testLocationInResults: false, - // The glob patterns Jest uses to detect test files - // testMatch: [ - // "**/__tests__/**/*.[jt]s?(x)", - // "**/?(*.)+(spec|test).[tj]s?(x)" - // ], + // The glob patterns Jest uses to detect test files + // testMatch: [ + // "**/__tests__/**/*.[jt]s?(x)", + // "**/?(*.)+(spec|test).[tj]s?(x)" + // ], - // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped - // testPathIgnorePatterns: [ - // "/node_modules/" - // ], + // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped + // testPathIgnorePatterns: [ + // "/node_modules/" + // ], - // The regexp pattern or array of patterns that Jest uses to detect test files - // testRegex: [], + // The regexp pattern or array of patterns that Jest uses to detect test files + // testRegex: [], - // This option allows the use of a custom results processor - // testResultsProcessor: undefined, + // This option allows the use of a custom results processor + // testResultsProcessor: undefined, - // This option allows use of a custom test runner - // testRunner: "jest-circus/runner", + // This option allows use of a custom test runner + // testRunner: "jest-circus/runner", - // A map from regular expressions to paths to transformers - // transform: undefined, + // A map from regular expressions to paths to transformers + // transform: undefined, - // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation - // transformIgnorePatterns: [ - // "/node_modules/", - // "\\.pnp\\.[^\\/]+$" - // ], + // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation + // transformIgnorePatterns: [ + // "/node_modules/", + // "\\.pnp\\.[^\\/]+$" + // ], - // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them - // unmockedModulePathPatterns: undefined, + // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them + // unmockedModulePathPatterns: undefined, - // Indicates whether each individual test should be reported during the run - // verbose: undefined, + // Indicates whether each individual test should be reported during the run + // verbose: undefined, - // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode - // watchPathIgnorePatterns: [], + // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode + // watchPathIgnorePatterns: [], - // Whether to use watchman for file crawling - // watchman: true, - - - transformIgnorePatterns: [".*/node_modules/.*"] + // Whether to use watchman for file crawling + // watchman: true, + transformIgnorePatterns: [".*/node_modules/.*"], }; diff --git a/packages/lib/package.json b/packages/lib/package.json index 8394f161..69fa2364 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -13,7 +13,7 @@ "fmt": "npx prettier --write src/", "lint": "eslint --ignore-path ../../.eslintignore --ext .js,.ts src/", "prettier-check": "npx prettier --check src/", - "test": "jest" + "test": "NODE_OPTIONS=--experimental-vm-modules jest" }, "keywords": [ "markdown", @@ -31,7 +31,7 @@ "license": "Apache 2.0", "devDependencies": { "@jest/globals": "^29.5.0", - "@types/lodash": "^4.14.194", + "@types/lodash-es": "^4.14.194", "@types/markdown-it": "^12.2.3", "@types/prosemirror-model": "1.16.2", "@types/prosemirror-state": "^1.3.0", @@ -52,8 +52,9 @@ "glob": "^10.2.2", "gray-matter": "^4.0.3", "image-size": "^1.0.2", - "lodash": "^4.17.21", + "lodash-es": "^4.17.21", "markdown-it-table": "^2.0.4", + "markdown-table": "^3.0.3", "prosemirror-markdown": "^1.10.1", "prosemirror-model": "1.14.3", "sort-any": "^4.0.5", diff --git a/packages/lib/src/ADFToMarkdown.test.ts b/packages/lib/src/ADFToMarkdown.test.ts new file mode 100644 index 00000000..3022b2c7 --- /dev/null +++ b/packages/lib/src/ADFToMarkdown.test.ts @@ -0,0 +1,920 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import { expect, test } from "@jest/globals"; +import { renderADFDoc } from "./ADFToMarkdown"; +import { JSONDocNode } from "@atlaskit/editor-json-transformer"; + +const ADFToMDTest = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "inlineCard", + attrs: { + url: "https://testing.com/", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "inlineCard", + attrs: { + url: "https://andymac4182.atlassian.net/wiki/spaces/T2/pages/8290305", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + { + type: "decisionList", + attrs: { + localId: "39becc7a-48c3-4e2d-a6bc-35648d6689a2", + }, + content: [ + { + type: "decisionItem", + attrs: { + state: "DECIDED", + localId: "95268b83-2fc0-4801-9b4f-e5c42cc530e6", + }, + content: [ + { + text: "Decision to track", + type: "text", + }, + ], + }, + { + type: "decisionItem", + attrs: { + state: "DECIDED", + localId: "564ab974-e6fe-48d2-a144-507f5ec30906", + }, + content: [ + { + text: "Testing Decision 2", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "date", + attrs: { + timestamp: "1683244800000", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + { + type: "table", + attrs: { + layout: "default", + localId: "239ebaeb-0f55-4f40-b28e-b0982b4cc577", + }, + content: [ + { + type: "tableRow", + content: [ + { + type: "tableHeader", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing", + type: "text", + marks: [ + { + type: "strong", + }, + ], + }, + ], + }, + ], + }, + { + type: "tableHeader", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + marks: [ + { + type: "alignment", + attrs: { + align: "end", + }, + }, + ], + content: [ + { + text: "Testing 2", + type: "text", + marks: [ + { + type: "strong", + }, + ], + }, + ], + }, + ], + }, + { + type: "tableHeader", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing 3", + type: "text", + marks: [ + { + type: "strong", + }, + ], + }, + ], + }, + ], + }, + ], + }, + { + type: "tableRow", + content: [ + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Row 1", + type: "text", + }, + ], + }, + ], + }, + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + marks: [ + { + type: "alignment", + attrs: { + align: "end", + }, + }, + ], + content: [ + { + text: "Row 1.2", + type: "text", + }, + ], + }, + ], + }, + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Row 1.3", + type: "text", + }, + ], + }, + ], + }, + ], + }, + { + type: "tableRow", + content: [ + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Row 2.1", + type: "text", + }, + ], + }, + ], + }, + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + marks: [ + { + type: "alignment", + attrs: { + align: "end", + }, + }, + ], + content: [ + { + text: "Row 2.2", + type: "text", + }, + ], + }, + ], + }, + { + type: "tableCell", + attrs: { + colspan: 1, + rowspan: 1, + colwidth: [226.67], + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Row 2.3", + type: "text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + { + type: "paragraph", + content: [ + { + text: "Testing ", + type: "text", + }, + { + text: "TESTING", + type: "text", + marks: [ + { + type: "em", + }, + { + type: "link", + attrs: { + href: "https://www.google.com/webmasters/tools/siteoverview", + }, + }, + ], + }, + { + type: "hardBreak", + }, + { + text: "Testing ", + type: "text", + }, + { + text: "New", + type: "text", + marks: [ + { + type: "strong", + }, + ], + }, + { + text: " Line", + type: "text", + }, + ], + }, + { + type: "paragraph", + marks: [ + { + type: "indentation", + attrs: { + level: 1, + }, + }, + ], + content: [ + { + text: "sdfsdf", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "paragraph", + content: [ + { + text: "TESTING", + type: "text", + marks: [ + { + type: "strong", + }, + { + type: "em", + }, + ], + }, + ], + }, + { + type: "rule", + }, + { + type: "paragraph", + content: [ + { + text: "TESTING BELOW", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "heading", + attrs: { + level: 1, + }, + content: [ + { + text: "Testing Header 1", + type: "text", + }, + ], + }, + { + type: "heading", + attrs: { + level: 2, + }, + content: [ + { + text: "Header 2", + type: "text", + }, + ], + }, + { + type: "heading", + attrs: { + level: 3, + }, + content: [ + { + text: "Heading 3", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "heading", + attrs: { + level: 4, + }, + content: [ + { + text: "Heading 4", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "heading", + attrs: { + level: 5, + }, + content: [ + { + text: "Heading 5", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "heading", + attrs: { + level: 6, + }, + content: [ + { + text: "Heading 6", + type: "text", + marks: [ + { + type: "strong", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "codeBlock", + attrs: { + language: "ada", + }, + content: [ + { + text: "TESTING CODE", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "bulletList", + content: [ + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Bullets", + type: "text", + }, + ], + }, + ], + }, + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Bullets 2", + type: "text", + }, + ], + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "orderedList", + attrs: { + order: 1, + }, + content: [ + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Numbered List", + type: "text", + }, + ], + }, + ], + }, + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Numbered List 2", + type: "text", + }, + ], + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "blockquote", + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Block Quote", + type: "text", + }, + ], + }, + { + type: "paragraph", + content: [ + { + text: "Multiple Lines", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "panel", + attrs: { + panelType: "info", + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Info Panel", + type: "text", + }, + ], + }, + ], + }, + { + type: "panel", + attrs: { + panelType: "note", + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing Note Panel", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "expand", + attrs: { + title: "Expand Title", + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Testing expanded expand", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "expand", + attrs: { + title: "Closed Expand", + }, + content: [ + { + type: "paragraph", + content: [ + { + text: "Closed Expand", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "paragraph", + content: [ + { + text: "Testing ", + type: "text", + }, + { + text: "Text", + type: "text", + marks: [ + { + type: "strike", + }, + ], + }, + { + text: " ", + type: "text", + }, + { + text: "Again ", + type: "text", + marks: [ + { + type: "subsup", + attrs: { + type: "sub", + }, + }, + ], + }, + { + text: "Testing More", + type: "text", + marks: [ + { + type: "subsup", + attrs: { + type: "sup", + }, + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "paragraph", + content: [ + { + type: "mention", + attrs: { + id: "557058:aea5688c-52b9-4d15-aabe-96def1abc413", + text: "@Andrew McClenaghan", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + { + type: "paragraph", + }, + { + type: "paragraph", + content: [ + { + text: "testing", + type: "text", + marks: [ + { + type: "code", + }, + ], + }, + { + text: "code", + type: "text", + }, + ], + }, + { + type: "taskList", + attrs: { + localId: "", + }, + content: [ + { + type: "taskItem", + attrs: { + state: "TODO", + localId: "1", + }, + content: [ + { + text: "Testing ", + type: "text", + }, + { + type: "mention", + attrs: { + id: "557058:aea5688c-52b9-4d15-aabe-96def1abc413", + text: "@Andrew McClenaghan", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + { + type: "taskItem", + attrs: { + state: "DONE", + localId: "2", + }, + content: [ + { + text: "Testing 2", + type: "text", + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "panel", + attrs: { + panelType: "info", + }, + content: [ + { + type: "bulletList", + content: [ + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "Bullet List in Panel", + type: "text", + }, + ], + }, + ], + }, + ], + }, + { + type: "paragraph", + content: [ + { + text: "asdf", + type: "text", + }, + ], + }, + { + type: "orderedList", + attrs: { + order: 1, + }, + content: [ + { + type: "listItem", + content: [ + { + type: "paragraph", + content: [ + { + text: "sdfsdf", + type: "text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + { + type: "paragraph", + }, + { + type: "paragraph", + content: [ + { + type: "emoji", + attrs: { + id: "1f92f", + text: "🤯", + shortName: ":exploding_head:", + }, + }, + { + text: " ", + type: "text", + }, + ], + }, + ], + version: 1, +} satisfies JSONDocNode; + +test("ADF To Markdown Test", async () => { + const result = renderADFDoc(ADFToMDTest); + expect(result).toMatchSnapshot(); +}); diff --git a/packages/lib/src/ADFToMarkdown.ts b/packages/lib/src/ADFToMarkdown.ts new file mode 100644 index 00000000..d99c833c --- /dev/null +++ b/packages/lib/src/ADFToMarkdown.ts @@ -0,0 +1,282 @@ +import { ADFEntity } from "@atlaskit/adf-utils/dist/types/types"; +import { JSONDocNode } from "@atlaskit/editor-json-transformer"; +import { markdownTable } from "markdown-table"; + +export function renderADFDoc(adfDoc: JSONDocNode) { + const lines = (adfDoc.content ?? []).reduce((prev, curr, currentIndex) => { + if (!curr) { + return prev; + } + const result = renderADFContent(curr, adfDoc, currentIndex); + if (!result) { + return prev; + } + if (result instanceof Error) { + const createADFCodeBlock = renderCodeBlock( + "adf", + JSON.stringify(curr) + ); + return [...prev, createADFCodeBlock]; + } + return [...prev, result]; + }, [] as (string | Error)[]); + const result = lines.join("\n"); + return result; +} + +function renderTextMarks(element: ADFEntity) { + if (!element.marks || !element.text) { + return element.text; + } + + let returnText = element.text; + for (const mark of element.marks) { + switch (mark.type) { + case "strong": + returnText = `**${returnText}**`; + break; + case "em": + returnText = `*${returnText}*`; + break; + case "strike": + returnText = `~${returnText}~`; + break; + case "code": + returnText = `\`${returnText}\``; + break; + case "subsup": { + const subsupType = + mark.attrs && mark.attrs["type"] + ? mark.attrs["type"] + : "sup"; + returnText = `<${subsupType}>${returnText}`; + break; + } + case "link": { + const linkHref = + mark.attrs && mark.attrs["href"] ? mark.attrs["href"] : "#"; + returnText = `[${returnText}](${linkHref})`; + break; + } + default: + return new Error(`Unknown Mark ${mark.type}`); + } + } + return returnText; +} + +function renderADFContent( + element: ADFEntity, + parent: ADFEntity, + currentIndex: number +): string | Error | undefined { + const renderChildrenResult = renderChildren(element); + if (renderChildrenResult instanceof Error) { + return renderChildrenResult; + } + + switch (element.type) { + case "doc": { + return new Error("Call renderADFDoc"); + } + case "paragraph": { + if (parent.type.startsWith("table")) { + return renderChildrenResult; + } + return renderChildrenResult + "\n"; + } + case "text": { + return renderTextMarks(element); + } + case "hardBreak": { + return "\n"; + } + case "rule": { + return "\n---\n"; + } + case "heading": { + const headingLevel = + element.attrs && element.attrs["level"] + ? parseInt(element.attrs["level"]) + : 1; + const beforeText = "#".repeat(headingLevel); + return beforeText + " " + renderChildrenResult; + } + case "codeBlock": { + const language = + element.attrs && element.attrs["language"] + ? element.attrs["language"] + : ""; + return renderCodeBlock(language, renderChildrenResult); + } + case "taskList": + case "bulletList": + case "orderedList": { + return renderChildrenResult; + } + case "listItem": { + let prefix = "- "; + switch (parent.type) { + case "bulletList": + prefix = "- "; + break; + case "orderedList": { + const orderAttr = + element.attrs && element.attrs["order"] + ? parseInt(element.attrs["order"]) + : 1; + prefix = `${orderAttr + currentIndex}. `; + break; + } + default: + return new Error("Unhandled listItem parent"); + } + return prefix + renderChildrenResult; + } + case "blockquote": { + const result = renderChildrenResult + .split("\n") + .map((line) => (line ? `> ${line}\n` : line)) + .join(""); + + return result; + } + case "panel": { + const panelType = + element.attrs && element.attrs["panelType"] + ? element.attrs["panelType"] + : "info"; + const result = renderChildrenResult + .split("\n") + .map((line) => (line ? `> ${line}\n` : line)) + .join(""); + + const headerRow = `> [!${panelType}]\n`; + return headerRow + result; + } + case "expand": { + const title = + element.attrs && element.attrs["title"] + ? element.attrs["title"] + : "info"; + const result = renderChildrenResult + .split("\n") + .map((line) => (line ? `> ${line}\n` : line)) + .join(""); + + const headerRow = `> [!expand]+ ${title}\n`; + return headerRow + result; + } + case "mention": { + const userId = + element.attrs && element.attrs["id"] + ? element.attrs["id"] + : undefined; + const text = + element.attrs && element.attrs["text"] + ? element.attrs["text"] + : undefined; + return `[[mention:${userId}|${text}]]`; + } + case "taskItem": { + const taskState = + element.attrs && element.attrs["state"] + ? element.attrs["state"] + : "TODO"; + const taskStateMarkdown = taskState === "TODO" ? " " : "x"; + return `- [${taskStateMarkdown}] ${renderChildrenResult}\n`; + } + case "emoji": { + const emojiId = + element.attrs && element.attrs["id"] + ? element.attrs["id"] + : undefined; + + let shortName = + element.attrs && + element.attrs["shortName"] && + typeof element.attrs["shortName"] === "string" + ? element.attrs["shortName"] + : ""; + + if (shortName) { + shortName = `|${shortName.replaceAll(":", "")}`; + } + + return `:${emojiId}${shortName}:`; + } + case "inlineCard": { + const inlineCardUrl = + element.attrs && element.attrs["url"] + ? element.attrs["url"] + : undefined; + return `[${inlineCardUrl}](${inlineCardUrl})`; + } + case "table": { + return renderTable(element); + } + case "tableHeader": + case "tableRow": + case "tableCell": { + return renderChildrenResult; + } + default: + console.warn(`Unknown ADFEntity Type ${element.type}`); + return new Error(`Unknown ADFEntity Type ${element.type}`); + } +} + +function renderTable(element: ADFEntity) { + if (!element.content) { + return ""; + } + + const tableCells: string[][] = []; + + // TODO: Handle alignment + for (const tableRow of element.content) { + if (!tableRow || !tableRow.content) { + continue; + } + + const rowCells: string[] = []; + for (const tableCell of tableRow.content) { + if (!tableCell || !tableCell.content) { + continue; + } + const cellContent = renderChildren(tableCell); + if (typeof cellContent === "string") { + rowCells.push(cellContent); + } + } + tableCells.push(rowCells); + } + + return markdownTable(tableCells); +} + +function renderCodeBlock(language: string, code: string) { + return `\`\`\`${language} \n${code}\n\`\`\``; +} + +function renderChildren(element: ADFEntity) { + const lines = (element.content ?? []).reduce((prev, curr, currentIndex) => { + if (!curr || prev[0] instanceof Error) { + return prev; + } + const result = renderADFContent(curr, element, currentIndex); + if (!result) { + return prev; + } + if (result instanceof Error) { + return [result]; + } + return [...prev, result]; + }, [] as (string | Error)[]); + const firstResult = lines.at(0); + if (firstResult instanceof Error) { + return firstResult; + } + const result = lines.join(""); + return result; +} diff --git a/packages/lib/src/AdfEqual.ts b/packages/lib/src/AdfEqual.ts index 6dd4cfe4..ba72f858 100644 --- a/packages/lib/src/AdfEqual.ts +++ b/packages/lib/src/AdfEqual.ts @@ -1,5 +1,5 @@ import sortAny from "sort-any"; -import { mapValues } from "lodash"; +import { mapValues } from "lodash-es"; import { traverse } from "@atlaskit/adf-utils/traverse"; import { ADFEntity, ADFEntityMark } from "@atlaskit/adf-utils/types"; import { isEqual } from "./isEqual"; diff --git a/packages/lib/src/AdfProcessing.ts b/packages/lib/src/AdfProcessing.ts index 18b4fc68..7b47f8e5 100644 --- a/packages/lib/src/AdfProcessing.ts +++ b/packages/lib/src/AdfProcessing.ts @@ -701,7 +701,6 @@ function processWikilinkToActualLink( typeof node.marks[0].attrs["href"] === "string" && node.marks[0].attrs["href"].startsWith("mention:") ) { - console.log({ node }); const mentionUrl = new URL(node.marks[0].attrs["href"]); node = { diff --git a/packages/lib/src/__snapshots__/ADFToMarkdown.test.ts.snap b/packages/lib/src/__snapshots__/ADFToMarkdown.test.ts.snap new file mode 100644 index 00000000..c0883ad1 --- /dev/null +++ b/packages/lib/src/__snapshots__/ADFToMarkdown.test.ts.snap @@ -0,0 +1,110 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ADF To Markdown Test 1`] = ` +"[https://testing.com/](https://testing.com/) + +[https://andymac4182.atlassian.net/wiki/spaces/T2/pages/8290305](https://andymac4182.atlassian.net/wiki/spaces/T2/pages/8290305) + +\`\`\`adf +{"type":"decisionList","attrs":{"localId":"39becc7a-48c3-4e2d-a6bc-35648d6689a2"},"content":[{"type":"decisionItem","attrs":{"state":"DECIDED","localId":"95268b83-2fc0-4801-9b4f-e5c42cc530e6"},"content":[{"text":"Decision to track","type":"text"}]},{"type":"decisionItem","attrs":{"state":"DECIDED","localId":"564ab974-e6fe-48d2-a144-507f5ec30906"},"content":[{"text":"Testing Decision 2","type":"text"}]}]} +\`\`\` +\`\`\`adf +{"type":"paragraph","content":[{"type":"date","attrs":{"timestamp":"1683244800000"}},{"text":" ","type":"text"}]} +\`\`\` +| **Testing** | **Testing 2** | **Testing 3** | +| ----------- | ------------- | ------------- | +| Row 1 | Row 1.2 | Row 1.3 | +| Row 2.1 | Row 2.2 | Row 2.3 | +Testing [*TESTING*](https://www.google.com/webmasters/tools/siteoverview) +Testing **New** Line + +sdfsdf + + + +***TESTING*** + + +--- + +TESTING BELOW + + + +# Testing Header 1 +## Header 2 +### Heading 3 + + +#### Heading 4 + + +##### Heading 5 + + +###### **Heading 6** + + +\`\`\`ada +TESTING CODE +\`\`\` + + +- Testing Bullets +- Testing Bullets 2 + + + +1. Testing Numbered List +2. Testing Numbered List 2 + + + +> Testing Block Quote +> Multiple Lines + + + +> [!info] +> Testing Info Panel + +> [!note] +> Testing Note Panel + + + +> [!expand]+ Expand Title +> Testing expanded expand + + + +> [!expand]+ Closed Expand +> Closed Expand + + + +Testing ~Text~ Again Testing More + + + +[[mention:557058:aea5688c-52b9-4d15-aabe-96def1abc413|@Andrew McClenaghan]] + + + +\`testing\`code + +- [ ] Testing [[mention:557058:aea5688c-52b9-4d15-aabe-96def1abc413|@Andrew McClenaghan]] +- [x] Testing 2 + + + +> [!info] +> - Bullet List in Panel +> asdf +> 1. sdfsdf + + + +:1f92f|exploding_head: +" +`; diff --git a/packages/lib/src/index.ts b/packages/lib/src/index.ts index 9aaec747..95a6a0f8 100644 --- a/packages/lib/src/index.ts +++ b/packages/lib/src/index.ts @@ -5,3 +5,4 @@ export * as ConfluenceUploadSettings from "./Settings"; export * from "./mermaid_renderers"; export * as ConfluencePageConfig from "./ConniePageConfig"; export * from "./SettingsLoader"; +export * from "./ADFToMarkdown"; diff --git a/packages/obsidian/src/main.ts b/packages/obsidian/src/main.ts index c3a2d62d..aaa6d51a 100644 --- a/packages/obsidian/src/main.ts +++ b/packages/obsidian/src/main.ts @@ -4,6 +4,7 @@ import { Publisher, ConfluencePageConfig, StaticSettingsLoader, + renderADFDoc, } from "@markdown-confluence/lib"; import { ElectronMermaidRenderer } from "@markdown-confluence/mermaid-electron-renderer"; import { ConfluenceSettingTab } from "./ConfluenceSettingTab"; @@ -175,6 +176,38 @@ export default class ConfluencePlugin extends Plugin { } }); + this.addCommand({ + id: "adf-to-markdown", + name: "ADF To Markdown", + callback: async () => { + console.log("HMMMM"); + const json = JSON.parse( + '{"type":"doc","content":[{"type":"paragraph","content":[{"text":"Testing","type":"text"}]}],"version":1}' + ); + console.log({ json }); + + const confluenceClient = new ObsidianConfluenceClient({ + host: this.settings.confluenceBaseUrl, + authentication: { + basic: { + email: this.settings.atlassianUserName, + apiToken: this.settings.atlassianApiToken, + }, + }, + }); + const testingPage = + await confluenceClient.content.getContentById({ + id: "9732097", + expand: ["body.atlas_doc_format", "space"], + }); + const adf = JSON.parse( + testingPage.body?.atlas_doc_format?.value || + '{type: "doc", content:[]}' + ); + renderADFDoc(adf); + }, + }); + this.addCommand({ id: "publish-current", name: "Publish Current File to Confluence",