From e66553705fa938beea5900bfc0ce1b12dabb07e5 Mon Sep 17 00:00:00 2001 From: Konstantin Schaper Date: Thu, 11 Mar 2021 10:28:18 +0100 Subject: [PATCH] Create api for markdown ast plugins (#1578) Currently we only have the option to adjust rendering of the markdown output, but no option to change the generated ast tree before rendering takes place (i.e. to adjust the structure like when replacing text with links). This PR adds a new api to create ast plugins that can be integrated with the markdown view component. This is intended to be backwards-compatible and work independently from the underlying implementation. Co-authored-by: Sebastian Sdorra --- gradle/changelog/markdown-ast-plugins.yaml | 2 + scm-ui/ui-api/src/ApiProvider.tsx | 5 +- .../.storybook/withApiProvider.js | 1 + scm-ui/ui-components/package.json | 1 + .../src/__snapshots__/storyshots.test.ts.snap | 216716 +++++---------- scm-ui/ui-components/src/index.ts | 5 +- .../{ => markdown}/MarkdownCodeRenderer.tsx | 2 +- .../MarkdownHeadingRenderer.test.ts | 0 .../MarkdownHeadingRenderer.tsx | 6 +- .../MarkdownLinkRenderer.test.tsx | 0 .../{ => markdown}/MarkdownLinkRenderer.tsx | 2 +- .../{ => markdown}/MarkdownView.stories.tsx | 20 +- .../src/{ => markdown}/MarkdownView.tsx | 23 +- .../ui-components/src/markdown/PluginApi.ts | 9 + .../src/markdown/createMdastPlugin.ts | 12 + .../remarkChangesetShortLinkParser.test.ts | 0 .../remarkChangesetShortLinkParser.ts | 19 +- 17 files changed, 69503 insertions(+), 147320 deletions(-) create mode 100644 gradle/changelog/markdown-ast-plugins.yaml rename scm-ui/ui-components/src/{ => markdown}/MarkdownCodeRenderer.tsx (97%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownHeadingRenderer.test.ts (100%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownHeadingRenderer.tsx (97%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownLinkRenderer.test.tsx (100%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownLinkRenderer.tsx (98%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownView.stories.tsx (83%) rename scm-ui/ui-components/src/{ => markdown}/MarkdownView.tsx (91%) create mode 100644 scm-ui/ui-components/src/markdown/PluginApi.ts create mode 100644 scm-ui/ui-components/src/markdown/createMdastPlugin.ts rename scm-ui/ui-components/src/{ => markdown}/remarkChangesetShortLinkParser.test.ts (100%) rename scm-ui/ui-components/src/{ => markdown}/remarkChangesetShortLinkParser.ts (86%) diff --git a/gradle/changelog/markdown-ast-plugins.yaml b/gradle/changelog/markdown-ast-plugins.yaml new file mode 100644 index 0000000000..27af250572 --- /dev/null +++ b/gradle/changelog/markdown-ast-plugins.yaml @@ -0,0 +1,2 @@ +- type: added + description: create api for markdown ast plugins ([#1578](https://github.com/scm-manager/scm-manager/pull/1578)) diff --git a/scm-ui/ui-api/src/ApiProvider.tsx b/scm-ui/ui-api/src/ApiProvider.tsx index 50e9a05cb2..8319f55cb5 100644 --- a/scm-ui/ui-api/src/ApiProvider.tsx +++ b/scm-ui/ui-api/src/ApiProvider.tsx @@ -39,9 +39,10 @@ const queryClient = new QueryClient({ type Props = LegacyContext & { index?: IndexResources; me?: Me; + devtools?: boolean; }; -const ApiProvider: FC = ({ children, index, me, onMeFetched, onIndexFetched }) => { +const ApiProvider: FC = ({ children, index, me, onMeFetched, onIndexFetched, devtools = true }) => { useEffect(() => { if (index) { queryClient.setQueryData("index", index); @@ -63,7 +64,7 @@ const ApiProvider: FC = ({ children, index, me, onMeFetched, onIndexFetch {children} - + {devtools ? : null} ); }; diff --git a/scm-ui/ui-components/.storybook/withApiProvider.js b/scm-ui/ui-components/.storybook/withApiProvider.js index 1ae78e15f9..8fc42f2f01 100644 --- a/scm-ui/ui-components/.storybook/withApiProvider.js +++ b/scm-ui/ui-components/.storybook/withApiProvider.js @@ -39,6 +39,7 @@ const withApiProvider = (storyFn) => { groups: [], _links: {} }, + devtools: false, children: storyFn() }); } diff --git a/scm-ui/ui-components/package.json b/scm-ui/ui-components/package.json index e6fb4a2832..f8fbf5f485 100644 --- a/scm-ui/ui-components/package.json +++ b/scm-ui/ui-components/package.json @@ -42,6 +42,7 @@ "@types/refractor": "^3.0.0", "@types/styled-components": "^5.1.0", "@types/to-camel-case": "^1.0.0", + "@types/unist": "^2.0.3", "css": "^3.0.0", "enzyme-context": "^1.1.2", "enzyme-context-react-router-4": "^2.0.0", diff --git a/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap b/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap index b89821b4cd..6edfcb550a 100644 --- a/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap +++ b/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap @@ -1,35 +1,58 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Storyshots Annotate Default 1`] = ` -Array [ +
-
-
-        
-          
-
-
- Arthur Dent -
- -
- -
- -
-
- 1 -
- -
- - - package - - - main - - - -
-
-
-
-
- 2 -
- -
- - - - -
-
-
-
-
- Tricia Marie McMillan -
- -
- -
- -
-
- 3 -
- -
- - - - - - import - - - - - - "fmt" - - - - - - -
-
-
-
-
- 4 -
- -
- - - - -
-
-
-
-
- Arthur Dent -
- -
- -
- -
-
- 5 -
- -
- - - - - - func - - - - - - main - - - ( - - - ) - - - - - - { - - - - - - -
-
-
-
-
- Ford Prefect -
- -
- -
- -
-
- 6 -
- -
- - - fmt - - - . - - - Println - - - ( - - - "Hello World" - - - ) - - - - - - -
-
-
-
-
- Arthur Dent -
- -
- -
- -
-
- 7 -
- -
- - - - - - } - - - - - - -
-
-
-
-
- 8 -
- -
- - - - -
-
- - - - -
-
-
-
, -