lit-intlayer 9.9.3 → 9.9.4
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/dist/cjs/client/IntlayerBinding.cjs +93 -1
- package/dist/cjs/client/index.cjs +28 -1
- package/dist/cjs/client/installIntlayer.cjs +6 -1
- package/dist/cjs/client/useDictionary.cjs +24 -1
- package/dist/cjs/client/useDictionaryDynamic.cjs +47 -1
- package/dist/cjs/client/useIntlayer.cjs +30 -1
- package/dist/cjs/client/useLocale.cjs +90 -1
- package/dist/cjs/client/useLocaleStorage.cjs +56 -1
- package/dist/cjs/client/useRewriteURL.cjs +48 -1
- package/dist/cjs/editor/useEditor.cjs +3 -1
- package/dist/cjs/format/index.cjs +19 -1
- package/dist/cjs/format/useCompact.cjs +34 -1
- package/dist/cjs/format/useCurrency.cjs +34 -1
- package/dist/cjs/format/useDate.cjs +52 -1
- package/dist/cjs/format/useIntl.cjs +66 -1
- package/dist/cjs/format/useList.cjs +34 -1
- package/dist/cjs/format/useNumber.cjs +40 -1
- package/dist/cjs/format/usePercentage.cjs +34 -1
- package/dist/cjs/format/useRelativeTime.cjs +34 -1
- package/dist/cjs/format/useUnit.cjs +34 -1
- package/dist/cjs/getDictionary.cjs +14 -1
- package/dist/cjs/getIntlayer.cjs +14 -1
- package/dist/cjs/html/HTMLRenderer.cjs +53 -1
- package/dist/cjs/html/index.cjs +8 -1
- package/dist/cjs/html/installIntlayerHTML.cjs +90 -1
- package/dist/cjs/html/types.cjs +1 -1
- package/dist/cjs/index.cjs +41 -1
- package/dist/cjs/markdown/MarkdownRenderer.cjs +97 -1
- package/dist/cjs/markdown/compiler.cjs +72 -1
- package/dist/cjs/markdown/index.cjs +12 -1
- package/dist/cjs/markdown/installIntlayerMarkdown.cjs +98 -1
- package/dist/cjs/markdown/runtime.cjs +86 -1
- package/dist/cjs/plugins.cjs +217 -3
- package/dist/cjs/renderIntlayerNode.cjs +29 -1
- package/dist/esm/client/IntlayerBinding.mjs +75 -33
- package/dist/esm/client/index.mjs +9 -9
- package/dist/esm/client/installIntlayer.mjs +1 -1
- package/dist/esm/client/useDictionary.mjs +22 -10
- package/dist/esm/client/useDictionaryDynamic.mjs +44 -11
- package/dist/esm/client/useIntlayer.mjs +28 -10
- package/dist/esm/client/useLocale.mjs +71 -19
- package/dist/esm/client/useLocaleStorage.mjs +45 -12
- package/dist/esm/client/useRewriteURL.mjs +36 -18
- package/dist/esm/editor/useEditor.mjs +1 -1
- package/dist/esm/format/index.mjs +10 -10
- package/dist/esm/format/useCompact.mjs +23 -19
- package/dist/esm/format/useCurrency.mjs +23 -19
- package/dist/esm/format/useDate.mjs +37 -27
- package/dist/esm/format/useIntl.mjs +54 -13
- package/dist/esm/format/useList.mjs +23 -19
- package/dist/esm/format/useNumber.mjs +29 -19
- package/dist/esm/format/usePercentage.mjs +23 -19
- package/dist/esm/format/useRelativeTime.mjs +23 -19
- package/dist/esm/format/useUnit.mjs +23 -19
- package/dist/esm/getDictionary.mjs +11 -6
- package/dist/esm/getIntlayer.mjs +11 -6
- package/dist/esm/html/HTMLRenderer.mjs +38 -13
- package/dist/esm/html/index.mjs +3 -3
- package/dist/esm/html/installIntlayerHTML.mjs +82 -13
- package/dist/esm/index.mjs +12 -12
- package/dist/esm/markdown/MarkdownRenderer.mjs +66 -26
- package/dist/esm/markdown/compiler.mjs +68 -22
- package/dist/esm/markdown/index.mjs +4 -4
- package/dist/esm/markdown/installIntlayerMarkdown.mjs +87 -23
- package/dist/esm/markdown/runtime.mjs +58 -28
- package/dist/esm/plugins.mjs +156 -106
- package/dist/esm/renderIntlayerNode.mjs +17 -17
- package/dist/installIntlayer-CCMOc1XT.mjs +174 -0
- package/dist/installIntlayer-HB8aIQ-i.js +203 -0
- package/package.json +7 -7
- package/dist/installIntlayer-CFvvijL7.js +0 -1
- package/dist/installIntlayer-CTpPd7yz.mjs +0 -82
|
@@ -1,17 +1,86 @@
|
|
|
1
1
|
// src/html/installIntlayerHTML.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Singleton instance
|
|
4
|
+
*/
|
|
5
|
+
var instance = null;
|
|
6
|
+
/**
|
|
7
|
+
* Create and return a single IntlayerHTMLProvider instance.
|
|
8
|
+
*/
|
|
9
|
+
var createIntlayerHTMLClient = (renderHTML) => {
|
|
10
|
+
if (instance) return instance;
|
|
11
|
+
instance = { renderHTML };
|
|
12
|
+
return instance;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Install the Intlayer HTML provider for your Lit application.
|
|
16
|
+
*
|
|
17
|
+
* The installed renderer is used by `<intlayer-html-renderer>` and
|
|
18
|
+
* `useHTMLRenderer()`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { installIntlayerHTML } from 'lit-intlayer';
|
|
23
|
+
*
|
|
24
|
+
* installIntlayerHTML({
|
|
25
|
+
* components: {
|
|
26
|
+
* a: ({ href, children }) => `<a class="styled-link" href="${href}">${children}</a>`,
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
var installIntlayerHTML = (pluginOptions) => {
|
|
32
|
+
let renderHTML;
|
|
33
|
+
if (typeof pluginOptions === "function") renderHTML = pluginOptions;
|
|
5
34
|
else {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
35
|
+
const { components, renderHTML: customRender } = pluginOptions ?? {};
|
|
36
|
+
if (customRender) renderHTML = customRender;
|
|
37
|
+
else renderHTML = (rawHTML, overrides) => {
|
|
38
|
+
const overrideComponents = overrides && typeof overrides === "object" && "components" in overrides ? overrides.components : overrides;
|
|
39
|
+
const mergedComponents = {
|
|
40
|
+
...components ?? {},
|
|
41
|
+
...overrideComponents ?? {}
|
|
11
42
|
};
|
|
12
|
-
|
|
13
|
-
|
|
43
|
+
if (!Object.keys(mergedComponents).length) return rawHTML;
|
|
44
|
+
return rawHTML;
|
|
45
|
+
};
|
|
14
46
|
}
|
|
15
|
-
return
|
|
16
|
-
}
|
|
17
|
-
|
|
47
|
+
return createIntlayerHTMLClient(renderHTML);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Access the installed IntlayerHTMLProvider.
|
|
51
|
+
* Returns a fallback renderer that outputs the raw HTML string if no
|
|
52
|
+
* provider has been installed.
|
|
53
|
+
*/
|
|
54
|
+
var useHTML = () => {
|
|
55
|
+
if (instance) return instance;
|
|
56
|
+
return { renderHTML: (rawHTML) => rawHTML };
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Asynchronously install an HTML renderer whose implementation is loaded
|
|
60
|
+
* via a dynamic `import()`.
|
|
61
|
+
*
|
|
62
|
+
* Use this to keep a heavy HTML sanitiser / component renderer out of the
|
|
63
|
+
* initial bundle — the loader is only called the first time this function
|
|
64
|
+
* is executed.
|
|
65
|
+
*
|
|
66
|
+
* The returned promise resolves once the provider is ready. Any calls to
|
|
67
|
+
* `useHTML()` before the promise resolves will use the fallback (identity)
|
|
68
|
+
* renderer.
|
|
69
|
+
*
|
|
70
|
+
* @param loader - A zero-argument async function that resolves to either a
|
|
71
|
+
* `RenderHTMLFunction` or an `IntlayerHTMLPluginOptions` object.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* // Load a custom HTML sanitiser only when needed
|
|
76
|
+
* await installIntlayerHTMLDynamic(async () => {
|
|
77
|
+
* const DOMPurify = await import('dompurify');
|
|
78
|
+
* return (html) => DOMPurify.sanitize(html);
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
var installIntlayerHTMLDynamic = async (loader) => {
|
|
83
|
+
if (instance) return instance;
|
|
84
|
+
return installIntlayerHTML(await loader());
|
|
85
|
+
};
|
|
86
|
+
export { createIntlayerHTMLClient, installIntlayerHTML, installIntlayerHTMLDynamic, useHTML };
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { getLocaleInStorage
|
|
2
|
-
import { a as
|
|
3
|
-
import { getDictionary
|
|
4
|
-
import { getIntlayer
|
|
5
|
-
import { IntlayerBinding
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { useIntlayer
|
|
10
|
-
import { LocaleController
|
|
11
|
-
import { useRewriteURL
|
|
1
|
+
import { getLocaleInStorage, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, useLocaleCookie, useLocaleStorage } from "./client/useLocaleStorage.mjs";
|
|
2
|
+
import { a as useEditor, i as installIntlayer, n as createIntlayerClient, r as getIntlayerClient, t as IntlayerClient } from "../installIntlayer-CCMOc1XT.mjs";
|
|
3
|
+
import { getDictionary } from "./getDictionary.mjs";
|
|
4
|
+
import { getIntlayer } from "./getIntlayer.mjs";
|
|
5
|
+
import { IntlayerBinding, createContentProxy } from "./client/IntlayerBinding.mjs";
|
|
6
|
+
import { useDictionary } from "./client/useDictionary.mjs";
|
|
7
|
+
import { useDictionaryDynamic } from "./client/useDictionaryDynamic.mjs";
|
|
8
|
+
import { getPlugins, htmlPlugin, insertionPlugin, intlayerNodePlugins, markdownPlugin, markdownStringPlugin } from "./plugins.mjs";
|
|
9
|
+
import { useIntlayer } from "./client/useIntlayer.mjs";
|
|
10
|
+
import { LocaleController, useLocale } from "./client/useLocale.mjs";
|
|
11
|
+
import { useRewriteURL } from "./client/useRewriteURL.mjs";
|
|
12
12
|
import "./client/index.mjs";
|
|
13
|
-
export {
|
|
13
|
+
export { IntlayerBinding, IntlayerClient, LocaleController, createContentProxy, createIntlayerClient, getDictionary, getIntlayer, getIntlayerClient, getLocaleInStorage, getPlugins, htmlPlugin, insertionPlugin, installIntlayer, intlayerNodePlugins, localeCookie, localeInStorage, markdownPlugin, markdownStringPlugin, setLocaleCookie, setLocaleInStorage, useDictionary, useDictionaryDynamic, useEditor, useIntlayer, useLocale, useLocaleCookie, useLocaleStorage, useRewriteURL };
|
|
@@ -1,54 +1,94 @@
|
|
|
1
|
-
import { LitElement
|
|
2
|
-
import { unsafeHTML
|
|
3
|
-
import { compileMarkdown
|
|
4
|
-
import { useMarkdown
|
|
1
|
+
import { LitElement, html, nothing } from "lit";
|
|
2
|
+
import { unsafeHTML } from "lit/directives/unsafe-html.js";
|
|
3
|
+
import { compileMarkdown } from "./compiler.mjs";
|
|
4
|
+
import { useMarkdown } from "./installIntlayerMarkdown.mjs";
|
|
5
5
|
// src/markdown/MarkdownRenderer.ts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Renders markdown to an HTML string without using the global provider.
|
|
8
|
+
* Pass the result to `unsafeHTML()` in a Lit template.
|
|
9
|
+
*/
|
|
10
|
+
var renderMarkdown = (content, { forceBlock, forceInline, preserveFrontmatter, tagfilter } = {}) => {
|
|
11
|
+
return compileMarkdown(content, {
|
|
12
|
+
forceBlock,
|
|
13
|
+
forceInline,
|
|
14
|
+
preserveFrontmatter,
|
|
15
|
+
tagfilter
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Returns a render function that uses the global provider's configuration
|
|
20
|
+
* (installed via `installIntlayerMarkdown`), falling back to `compileMarkdown`.
|
|
21
|
+
*/
|
|
22
|
+
var useMarkdownRenderer = (props = {}) => {
|
|
23
|
+
const provider = useMarkdown();
|
|
24
|
+
return (content) => provider.renderMarkdown(content, {
|
|
25
|
+
forceBlock: props.forceBlock,
|
|
26
|
+
forceInline: props.forceInline,
|
|
27
|
+
preserveFrontmatter: props.preserveFrontmatter,
|
|
28
|
+
tagfilter: props.tagfilter
|
|
29
|
+
}, props.components, props.wrapper);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* `<intlayer-markdown-renderer>` — renders a markdown string as HTML.
|
|
33
|
+
*
|
|
34
|
+
* Renders into the light DOM (no shadow root) so that the host page's CSS
|
|
35
|
+
* can style the generated markup.
|
|
36
|
+
*
|
|
37
|
+
* Uses the globally installed markdown renderer when available
|
|
38
|
+
* (see `installIntlayerMarkdown`), or falls back to the default compiler.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```html
|
|
42
|
+
* <intlayer-markdown-renderer content="# Hello **World**">
|
|
43
|
+
* </intlayer-markdown-renderer>
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { html } from 'lit';
|
|
49
|
+
* import 'lit-intlayer/markdown'; // registers the element
|
|
50
|
+
*
|
|
51
|
+
* html`<intlayer-markdown-renderer .content=${myMarkdown}></intlayer-markdown-renderer>`
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
var MarkdownRenderer = class extends LitElement {
|
|
20
55
|
static properties = {
|
|
21
56
|
content: { type: [String, Object] },
|
|
22
57
|
forceBlock: { type: Boolean },
|
|
23
58
|
forceInline: { type: Boolean },
|
|
24
59
|
preserveFrontmatter: { type: Boolean },
|
|
25
60
|
tagfilter: { type: Boolean },
|
|
26
|
-
renderMarkdown: { attribute:
|
|
61
|
+
renderMarkdown: { attribute: false }
|
|
27
62
|
};
|
|
63
|
+
/** The markdown string to compile and render. */
|
|
28
64
|
content = "";
|
|
29
65
|
forceBlock;
|
|
30
66
|
forceInline;
|
|
31
67
|
preserveFrontmatter;
|
|
32
68
|
tagfilter;
|
|
69
|
+
/** Optional custom render function — overrides the global provider. */
|
|
33
70
|
renderMarkdownFn;
|
|
71
|
+
/** Render into light DOM so host-page CSS can style the output. */
|
|
34
72
|
createRenderRoot() {
|
|
35
73
|
return this;
|
|
36
74
|
}
|
|
37
75
|
render() {
|
|
38
|
-
if (!this.content) return
|
|
39
|
-
let
|
|
40
|
-
|
|
76
|
+
if (!this.content) return nothing;
|
|
77
|
+
let compiled;
|
|
78
|
+
if (this.renderMarkdownFn) compiled = this.renderMarkdownFn(this.content, {
|
|
41
79
|
forceBlock: this.forceBlock,
|
|
42
80
|
forceInline: this.forceInline,
|
|
43
81
|
preserveFrontmatter: this.preserveFrontmatter,
|
|
44
82
|
tagfilter: this.tagfilter
|
|
45
|
-
}, void 0, void 0)
|
|
83
|
+
}, void 0, void 0);
|
|
84
|
+
else compiled = useMarkdownRenderer({
|
|
46
85
|
forceBlock: this.forceBlock,
|
|
47
86
|
forceInline: this.forceInline,
|
|
48
87
|
preserveFrontmatter: this.preserveFrontmatter,
|
|
49
88
|
tagfilter: this.tagfilter
|
|
50
|
-
})(this.content)
|
|
89
|
+
})(this.content);
|
|
90
|
+
return html`${unsafeHTML(compiled)}`;
|
|
51
91
|
}
|
|
52
92
|
};
|
|
53
|
-
customElements.get("intlayer-markdown-renderer")
|
|
54
|
-
export {
|
|
93
|
+
if (!customElements.get("intlayer-markdown-renderer")) customElements.define("intlayer-markdown-renderer", MarkdownRenderer);
|
|
94
|
+
export { MarkdownRenderer, renderMarkdown, useMarkdownRenderer };
|
|
@@ -1,24 +1,70 @@
|
|
|
1
|
-
import { compileWithOptions
|
|
2
|
-
import
|
|
1
|
+
import { compileWithOptions, parseMarkdown as parseMarkdown$1, renderMarkdownAst } from "@intlayer/core/markdown";
|
|
2
|
+
import litRuntime from "./runtime.mjs";
|
|
3
3
|
// src/markdown/compiler.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Lit adapter for the framework-agnostic markdown processor.
|
|
6
|
+
*
|
|
7
|
+
* Compiles markdown to an HTML string for use with Lit's `unsafeHTML` directive
|
|
8
|
+
* or the `<intlayer-markdown-renderer>` custom element.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* **Step 1 of 2 — parse only.**
|
|
12
|
+
* Converts a raw markdown string into a `ParsedMarkdown` AST without rendering
|
|
13
|
+
* any HTML. Use this when you need to:
|
|
14
|
+
* - Cache the parsed result and render it several times with different options.
|
|
15
|
+
* - Inspect or transform the AST before rendering.
|
|
16
|
+
* - Defer the render step to a later point.
|
|
17
|
+
*
|
|
18
|
+
* @param markdown - The markdown source string.
|
|
19
|
+
* @param options - Options that affect parsing (sanitizer, slugify, …).
|
|
20
|
+
* @returns A `ParsedMarkdown` AST ready to be passed to `compileMarkdown`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const ast = parseMarkdown('# Hello **World**');
|
|
25
|
+
* const html = compileMarkdown(ast);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
var parseMarkdown = (markdown = "", options = {}) => {
|
|
29
|
+
const { components, namedCodesToUnicode, sanitizer, slugify, ...compilerOptions } = options;
|
|
30
|
+
return parseMarkdown$1(markdown, {
|
|
31
|
+
runtime: litRuntime,
|
|
32
|
+
components,
|
|
33
|
+
namedCodesToUnicode,
|
|
34
|
+
sanitizer,
|
|
35
|
+
slugify
|
|
36
|
+
}, compilerOptions);
|
|
23
37
|
};
|
|
24
|
-
|
|
38
|
+
/**
|
|
39
|
+
* **Steps 1 + 2 — parse and render in one shot.**
|
|
40
|
+
* Accepts a raw markdown string or a pre-parsed `ParsedMarkdown` AST and
|
|
41
|
+
* returns an HTML string. Use with Lit's `unsafeHTML` directive or the
|
|
42
|
+
* `<intlayer-markdown-renderer>` custom element.
|
|
43
|
+
*
|
|
44
|
+
* @param input - Markdown string or pre-parsed AST.
|
|
45
|
+
* @param options - Rendering options (custom components, sanitizer, slugify, …).
|
|
46
|
+
* @returns An HTML string representing the rendered markdown.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
|
51
|
+
*
|
|
52
|
+
* class MyElement extends LitElement {
|
|
53
|
+
* render() {
|
|
54
|
+
* return html`${unsafeHTML(compileMarkdown('# Hello **World**'))}`;
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
var compileMarkdown = (input = "", options = {}) => {
|
|
60
|
+
if (typeof input === "string") return compileWithOptions(input, litRuntime, options);
|
|
61
|
+
const { components, namedCodesToUnicode, sanitizer, slugify, ...compilerOptions } = options;
|
|
62
|
+
return renderMarkdownAst(input, {
|
|
63
|
+
runtime: litRuntime,
|
|
64
|
+
components,
|
|
65
|
+
namedCodesToUnicode,
|
|
66
|
+
sanitizer,
|
|
67
|
+
slugify
|
|
68
|
+
}, compilerOptions);
|
|
69
|
+
};
|
|
70
|
+
export { compileMarkdown, parseMarkdown };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { compileMarkdown
|
|
2
|
-
import { installIntlayerMarkdown
|
|
3
|
-
import { MarkdownRenderer
|
|
4
|
-
export {
|
|
1
|
+
import { compileMarkdown, parseMarkdown } from "./compiler.mjs";
|
|
2
|
+
import { installIntlayerMarkdown, installIntlayerMarkdownDynamic, useMarkdown } from "./installIntlayerMarkdown.mjs";
|
|
3
|
+
import { MarkdownRenderer, renderMarkdown, useMarkdownRenderer } from "./MarkdownRenderer.mjs";
|
|
4
|
+
export { MarkdownRenderer, compileMarkdown, installIntlayerMarkdown, installIntlayerMarkdownDynamic, parseMarkdown, renderMarkdown, useMarkdown, useMarkdownRenderer };
|
|
@@ -1,30 +1,94 @@
|
|
|
1
|
-
import { compileMarkdown
|
|
1
|
+
import { compileMarkdown } from "./compiler.mjs";
|
|
2
2
|
// src/markdown/installIntlayerMarkdown.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Singleton instance
|
|
5
|
+
*/
|
|
6
|
+
var instance = null;
|
|
7
|
+
/**
|
|
8
|
+
* Create and return a single IntlayerMarkdownProvider instance.
|
|
9
|
+
*/
|
|
10
|
+
var createIntlayerMarkdownClient = (renderMarkdown, components) => {
|
|
11
|
+
if (instance) return instance;
|
|
12
|
+
instance = {
|
|
13
|
+
components,
|
|
14
|
+
renderMarkdown
|
|
15
|
+
};
|
|
16
|
+
return instance;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Install the Intlayer Markdown provider for your Lit application.
|
|
20
|
+
*
|
|
21
|
+
* Call once at startup, before any elements that render markdown are connected.
|
|
22
|
+
* The installed renderer is used by `<intlayer-markdown-renderer>` and
|
|
23
|
+
* `useMarkdownRenderer()`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { installIntlayerMarkdown } from 'lit-intlayer/markdown';
|
|
28
|
+
*
|
|
29
|
+
* installIntlayerMarkdown({ forceBlock: true });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
var installIntlayerMarkdown = (pluginOptions) => {
|
|
33
|
+
let renderMarkdown;
|
|
34
|
+
let providerComponents;
|
|
35
|
+
if (typeof pluginOptions === "function") renderMarkdown = pluginOptions;
|
|
9
36
|
else {
|
|
10
|
-
|
|
11
|
-
|
|
37
|
+
const { components, wrapper, forceBlock, forceInline, preserveFrontmatter, tagfilter, renderMarkdown: customRender } = pluginOptions ?? {};
|
|
38
|
+
providerComponents = components;
|
|
39
|
+
if (customRender) renderMarkdown = customRender;
|
|
12
40
|
else {
|
|
13
|
-
|
|
14
|
-
forceBlock
|
|
15
|
-
forceInline
|
|
16
|
-
preserveFrontmatter
|
|
17
|
-
tagfilter
|
|
41
|
+
const internalOptions = {
|
|
42
|
+
forceBlock,
|
|
43
|
+
forceInline,
|
|
44
|
+
preserveFrontmatter,
|
|
45
|
+
tagfilter
|
|
18
46
|
};
|
|
19
|
-
|
|
20
|
-
...
|
|
21
|
-
forceBlock:
|
|
22
|
-
forceInline:
|
|
23
|
-
preserveFrontmatter:
|
|
24
|
-
tagfilter:
|
|
47
|
+
renderMarkdown = (markdown, options, componentsOverride, wrapperOverride) => compileMarkdown(markdown, {
|
|
48
|
+
...internalOptions,
|
|
49
|
+
forceBlock: options?.forceBlock ?? internalOptions.forceBlock,
|
|
50
|
+
forceInline: options?.forceInline ?? internalOptions.forceInline,
|
|
51
|
+
preserveFrontmatter: options?.preserveFrontmatter ?? internalOptions.preserveFrontmatter,
|
|
52
|
+
tagfilter: options?.tagfilter ?? internalOptions.tagfilter
|
|
25
53
|
});
|
|
26
54
|
}
|
|
27
55
|
}
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
56
|
+
return createIntlayerMarkdownClient(renderMarkdown, providerComponents);
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Access the installed IntlayerMarkdownProvider.
|
|
60
|
+
* Returns a fallback renderer that outputs the raw markdown string if no
|
|
61
|
+
* provider has been installed.
|
|
62
|
+
*/
|
|
63
|
+
var useMarkdown = () => {
|
|
64
|
+
if (instance) return instance;
|
|
65
|
+
return { renderMarkdown: (markdown) => compileMarkdown(markdown) };
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Asynchronously install a markdown renderer whose implementation is loaded
|
|
69
|
+
* via a dynamic `import()`.
|
|
70
|
+
*
|
|
71
|
+
* Use this to keep the markdown compiler out of the initial bundle — the
|
|
72
|
+
* loader is only called the first time this function is executed.
|
|
73
|
+
*
|
|
74
|
+
* The returned promise resolves once the provider is ready. Any calls to
|
|
75
|
+
* `useMarkdown()` before the promise resolves will use the fallback
|
|
76
|
+
* (raw-string) renderer.
|
|
77
|
+
*
|
|
78
|
+
* @param loader - A zero-argument async function that resolves to either a
|
|
79
|
+
* `RenderMarkdownFunction` or an `IntlayerMarkdownPluginOptions` object.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* // Load a custom markdown renderer (e.g. marked) only when needed
|
|
84
|
+
* await installIntlayerMarkdownDynamic(async () => {
|
|
85
|
+
* const { marked } = await import('marked');
|
|
86
|
+
* return (markdown) => marked(markdown) as string;
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
var installIntlayerMarkdownDynamic = async (loader) => {
|
|
91
|
+
if (instance) return instance;
|
|
92
|
+
return installIntlayerMarkdown(await loader());
|
|
93
|
+
};
|
|
94
|
+
export { createIntlayerMarkdownClient, installIntlayerMarkdown, installIntlayerMarkdownDynamic, useMarkdown };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/markdown/runtime.ts
|
|
2
|
-
var
|
|
2
|
+
var SELF_CLOSING_TAGS = /* @__PURE__ */ new Set([
|
|
3
3
|
"area",
|
|
4
4
|
"base",
|
|
5
5
|
"br",
|
|
@@ -14,37 +14,67 @@ var e = /* @__PURE__ */ new Set([
|
|
|
14
14
|
"source",
|
|
15
15
|
"track",
|
|
16
16
|
"wbr"
|
|
17
|
-
])
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (typeof
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
]);
|
|
18
|
+
var buildPropsString = (props) => {
|
|
19
|
+
if (!props) return "";
|
|
20
|
+
return Object.entries(props).filter(([key, value]) => value !== void 0 && value !== null && key !== "key").map(([key, value]) => {
|
|
21
|
+
if (typeof value === "boolean") return value ? key : "";
|
|
22
|
+
if (key === "_innerHTML") return "";
|
|
23
|
+
return `${key}="${String(value).replace(/&/g, "&").replace(/"/g, """)}"`;
|
|
24
|
+
}).filter(Boolean).join(" ");
|
|
25
|
+
};
|
|
26
|
+
var LIT_FRAGMENT = "__lit_fragment__";
|
|
27
|
+
/**
|
|
28
|
+
* Lit-specific MarkdownRuntime that produces HTML strings.
|
|
29
|
+
*/
|
|
30
|
+
var litRuntime = {
|
|
31
|
+
createElement: (type, props, ...children) => {
|
|
32
|
+
const childStr = children.flat(1e400).join("");
|
|
33
|
+
if (type === LIT_FRAGMENT) return childStr;
|
|
34
|
+
if (typeof type === "string") {
|
|
35
|
+
const propsStr = buildPropsString(props);
|
|
36
|
+
const propsWithSpace = propsStr ? ` ${propsStr}` : "";
|
|
37
|
+
const innerHTML = props?._innerHTML;
|
|
38
|
+
if (SELF_CLOSING_TAGS.has(type)) return `<${type}${propsWithSpace}>`;
|
|
39
|
+
if (innerHTML !== void 0) return `<${type}${propsWithSpace}>${innerHTML}</${type}>`;
|
|
40
|
+
return `<${type}${propsWithSpace}>${childStr}</${type}>`;
|
|
24
41
|
}
|
|
25
|
-
if (typeof
|
|
26
|
-
|
|
27
|
-
...
|
|
28
|
-
children:
|
|
42
|
+
if (typeof type === "function") try {
|
|
43
|
+
const result = type({
|
|
44
|
+
...props,
|
|
45
|
+
children: childStr
|
|
29
46
|
});
|
|
30
|
-
return typeof
|
|
47
|
+
return typeof result === "string" ? result : childStr;
|
|
31
48
|
} catch {
|
|
32
|
-
return
|
|
49
|
+
return childStr;
|
|
33
50
|
}
|
|
34
|
-
return
|
|
51
|
+
return childStr;
|
|
52
|
+
},
|
|
53
|
+
cloneElement: (element, _props, ..._children) => {
|
|
54
|
+
return typeof element === "string" ? element : String(element ?? "");
|
|
35
55
|
},
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
56
|
+
Fragment: LIT_FRAGMENT,
|
|
57
|
+
/**
|
|
58
|
+
* Normalize props to standard HTML attribute names.
|
|
59
|
+
*/
|
|
60
|
+
normalizeProps: (_tag, props) => {
|
|
61
|
+
const normalized = {};
|
|
62
|
+
for (const [key, value] of Object.entries(props)) if (key === "className") normalized.class = value;
|
|
63
|
+
else if (key === "htmlFor") normalized.for = value;
|
|
64
|
+
else if (key === "dangerouslySetInnerHTML" && value?.__html) normalized._innerHTML = value.__html;
|
|
65
|
+
else normalized[key] = value;
|
|
66
|
+
return normalized;
|
|
42
67
|
}
|
|
43
|
-
}, i = (e = {}) => {
|
|
44
|
-
let { onCreateElement: t } = e;
|
|
45
|
-
return t ? {
|
|
46
|
-
...r,
|
|
47
|
-
createElement: (e, n, ...r) => t(e, n, r)
|
|
48
|
-
} : r;
|
|
49
68
|
};
|
|
50
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Creates a Lit runtime with a custom createElement for advanced use cases.
|
|
71
|
+
*/
|
|
72
|
+
var createLitRuntime = (options = {}) => {
|
|
73
|
+
const { onCreateElement } = options;
|
|
74
|
+
if (onCreateElement) return {
|
|
75
|
+
...litRuntime,
|
|
76
|
+
createElement: (type, props, ...children) => onCreateElement(type, props, children)
|
|
77
|
+
};
|
|
78
|
+
return litRuntime;
|
|
79
|
+
};
|
|
80
|
+
export { createLitRuntime, litRuntime as default, litRuntime };
|