@chobantonov/jsonforms-react-extended-renderers 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +17 -2
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js +2 -0
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js.map +1 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs +429 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs.map +1 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js +2 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js.map +1 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs +109 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs +12 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js +2 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js +3 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs +186 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs.map +1 -0
- package/lib/RactiveTemplate-7L44A38e.js +2 -0
- package/lib/RactiveTemplate-7L44A38e.js.map +1 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs +74 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs.map +1 -0
- package/lib/ajv-localizers.cjs.js +2 -0
- package/lib/ajv-localizers.cjs.js.map +1 -0
- package/lib/ajv-localizers.esm.js +7 -0
- package/lib/ajv-localizers.esm.js.map +1 -0
- package/lib/ajvI18n-2UTyJv5g.js +2 -0
- package/lib/ajvI18n-2UTyJv5g.js.map +1 -0
- package/lib/ajvI18n-DO8FFvx1.mjs +227 -0
- package/lib/ajvI18n-DO8FFvx1.mjs.map +1 -0
- package/lib/celTemplate-Cl0alaCz.js +3 -0
- package/lib/celTemplate-Cl0alaCz.js.map +1 -0
- package/lib/celTemplate-D_AGhaIL.mjs +137 -0
- package/lib/celTemplate-D_AGhaIL.mjs.map +1 -0
- package/lib/components/DynamicJSXRenderer.d.ts +7 -0
- package/lib/components/ExtendedJsonForms.d.ts +80 -0
- package/lib/components/GenericAdditionalProperties.d.ts +4 -0
- package/lib/components/InterpolatedText.d.ts +34 -0
- package/lib/components/RactiveTemplate.d.ts +37 -0
- package/lib/components/editorRendererTypes.d.ts +139 -0
- package/lib/components/elementRender.d.ts +15 -0
- package/lib/configNamespaces-BFclHtB8.mjs +1 -0
- package/lib/configNamespaces-sboXDudL.js +1 -0
- package/lib/core/ajv.d.ts +82 -0
- package/lib/core/ajvI18n/bg.d.ts +2 -0
- package/lib/core/ajvI18n/index.d.ts +118 -0
- package/lib/core/ajvI18n/localizers.d.ts +20 -0
- package/lib/core/dynamicDefaults.d.ts +34 -0
- package/lib/core/keywords.d.ts +24 -0
- package/lib/core/transform.d.ts +29 -0
- package/lib/core/uiSchema.d.ts +48 -0
- package/lib/elementRender-Dbp2e-Cc.js +2 -0
- package/lib/elementRender-Dbp2e-Cc.js.map +1 -0
- package/lib/elementRender-y8MK9CCE.mjs +5 -0
- package/lib/elementRender-y8MK9CCE.mjs.map +1 -0
- package/lib/index.d.ts +41 -0
- package/lib/jsonforms-react-extended.cjs.js +16 -0
- package/lib/jsonforms-react-extended.cjs.js.map +1 -0
- package/lib/jsonforms-react-extended.esm.js +2335 -0
- package/lib/jsonforms-react-extended.esm.js.map +1 -0
- package/lib/locale/bg.d.ts +14 -0
- package/lib/markdown-C_y73Q0w.mjs +123 -0
- package/lib/markdown-C_y73Q0w.mjs.map +1 -0
- package/lib/markdown-CuqtpGVd.js +2 -0
- package/lib/markdown-CuqtpGVd.js.map +1 -0
- package/lib/objectWithoutProperties-6zedhz76.js +2 -0
- package/lib/objectWithoutProperties-6zedhz76.js.map +1 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs +58 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs.map +1 -0
- package/lib/renderers/AgGridControlRenderer.d.ts +10 -0
- package/lib/renderers/AgGridControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/ButtonRenderer.d.ts +83 -0
- package/lib/renderers/GenericMixedRenderer.d.ts +9 -0
- package/lib/renderers/HorizontalLayoutRenderer.d.ts +5 -0
- package/lib/renderers/ImageViewRenderer.d.ts +48 -0
- package/lib/renderers/LinkRenderer.d.ts +23 -0
- package/lib/renderers/MarkupLabelRenderer.d.ts +35 -0
- package/lib/renderers/MarkupLabelRenderer.impl.d.ts +17 -0
- package/lib/renderers/MonacoControlRenderer.d.ts +8 -0
- package/lib/renderers/MonacoControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/RactiveTemplateLayoutRenderer.d.ts +6 -0
- package/lib/renderers/SeparatorRenderer.d.ts +17 -0
- package/lib/renderers/SlotRenderer.d.ts +5 -0
- package/lib/renderers/SpacerRenderer.d.ts +5 -0
- package/lib/renderers/SplitLayoutRenderer.d.ts +18 -0
- package/lib/renderers/TemplateLayoutRenderer.d.ts +13 -0
- package/lib/renderers/TemplateRenderer.d.ts +17 -0
- package/lib/renderers/TsxTemplateLayoutRenderer.d.ts +111 -0
- package/lib/renderers/index.d.ts +13 -0
- package/lib/renderers/templateEngines.d.ts +43 -0
- package/lib/util/actionContext.d.ts +23 -0
- package/lib/util/additionalErrors.d.ts +77 -0
- package/lib/util/celEnvironment.d.ts +23 -0
- package/lib/util/celTemplate.d.ts +47 -0
- package/lib/util/childNames.d.ts +46 -0
- package/lib/util/configNamespaces.d.ts +1 -0
- package/lib/util/cron.d.ts +122 -0
- package/lib/util/editorControls.d.ts +8 -0
- package/lib/util/editorDiagnostics.d.ts +56 -0
- package/lib/util/extendedControls.d.ts +4 -0
- package/lib/util/extendedLocale.d.ts +32 -0
- package/lib/util/fileFilterValue.d.ts +3 -0
- package/lib/util/formContext.d.ts +54 -0
- package/lib/util/gridColumns.d.ts +6 -0
- package/lib/util/horizontalLayout.d.ts +10 -0
- package/lib/util/i18nDefaults.d.ts +45 -0
- package/lib/util/interpolate.d.ts +105 -0
- package/lib/util/labelFilterValue.d.ts +2 -0
- package/lib/util/lazyControl.d.ts +36 -0
- package/lib/util/lazyTemplate.d.ts +18 -0
- package/lib/util/markdown.d.ts +55 -0
- package/lib/util/markdownEscape.d.ts +27 -0
- package/lib/util/markup.d.ts +74 -0
- package/lib/util/maskControls.d.ts +2 -0
- package/lib/util/templateLang.d.ts +87 -0
- package/lib/util/templateUrls.d.ts +40 -0
- package/lib/util/urlPolicy.d.ts +42 -0
- package/lib/util/useCronPicker.d.ts +24 -0
- package/lib/util/useDurationControl.d.ts +99 -0
- package/lib/util/useEditorAppearance.d.ts +21 -0
- package/lib/util/useExtendedTranslator.d.ts +10 -0
- package/lib/util/useMonacoShadowStyles.d.ts +2 -0
- package/package.json +110 -3
- package/src/components/DynamicJSXRenderer.tsx +274 -0
- package/src/components/ExtendedJsonForms.tsx +329 -0
- package/src/components/GenericAdditionalProperties.tsx +362 -0
- package/src/components/InterpolatedText.tsx +114 -0
- package/src/components/RactiveTemplate.tsx +161 -0
- package/src/components/editorRendererTypes.ts +135 -0
- package/src/components/elementRender.ts +15 -0
- package/src/core/ajv.ts +177 -0
- package/src/core/ajvI18n/bg.ts +203 -0
- package/src/core/ajvI18n/index.ts +267 -0
- package/src/core/ajvI18n/localizers.ts +25 -0
- package/src/core/dynamicDefaults.ts +170 -0
- package/src/core/keywords.ts +71 -0
- package/src/core/transform.ts +137 -0
- package/src/core/uiSchema.ts +53 -0
- package/src/index.tsx +196 -0
- package/src/locale/bg.ts +51 -0
- package/src/renderers/AgGridControlRenderer.impl.tsx +788 -0
- package/src/renderers/AgGridControlRenderer.tsx +24 -0
- package/src/renderers/ButtonRenderer.tsx +328 -0
- package/src/renderers/GenericMixedRenderer.tsx +224 -0
- package/src/renderers/HorizontalLayoutRenderer.tsx +93 -0
- package/src/renderers/ImageViewRenderer.tsx +328 -0
- package/src/renderers/LinkRenderer.tsx +92 -0
- package/src/renderers/MarkupLabelRenderer.impl.tsx +26 -0
- package/src/renderers/MarkupLabelRenderer.tsx +327 -0
- package/src/renderers/MonacoControlRenderer.impl.tsx +351 -0
- package/src/renderers/MonacoControlRenderer.tsx +24 -0
- package/src/renderers/RactiveTemplateLayoutRenderer.tsx +172 -0
- package/src/renderers/SeparatorRenderer.tsx +55 -0
- package/src/renderers/SlotRenderer.tsx +34 -0
- package/src/renderers/SpacerRenderer.tsx +69 -0
- package/src/renderers/SplitLayoutRenderer.tsx +220 -0
- package/src/renderers/TemplateLayoutRenderer.tsx +394 -0
- package/src/renderers/TemplateRenderer.tsx +71 -0
- package/src/renderers/TsxTemplateLayoutRenderer.tsx +258 -0
- package/src/renderers/index.ts +14 -0
- package/src/renderers/templateEngines.tsx +100 -0
- package/src/util/actionContext.ts +29 -0
- package/src/util/additionalErrors.tsx +391 -0
- package/src/util/celEnvironment.ts +300 -0
- package/src/util/celTemplate.ts +267 -0
- package/src/util/childNames.ts +135 -0
- package/src/util/configNamespaces.ts +6 -0
- package/src/util/cron.ts +433 -0
- package/src/util/editorControls.ts +58 -0
- package/src/util/editorDiagnostics.ts +103 -0
- package/src/util/extendedControls.ts +34 -0
- package/src/util/extendedLocale.ts +67 -0
- package/src/util/fileFilterValue.ts +28 -0
- package/src/util/formContext.ts +74 -0
- package/src/util/gridColumns.ts +80 -0
- package/src/util/horizontalLayout.ts +59 -0
- package/src/util/i18nDefaults.ts +80 -0
- package/src/util/interpolate.ts +218 -0
- package/src/util/labelFilterValue.ts +49 -0
- package/src/util/lazyControl.tsx +66 -0
- package/src/util/lazyTemplate.tsx +110 -0
- package/src/util/markdown.tsx +345 -0
- package/src/util/markdownEscape.ts +29 -0
- package/src/util/markup.ts +144 -0
- package/src/util/maskControls.ts +6 -0
- package/src/util/templateLang.ts +145 -0
- package/src/util/templateUrls.ts +274 -0
- package/src/util/urlPolicy.ts +115 -0
- package/src/util/useCronPicker.ts +102 -0
- package/src/util/useDurationControl.ts +272 -0
- package/src/util/useEditorAppearance.ts +86 -0
- package/src/util/useExtendedTranslator.ts +25 -0
- package/src/util/useMonacoShadowStyles.ts +47 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React, { Suspense, lazy } from 'react';
|
|
2
|
+
import { ExtendedI18nKey } from './i18nDefaults';
|
|
3
|
+
import { useExtendedTranslator } from './useExtendedTranslator';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Defers a template engine's compiler until a template is actually rendered.
|
|
7
|
+
*
|
|
8
|
+
* Both engines carry a heavyweight dependency at module scope - Sucrase for
|
|
9
|
+
* the JSX profile, Ractive for the Ractive one - and a form with no
|
|
10
|
+
* `TemplateLayout` in it should download neither. `React.lazy` puts each
|
|
11
|
+
* behind its own chunk, the same arrangement the AG Grid and Monaco controls
|
|
12
|
+
* already use.
|
|
13
|
+
*
|
|
14
|
+
* Selection happens in the **tester**, so only the engine an element actually
|
|
15
|
+
* asks for is ever fetched: a form of `lang: "ractive"` templates never loads
|
|
16
|
+
* Sucrase, and vice versa.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Suspense has no error path of its own, so a boundary catches import failures.
|
|
21
|
+
*
|
|
22
|
+
* It catches **render** failures too, which is not the same thing and must not
|
|
23
|
+
* be reported as if it were: a template that throws while React reconciles its
|
|
24
|
+
* output used to surface as "The template engine could not be loaded", which
|
|
25
|
+
* sends the reader to look at chunks and network tabs instead of at their
|
|
26
|
+
* template. `loaded` says which of the two happened.
|
|
27
|
+
*/
|
|
28
|
+
class LoadBoundary extends React.Component<
|
|
29
|
+
React.PropsWithChildren<{ describe: () => React.ReactNode }>,
|
|
30
|
+
{ failed: boolean }
|
|
31
|
+
> {
|
|
32
|
+
state = { failed: false };
|
|
33
|
+
static getDerivedStateFromError() {
|
|
34
|
+
return { failed: true };
|
|
35
|
+
}
|
|
36
|
+
componentDidCatch(error: Error) {
|
|
37
|
+
// oxlint-disable-next-line no-console
|
|
38
|
+
console.error('template.failed:', error);
|
|
39
|
+
}
|
|
40
|
+
render() {
|
|
41
|
+
return this.state.failed ? this.props.describe() : this.props.children;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Translation **keys**, not text.
|
|
47
|
+
*
|
|
48
|
+
* The labels are chosen where `createLazyTemplate` is called, which is module
|
|
49
|
+
* scope - no hook can run there, and text fixed at module scope is text that
|
|
50
|
+
* cannot follow the locale. Naming the keys here and translating them inside
|
|
51
|
+
* the component is what lets these three strings be translated at all.
|
|
52
|
+
*/
|
|
53
|
+
export interface LazyTemplateLabels {
|
|
54
|
+
loading: ExtendedI18nKey;
|
|
55
|
+
/** Shown when the engine's chunk itself could not be fetched. */
|
|
56
|
+
error: ExtendedI18nKey;
|
|
57
|
+
/** Shown when the engine loaded and the template then threw. */
|
|
58
|
+
renderError?: ExtendedI18nKey;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const createLazyTemplate = <P extends object>(
|
|
62
|
+
load: () => Promise<React.ComponentType<P>>,
|
|
63
|
+
labels: LazyTemplateLabels,
|
|
64
|
+
): React.ComponentType<P> => {
|
|
65
|
+
/*
|
|
66
|
+
Module scope, so it survives a remount: once the chunk is in, a later
|
|
67
|
+
failure can only be the template's own.
|
|
68
|
+
*/
|
|
69
|
+
let loaded = false;
|
|
70
|
+
// Created once per module, not per render, so the chunk is fetched once.
|
|
71
|
+
const Lazy = lazy(async () => {
|
|
72
|
+
const component = await load();
|
|
73
|
+
loaded = true;
|
|
74
|
+
return { default: component };
|
|
75
|
+
});
|
|
76
|
+
const LazyTemplate = (props: P) => {
|
|
77
|
+
const t = useExtendedTranslator();
|
|
78
|
+
return (
|
|
79
|
+
<LoadBoundary
|
|
80
|
+
describe={() => (
|
|
81
|
+
<div
|
|
82
|
+
role='alert'
|
|
83
|
+
data-template-load-error={loaded ? undefined : true}
|
|
84
|
+
data-template-render-error={loaded ? true : undefined}
|
|
85
|
+
>
|
|
86
|
+
{t(
|
|
87
|
+
loaded
|
|
88
|
+
? (labels.renderError ?? 'template.renderError')
|
|
89
|
+
: labels.error,
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
)}
|
|
93
|
+
>
|
|
94
|
+
<Suspense
|
|
95
|
+
fallback={
|
|
96
|
+
<div aria-busy='true' aria-live='polite' data-template-loading>
|
|
97
|
+
{t(labels.loading)}
|
|
98
|
+
</div>
|
|
99
|
+
}
|
|
100
|
+
>
|
|
101
|
+
{/* The generic defeats React.lazy's prop inference; the call sites are typed. */}
|
|
102
|
+
<Lazy {...(props as any)} />
|
|
103
|
+
</Suspense>
|
|
104
|
+
</LoadBoundary>
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
// Named so React DevTools and the lint rule both have something to show.
|
|
108
|
+
LazyTemplate.displayName = 'LazyTemplate';
|
|
109
|
+
return LazyTemplate;
|
|
110
|
+
};
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import MarkdownIt, { type MarkdownIt as MarkdownItInstance } from 'markdown-it';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { isAllowedUrl, UrlPolicy } from './urlPolicy';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
Re-exported so this module stays the one place a caller looks for Markdown
|
|
7
|
+
concerns, while the function itself carries no dependency - see
|
|
8
|
+
`markdownEscape.ts` for why that separation matters to the bundle.
|
|
9
|
+
*/
|
|
10
|
+
export { escapeMarkdown } from './markdownEscape';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The Markdown profiles of specification section 10, rendered to React.
|
|
14
|
+
*
|
|
15
|
+
* ## Why a token stream rather than HTML
|
|
16
|
+
*
|
|
17
|
+
* Section 10 requires that Markdown output be sanitized **after** parsing.
|
|
18
|
+
* `md.render()` returns an HTML string, which would have to be injected with
|
|
19
|
+
* `dangerouslySetInnerHTML` and therefore sanitized first by some other
|
|
20
|
+
* library. `md.parse()` returns a token stream instead, and walking that into
|
|
21
|
+
* React elements means **no HTML string is ever produced**: there is nothing
|
|
22
|
+
* unsanitized to sanitize, no sanitizer in the dependency chain to be
|
|
23
|
+
* misconfigured, and the set of elements that can reach the page is the
|
|
24
|
+
* allowlist below rather than whatever the sanitizer was talked into.
|
|
25
|
+
*
|
|
26
|
+
* ## Why rules are disabled rather than output filtered
|
|
27
|
+
*
|
|
28
|
+
* Basic excludes headings, images, tables, blockquotes and fenced code.
|
|
29
|
+
* Disabling the **rule** means `# Title` renders as the literal text
|
|
30
|
+
* `# Title`, which is what a profile that excludes headings should do.
|
|
31
|
+
* Filtering the output instead would consume the `#` and silently promote the
|
|
32
|
+
* text to a paragraph, leaving an author to wonder where their heading went.
|
|
33
|
+
*
|
|
34
|
+
* Disabling a rule is not the same as escaping the text, and one case shows
|
|
35
|
+
* it: with `fence` off, a ``` block is claimed by the inline backtick rule
|
|
36
|
+
* and comes out as inline code. Nothing is lost - the code is still the
|
|
37
|
+
* content - but the construct degrades to its nearest supported neighbour
|
|
38
|
+
* rather than to literal backticks.
|
|
39
|
+
*
|
|
40
|
+
* `html: false` means the parser never emits a raw-HTML token at all, so the
|
|
41
|
+
* excluded raw HTML is excluded by construction rather than by a later pass.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
export type MarkdownProfile = 'basic' | 'extended';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Rules off in **both** profiles.
|
|
48
|
+
*
|
|
49
|
+
* `image` and `html_inline` are section 10's independently-gated constructs,
|
|
50
|
+
* and neither gate is open: images are not implemented here, and raw HTML has
|
|
51
|
+
* no rule to disable beyond `html: false` - these two names are belt and
|
|
52
|
+
* braces should a future option turn `html` on for some other reason.
|
|
53
|
+
*
|
|
54
|
+
* `hr` is in neither profile's list. Basic enumerates what it supports and a
|
|
55
|
+
* thematic break is not in it; extended is defined as basic *plus headings,
|
|
56
|
+
* blockquotes, tables and fenced code*, which does not add one either.
|
|
57
|
+
*/
|
|
58
|
+
const ALWAYS_OFF = ['image', 'html_inline', 'html_block', 'hr'];
|
|
59
|
+
|
|
60
|
+
/** What extended adds back, and therefore exactly what basic withholds. */
|
|
61
|
+
const EXTENDED_ONLY = [
|
|
62
|
+
'heading',
|
|
63
|
+
'lheading',
|
|
64
|
+
'blockquote',
|
|
65
|
+
'table',
|
|
66
|
+
'fence',
|
|
67
|
+
'code',
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The elements that may reach the page.
|
|
72
|
+
*
|
|
73
|
+
* Every tag markdown-it produces for the rules left enabled, and nothing
|
|
74
|
+
* else. A token carrying any other tag is dropped rather than rendered, so
|
|
75
|
+
* this list - not the rule configuration - is the last word on what can be
|
|
76
|
+
* drawn. `reference` and `autolink` stay enabled because both produce
|
|
77
|
+
* ordinary links, which the profile supports and which the URL policy
|
|
78
|
+
* governs like any other.
|
|
79
|
+
*/
|
|
80
|
+
const ALLOWED_TAGS = new Set([
|
|
81
|
+
'p',
|
|
82
|
+
'br',
|
|
83
|
+
'strong',
|
|
84
|
+
'em',
|
|
85
|
+
's',
|
|
86
|
+
'code',
|
|
87
|
+
'pre',
|
|
88
|
+
'a',
|
|
89
|
+
'ul',
|
|
90
|
+
'ol',
|
|
91
|
+
'li',
|
|
92
|
+
'blockquote',
|
|
93
|
+
'h1',
|
|
94
|
+
'h2',
|
|
95
|
+
'h3',
|
|
96
|
+
'h4',
|
|
97
|
+
'h5',
|
|
98
|
+
'h6',
|
|
99
|
+
'table',
|
|
100
|
+
'thead',
|
|
101
|
+
'tbody',
|
|
102
|
+
'tr',
|
|
103
|
+
'th',
|
|
104
|
+
'td',
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const parsers = new Map<MarkdownProfile, MarkdownItInstance>();
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* `linkify` is off: turning a bare URL into a link is in neither profile, and
|
|
111
|
+
* it would route text the author never marked up through the URL policy.
|
|
112
|
+
*
|
|
113
|
+
* `breaks` is off because a single newline is not a line break in Markdown;
|
|
114
|
+
* the profile's "line breaks" are the two-space and backslash forms, which
|
|
115
|
+
* `hardbreak` carries.
|
|
116
|
+
*/
|
|
117
|
+
export const markdownParser = (
|
|
118
|
+
profile: MarkdownProfile = 'basic',
|
|
119
|
+
): MarkdownItInstance => {
|
|
120
|
+
const existing = parsers.get(profile);
|
|
121
|
+
if (existing) {
|
|
122
|
+
return existing;
|
|
123
|
+
}
|
|
124
|
+
const md = new MarkdownIt({
|
|
125
|
+
html: false,
|
|
126
|
+
linkify: false,
|
|
127
|
+
breaks: false,
|
|
128
|
+
typographer: false,
|
|
129
|
+
});
|
|
130
|
+
md.disable(
|
|
131
|
+
profile === 'extended' ? ALWAYS_OFF : [...ALWAYS_OFF, ...EXTENDED_ONLY],
|
|
132
|
+
/* ignoreInvalid: a rule renamed upstream must not throw at render time. */
|
|
133
|
+
true,
|
|
134
|
+
);
|
|
135
|
+
/*
|
|
136
|
+
markdown-it carries its own scheme check, which refuses `javascript:`,
|
|
137
|
+
`vbscript:`, `file:` and `data:` by turning the link back into plain text
|
|
138
|
+
before a `link_open` token exists. Two reasons to take it over rather than
|
|
139
|
+
leave it in front of the URL policy:
|
|
140
|
+
|
|
141
|
+
- Section 10 makes the URL policy the authority on link targets. With two
|
|
142
|
+
checks in series the effective policy is their intersection, so a host
|
|
143
|
+
that widens `allowedSchemes` silently gets nothing for the schemes
|
|
144
|
+
markdown-it happens to dislike.
|
|
145
|
+
- A link refused before parsing raises no diagnostic. The author is told
|
|
146
|
+
nothing, and the text keeps the raw `[go](...)` brackets.
|
|
147
|
+
|
|
148
|
+
Handing every href to `isAllowedUrl` makes one check authoritative and
|
|
149
|
+
every refusal reportable. It is not a loosening: the default policy allows
|
|
150
|
+
https, http and mailto only, which is narrower than what markdown-it
|
|
151
|
+
blocks.
|
|
152
|
+
*/
|
|
153
|
+
md.validateLink = () => true;
|
|
154
|
+
parsers.set(profile, md);
|
|
155
|
+
return md;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export interface MarkdownContext {
|
|
159
|
+
profile?: MarkdownProfile;
|
|
160
|
+
urlPolicy?: UrlPolicy;
|
|
161
|
+
/** Called for a link the policy refuses, so the caller can diagnose it. */
|
|
162
|
+
onRefusedUrl?: (href: string) => void;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface Frame {
|
|
166
|
+
/**
|
|
167
|
+
* `null` for a **transparent** frame: one that holds its children's place
|
|
168
|
+
* in the walk but draws nothing of its own.
|
|
169
|
+
*
|
|
170
|
+
* markdown-it wraps every list item's content in a paragraph and sets
|
|
171
|
+
* `hidden` on the wrapper when the list is tight, which is how `- one` ends
|
|
172
|
+
* up as `<li>one</li>` rather than `<li><p>one</p></li>`. The flag has to be
|
|
173
|
+
* honoured: rendering a hidden token puts a block element inside every list
|
|
174
|
+
* item and spaces the list out. It is also why a frame is pushed at all for
|
|
175
|
+
* such a token - the matching close must still find something to pop.
|
|
176
|
+
*/
|
|
177
|
+
tag: string | null;
|
|
178
|
+
props: Record<string, unknown>;
|
|
179
|
+
children: React.ReactNode[];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Walks a token stream into React nodes.
|
|
184
|
+
*
|
|
185
|
+
* markdown-it emits a flat list whose `nesting` is +1, 0 or -1, so the walk
|
|
186
|
+
* keeps an explicit stack rather than recursing: an opening token pushes a
|
|
187
|
+
* frame, a closing token pops it and appends the finished element to its
|
|
188
|
+
* parent. Keying off `nesting` and `tag` rather than off each token `type`
|
|
189
|
+
* is what lets the two profiles share one walk - an `h2` needs no case of
|
|
190
|
+
* its own, it needs only to be in {@link ALLOWED_TAGS}.
|
|
191
|
+
*/
|
|
192
|
+
const walk = (
|
|
193
|
+
tokens: ReturnType<MarkdownItInstance['parse']>,
|
|
194
|
+
context: MarkdownContext,
|
|
195
|
+
counter: { next: number },
|
|
196
|
+
): React.ReactNode[] => {
|
|
197
|
+
const root: React.ReactNode[] = [];
|
|
198
|
+
const stack: Frame[] = [];
|
|
199
|
+
const push = (node: React.ReactNode) =>
|
|
200
|
+
(stack.length ? stack[stack.length - 1].children : root).push(node);
|
|
201
|
+
const close = () => {
|
|
202
|
+
const frame = stack.pop();
|
|
203
|
+
if (!frame) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (frame.tag === null) {
|
|
207
|
+
for (const child of frame.children) {
|
|
208
|
+
push(child);
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
push(
|
|
213
|
+
React.createElement(
|
|
214
|
+
frame.tag,
|
|
215
|
+
{ key: `md-${counter.next++}`, ...frame.props },
|
|
216
|
+
frame.children.length ? frame.children : undefined,
|
|
217
|
+
),
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
for (const token of tokens) {
|
|
222
|
+
if (token.type === 'inline') {
|
|
223
|
+
for (const node of walk(token.children ?? [], context, counter)) {
|
|
224
|
+
push(node);
|
|
225
|
+
}
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (token.nesting === 1) {
|
|
229
|
+
if (token.type === 'link_open') {
|
|
230
|
+
const href = String(token.attrGet('href') ?? '');
|
|
231
|
+
if (isAllowedUrl(href, context.urlPolicy)) {
|
|
232
|
+
stack.push({
|
|
233
|
+
tag: 'a',
|
|
234
|
+
props: { href, target: '_blank', rel: 'noopener noreferrer' },
|
|
235
|
+
children: [],
|
|
236
|
+
});
|
|
237
|
+
} else {
|
|
238
|
+
context.onRefusedUrl?.(href);
|
|
239
|
+
/*
|
|
240
|
+
A refused destination keeps its text and loses its link, which is
|
|
241
|
+
what the Link renderer does under the same policy. A `span` leaves
|
|
242
|
+
the sentence intact without offering anywhere to click.
|
|
243
|
+
*/
|
|
244
|
+
stack.push({
|
|
245
|
+
tag: 'span',
|
|
246
|
+
props: { 'data-markdown-url-refused': true },
|
|
247
|
+
children: [],
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
/*
|
|
253
|
+
An unknown tag still pushes a frame, so its matching close pops this
|
|
254
|
+
one and the nesting stays balanced; `span` is the inert stand-in.
|
|
255
|
+
*/
|
|
256
|
+
stack.push({
|
|
257
|
+
tag: token.hidden
|
|
258
|
+
? null
|
|
259
|
+
: ALLOWED_TAGS.has(token.tag)
|
|
260
|
+
? token.tag
|
|
261
|
+
: 'span',
|
|
262
|
+
props: {},
|
|
263
|
+
children: [],
|
|
264
|
+
});
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (token.nesting === -1) {
|
|
268
|
+
close();
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
switch (token.type) {
|
|
272
|
+
case 'text':
|
|
273
|
+
if (token.content) {
|
|
274
|
+
push(token.content);
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
case 'softbreak':
|
|
278
|
+
push(' ');
|
|
279
|
+
break;
|
|
280
|
+
case 'hardbreak':
|
|
281
|
+
push(<br key={`md-${counter.next++}`} />);
|
|
282
|
+
break;
|
|
283
|
+
case 'code_inline':
|
|
284
|
+
push(<code key={`md-${counter.next++}`}>{token.content}</code>);
|
|
285
|
+
break;
|
|
286
|
+
case 'fence':
|
|
287
|
+
case 'code_block':
|
|
288
|
+
/*
|
|
289
|
+
No language class: the info string is author-supplied text, and a
|
|
290
|
+
highlighter is not in the profile. The code is content either way.
|
|
291
|
+
*/
|
|
292
|
+
push(
|
|
293
|
+
<pre key={`md-${counter.next++}`}>
|
|
294
|
+
<code>{token.content}</code>
|
|
295
|
+
</pre>,
|
|
296
|
+
);
|
|
297
|
+
break;
|
|
298
|
+
default:
|
|
299
|
+
/*
|
|
300
|
+
A disabled rule emits no tokens, so anything arriving here is a
|
|
301
|
+
construct this profile does not draw. Dropping it is right: the text
|
|
302
|
+
it carried is already in the stream as its own token.
|
|
303
|
+
*/
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/* An unbalanced stream (never seen from markdown-it) still yields its content. */
|
|
308
|
+
while (stack.length) {
|
|
309
|
+
close();
|
|
310
|
+
}
|
|
311
|
+
return root;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/** Parses and renders, in the requested profile. */
|
|
315
|
+
export const renderMarkdown = (
|
|
316
|
+
markdown: string,
|
|
317
|
+
context: MarkdownContext = {},
|
|
318
|
+
): React.ReactNode[] =>
|
|
319
|
+
walk(markdownParser(context.profile).parse(markdown, {}), context, {
|
|
320
|
+
next: 0,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
/** Visible text using the same Markdown profile as the renderer. */
|
|
324
|
+
export const markdownPlainText = (
|
|
325
|
+
source: string,
|
|
326
|
+
profile: MarkdownProfile,
|
|
327
|
+
): string => {
|
|
328
|
+
const collect = (tokens: any[]): string =>
|
|
329
|
+
tokens
|
|
330
|
+
.map((token) => {
|
|
331
|
+
if (token.children) return collect(token.children);
|
|
332
|
+
if (['text', 'code_inline', 'code_block', 'fence'].includes(token.type))
|
|
333
|
+
return token.content;
|
|
334
|
+
if (
|
|
335
|
+
['softbreak', 'hardbreak'].includes(token.type) ||
|
|
336
|
+
(token.block && token.nesting === -1)
|
|
337
|
+
)
|
|
338
|
+
return ' ';
|
|
339
|
+
return '';
|
|
340
|
+
})
|
|
341
|
+
.join('');
|
|
342
|
+
return collect(markdownParser(profile).parse(source, {}))
|
|
343
|
+
.replace(/\s+/g, ' ')
|
|
344
|
+
.trim();
|
|
345
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escaping a substituted value so it cannot introduce Markdown.
|
|
3
|
+
*
|
|
4
|
+
* A module of its own, with no dependencies, because of **where** it is
|
|
5
|
+
* needed. The interpolation path has to escape values before handing them to
|
|
6
|
+
* the parser, but a label that interpolates without asking for Markdown must
|
|
7
|
+
* not download a parser. Leaving this in `markdown.tsx` - which imports
|
|
8
|
+
* markdown-it - would have put the parser in the interpolation chunk, which
|
|
9
|
+
* is exactly the coupling the lazy split exists to avoid.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Escapes every character that could start a Markdown construct.
|
|
14
|
+
*
|
|
15
|
+
* Section 9: "Markdown substitutions are escaped before parsing." The order
|
|
16
|
+
* is interpolate -> escape -> parse, and swapping the last two makes the
|
|
17
|
+
* escaping pointless - by then the link already exists.
|
|
18
|
+
*
|
|
19
|
+
* Applied to **substituted values only**, never to the whole template: the
|
|
20
|
+
* author's own `**bold**` in the surrounding text has to keep working, and
|
|
21
|
+
* escaping the finished string would kill it.
|
|
22
|
+
*
|
|
23
|
+
* `<` and `>` are deliberately absent. Raw HTML is not a Markdown construct
|
|
24
|
+
* here - the parser runs with `html: false`, and the result is rendered as
|
|
25
|
+
* React text nodes, which React escapes. Adding them would put visible
|
|
26
|
+
* backslashes in front of ordinary punctuation for no gain.
|
|
27
|
+
*/
|
|
28
|
+
export const escapeMarkdown = (text: string): string =>
|
|
29
|
+
text.replace(/[\\`*_{}[\]()#+\-.!>|~]/g, (match) => `\\${match}`);
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { JSONFORMS_EXTENDED_CONFIG_KEY } from './configNamespaces';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolving what a text element should do with its text.
|
|
5
|
+
*
|
|
6
|
+
* Two independent requests, and either one is enough to take the element away
|
|
7
|
+
* from the plain renderer:
|
|
8
|
+
*
|
|
9
|
+
* - `options.markup: "markdown"` asks for the restricted Markdown profile.
|
|
10
|
+
* - `options.interpolate: true` asks for message formatting.
|
|
11
|
+
*
|
|
12
|
+
* With neither, the ordinary label renderer keeps the element - there is
|
|
13
|
+
* nothing here it would do differently, and replacing it would change how
|
|
14
|
+
* every existing form draws its labels.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The `markup` values the model defines. */
|
|
18
|
+
export type Markup = 'plain' | 'markdown';
|
|
19
|
+
|
|
20
|
+
export type MarkupDiagnostic =
|
|
21
|
+
/** `markup: "markdown"` while the host has the profile switched off. */
|
|
22
|
+
| 'markdownDisabled'
|
|
23
|
+
/** A `markup` value this implementation does not know. */
|
|
24
|
+
| 'markupUnsupported';
|
|
25
|
+
|
|
26
|
+
export interface MarkupRequest {
|
|
27
|
+
/** Whether Markdown should actually be parsed. */
|
|
28
|
+
markdown: boolean;
|
|
29
|
+
/** Whether the text should be run through message formatting. */
|
|
30
|
+
interpolate: boolean;
|
|
31
|
+
/** Wrap the result in the UI library's own typography components. */
|
|
32
|
+
typography: boolean;
|
|
33
|
+
/** Set when the request cannot be honoured; see `MarkupDiagnostic`. */
|
|
34
|
+
diagnostic?: MarkupDiagnostic;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const namespaced = (config: unknown): Record<string, unknown> | undefined =>
|
|
38
|
+
(config as Record<string, unknown> | undefined)?.[
|
|
39
|
+
JSONFORMS_EXTENDED_CONFIG_KEY
|
|
40
|
+
] as Record<string, unknown> | undefined;
|
|
41
|
+
|
|
42
|
+
const markupConfig = (config: unknown): Record<string, unknown> | undefined =>
|
|
43
|
+
namespaced(config)?.['markup'] as Record<string, unknown> | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether the host permits Markdown at all.
|
|
47
|
+
*
|
|
48
|
+
* Defaults to **true**, which the recommended configuration states and which
|
|
49
|
+
* differs from the other extension gates. Those gate something that executes;
|
|
50
|
+
* this gates a parser that cannot execute anything, and the profile already
|
|
51
|
+
* excludes raw HTML, images and embedded content.
|
|
52
|
+
*/
|
|
53
|
+
export const markdownEnabled = (config: unknown): boolean => {
|
|
54
|
+
const markdown = markupConfig(config)?.['markdown'] as
|
|
55
|
+
| Record<string, unknown>
|
|
56
|
+
| undefined;
|
|
57
|
+
return markdown?.['enabled'] !== false;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Which Markdown profile the host has selected.
|
|
62
|
+
*
|
|
63
|
+
* `basic` unless the host says otherwise, and an unrecognized value falls back
|
|
64
|
+
* to `basic` rather than being reported: a profile name is a host setting, not
|
|
65
|
+
* an authored one, so there is no author on the page to tell about it, and the
|
|
66
|
+
* safe direction for an unknown one is the smaller grammar.
|
|
67
|
+
*/
|
|
68
|
+
export const markdownProfile = (config: unknown): 'basic' | 'extended' =>
|
|
69
|
+
(markupConfig(config)?.['markdown'] as Record<string, unknown> | undefined)?.[
|
|
70
|
+
'profile'
|
|
71
|
+
] === 'extended'
|
|
72
|
+
? 'extended'
|
|
73
|
+
: 'basic';
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether to wrap rendered text in the UI library's typography components.
|
|
77
|
+
*
|
|
78
|
+
* Element option first, then the namespaced default, then **true**. It is
|
|
79
|
+
* switchable because those components carry their own margins, which can
|
|
80
|
+
* fight a form's own spacing - the same collision a non-zero column gap
|
|
81
|
+
* produces.
|
|
82
|
+
*/
|
|
83
|
+
export const typographyEnabled = (
|
|
84
|
+
options: Record<string, unknown> | undefined,
|
|
85
|
+
config: unknown,
|
|
86
|
+
): boolean => {
|
|
87
|
+
if (typeof options?.['typography'] === 'boolean') {
|
|
88
|
+
return options['typography'] as boolean;
|
|
89
|
+
}
|
|
90
|
+
const fromConfig = markupConfig(config)?.['typography'];
|
|
91
|
+
return typeof fromConfig === 'boolean' ? fromConfig : true;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* What this element is asking for, and whether it can have it.
|
|
96
|
+
*
|
|
97
|
+
* A refused Markdown request keeps `interpolate`: the two are independent, and
|
|
98
|
+
* switching the profile off is not a reason to stop formatting a message.
|
|
99
|
+
*/
|
|
100
|
+
export const resolveMarkup = (
|
|
101
|
+
options: Record<string, unknown> | undefined,
|
|
102
|
+
config: unknown,
|
|
103
|
+
): MarkupRequest => {
|
|
104
|
+
const requested = options?.['markup'];
|
|
105
|
+
const interpolate = options?.['interpolate'] === true;
|
|
106
|
+
const typography = typographyEnabled(options, config);
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
requested !== undefined &&
|
|
110
|
+
requested !== 'plain' &&
|
|
111
|
+
requested !== 'markdown'
|
|
112
|
+
) {
|
|
113
|
+
return {
|
|
114
|
+
markdown: false,
|
|
115
|
+
interpolate,
|
|
116
|
+
typography,
|
|
117
|
+
diagnostic: 'markupUnsupported',
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
if (requested === 'markdown' && !markdownEnabled(config)) {
|
|
121
|
+
return {
|
|
122
|
+
markdown: false,
|
|
123
|
+
interpolate,
|
|
124
|
+
typography,
|
|
125
|
+
diagnostic: 'markdownDisabled',
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return { markdown: requested === 'markdown', interpolate, typography };
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Whether the extended renderer should take this element at all.
|
|
133
|
+
*
|
|
134
|
+
* Deliberately reads the **requested** markup rather than the resolved one: an
|
|
135
|
+
* element asking for Markdown that the host has switched off still belongs
|
|
136
|
+
* here, because the refusal has to be reported. Leaving it to the plain
|
|
137
|
+
* renderer would render the source silently, which is the failure this
|
|
138
|
+
* diagnostic exists to prevent.
|
|
139
|
+
*/
|
|
140
|
+
export const wantsMarkup = (
|
|
141
|
+
options: Record<string, unknown> | undefined,
|
|
142
|
+
): boolean =>
|
|
143
|
+
options?.['interpolate'] === true ||
|
|
144
|
+
(options?.['markup'] !== undefined && options['markup'] !== 'plain');
|