@next-box/i18n 0.0.2-alpha-3.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 +21 -0
- package/README.md +126 -0
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/macros.cjs +2 -0
- package/dist/cjs/macros.cjs.map +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/production.analysis.txt +16 -0
- package/dist/types/cjs/components/I18nProvider.d.cts +28 -0
- package/dist/types/cjs/components/I18nProvider.d.cts.map +1 -0
- package/dist/types/cjs/components/Md.d.cts +10 -0
- package/dist/types/cjs/components/Md.d.cts.map +1 -0
- package/dist/types/cjs/helpers/injectVariables.d.cts +2 -0
- package/dist/types/cjs/helpers/injectVariables.d.cts.map +1 -0
- package/dist/types/cjs/index.d.cts +5 -0
- package/dist/types/cjs/index.d.cts.map +1 -0
- package/dist/types/cjs/macros.d.cts +2 -0
- package/dist/types/cjs/macros.d.cts.map +1 -0
- package/dist/types/cjs/main.d.cts +3 -0
- package/dist/types/cjs/main.d.cts.map +1 -0
- package/dist/types/cjs/types.d.cts +8 -0
- package/dist/types/cjs/types.d.cts.map +1 -0
- package/dist/types/esm/components/I18nProvider.d.ts +28 -0
- package/dist/types/esm/components/I18nProvider.d.ts.map +1 -0
- package/dist/types/esm/components/Md.d.ts +10 -0
- package/dist/types/esm/components/Md.d.ts.map +1 -0
- package/dist/types/esm/helpers/injectVariables.d.ts +2 -0
- package/dist/types/esm/helpers/injectVariables.d.ts.map +1 -0
- package/dist/types/esm/index.d.ts +5 -0
- package/dist/types/esm/index.d.ts.map +1 -0
- package/dist/types/esm/macros.d.ts +2 -0
- package/dist/types/esm/macros.d.ts.map +1 -0
- package/dist/types/esm/main.d.ts +3 -0
- package/dist/types/esm/main.d.ts.map +1 -0
- package/dist/types/esm/types.d.ts +8 -0
- package/dist/types/esm/types.d.ts.map +1 -0
- package/dist/types/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +70 -0
- package/rollup.config.mjs +17 -0
- package/src/__testUtils__/content.md +11 -0
- package/src/components/I18nProvider.tsx +24 -0
- package/src/components/Md.test.tsx +72 -0
- package/src/components/Md.tsx +39 -0
- package/src/helpers/injectVariables.ts +17 -0
- package/src/index.ts +4 -0
- package/src/macros.test.ts +35 -0
- package/src/macros.ts +15 -0
- package/src/main.test.ts +111 -0
- package/src/main.ts +23 -0
- package/src/types.ts +19 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dylan Aubrey
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @next-box/i18n
|
|
2
|
+
|
|
3
|
+
A type-safe content resolver with content path auto-complete, value preview, and markdown support.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/%40next-box%2Fi18n)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
> To use this library you must be able/willing to define your content in typescript and markdown files.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
# terminal
|
|
14
|
+
npm add @next-box/i18n
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Create content files
|
|
20
|
+
|
|
21
|
+
Content needs to be fed into the library's `createI18n` init function in the form of a serializable object. This can be defined in a single typescript file or multiple files with a barrel file. Each language for which you have content should be a root property on the object, i.e. ```{ en: {...}, fr: {...} }```
|
|
22
|
+
|
|
23
|
+
The function returns an object with the `t` and `tt` content resolver functions as properties. You can then use those functions to look up content by its path from the root of the object.
|
|
24
|
+
|
|
25
|
+
Below is a simplistic example of how to define a content object that would be compatible with `createI18n`.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
export const content = {
|
|
29
|
+
en: {
|
|
30
|
+
pages: {
|
|
31
|
+
homepage: {
|
|
32
|
+
documentTitle: 'This is the homepage',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
} as const; // This is important!
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Read the [typescript docs]((https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions)) on it for more details on `as const`.
|
|
40
|
+
|
|
41
|
+
### Create an instance of the i18n
|
|
42
|
+
|
|
43
|
+
If you pass the above content into `createI18n`, you can use the `t` and `tt` functions to retrieve the `documentTitle` like in the example below.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { createI18n } from '@next-box/i18n';
|
|
47
|
+
import { content } from './content.ts';
|
|
48
|
+
|
|
49
|
+
const { tt } = createI18n(content);
|
|
50
|
+
const documentTitle = tt('en.pages.homepage.documentTitle'); // path is type-checked against the object passed into `createI18n`
|
|
51
|
+
console.log(documentTitle); // Will log 'This is the homepage'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Templating
|
|
55
|
+
|
|
56
|
+
The `t` and `tt` functions support content with template variables. You can define template variables in content with double curley braces either side of a camelcase variable name, i.e. `Welcome back, {{fullName}}!`.
|
|
57
|
+
|
|
58
|
+
Then, when you want to retrieve that piece of content, you do so along with a second argument of key/value pairs used to populate the template variables like in the example below.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { createI18n } from '@next-box/i18n';
|
|
62
|
+
|
|
63
|
+
const content = {
|
|
64
|
+
en: {
|
|
65
|
+
greeting: 'Welcome back, {{fullName}}!',
|
|
66
|
+
},
|
|
67
|
+
} as const;
|
|
68
|
+
|
|
69
|
+
const { tt } = createI18n(content);
|
|
70
|
+
const greeting = tt('en.greeting', { fullName: 'Joe Bloggs' });
|
|
71
|
+
console.log(greeting); // Will log 'Welcome back, Joe Bloggs!'
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### Error handling
|
|
75
|
+
|
|
76
|
+
There are two types of errors to help guard against mistakes in retrieving content, build-time typescript errors and runtime javascript exceptions. The former prevents a developer from entering an invalid path into the `tt` function, while the latter provides a backstop in case the initial type errors were ignored.
|
|
77
|
+
|
|
78
|
+
> There is no error handling with the `t` function. It is indented to be much looser in terms of what a developer can enter and what happens when the function cannot resolve the path; it just returns `undefined`.
|
|
79
|
+
|
|
80
|
+
### Using markdown files
|
|
81
|
+
|
|
82
|
+
For blocks of content that consist of multiple paragraphs or different types of content, it can be easier to define these in markdown files. The library supports markdown files through the `md` macro.
|
|
83
|
+
|
|
84
|
+
Macros are a mechanism for running JavaScript functions at bundle-time. The value returned from these functions are directly inlined into your bundle.
|
|
85
|
+
|
|
86
|
+
Below is an example of a content object that includes markdown.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { md } from '@next-box/i18n/macros' with { type: 'macro' }; // This is important!
|
|
90
|
+
|
|
91
|
+
const content = {
|
|
92
|
+
en: {
|
|
93
|
+
contactUs: md('./src/markdown/contactUs.md'),
|
|
94
|
+
},
|
|
95
|
+
} as const;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> All the functions exported from the `'/macros'` sub-path are intended to be imported with the `with { type: 'macro' }` import attribute in conjunction with the use of the `unplugin-macros` [plugin](https://github.com/unplugin/unplugin-macros) or a compiler/bundler that supports the macros import attribute such as [Bun](https://bun.sh/) or [Parcel](https://parceljs.org/).
|
|
99
|
+
|
|
100
|
+
The `md` macro is designed to be used with the `Md` markdown render component and `I18nProvider` context provider. `Md` transforms markdown into React components, while `I18nProvider` provides a way to share a component mapper and `markdown-to-jsx` options between instances of `Md`.
|
|
101
|
+
|
|
102
|
+
> Under the hood, the library uses the `markdown-to-jsx` npm package to transform markdown into JSX.
|
|
103
|
+
|
|
104
|
+
The contrived example below illustrates how to use the `md` macro, `I18nProvider`, and `Md` component together.
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { md } from '@next-box/i18n/macros' with {type: 'macro'};
|
|
108
|
+
import { I18nProvider, Md } from '@next-box/i18n';
|
|
109
|
+
import { componentMapper } from './componentMapper.ts';
|
|
110
|
+
|
|
111
|
+
export const Component = () => {
|
|
112
|
+
return (
|
|
113
|
+
<I18nProvider componentMapper={componentMapper}>
|
|
114
|
+
<Md>{md('content.path.to.markdown.file')}</Md>
|
|
115
|
+
</I18nProvider>
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Changelog
|
|
121
|
+
|
|
122
|
+
Check out the [features, fixes and more](../../CHANGELOG.md) that go into each major, minor and patch version.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
@next-box/i18n is [MIT Licensed](LICENSE).
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var e=require("react/jsx-runtime"),r=require("react"),t=require("markdown-to-jsx"),n=require("zcb");const o=r.createContext({componentMapper:{}}),a=/{{[^\s{}]+}}/g,c=/{{ *([^ }]*) *}}/,p=(e,r)=>r?e.replaceAll(a,(e=>{const t=e.replace(c,"$1"),n=r[t];if(n)return n;throw new Error(`Markdown contains un-replaced variable '${t}'`)})):e;exports.I18nContext=o,exports.I18nProvider=({children:r,componentMapper:t,...n})=>e.jsx(o.Provider,{value:{componentMapper:t,...n},children:r}),exports.Md=({children:n,componentMapper:a,variables:c,...i})=>{const{componentMapper:s,...d}=r.useContext(o);return e.jsx(t,{options:{overrides:{...s,...a},...d,...i},children:p(n,c)})},exports.createI18n=e=>{const r=n.createConfigReader(e);return{t:(e,t)=>r.read(e,t),tt:(e,t)=>{const n=r.read(e,t);if(!n)throw new Error(`Path "${String(e)}" did not resolve to a value`);return n}}};
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../i18n/src//components/I18nProvider.tsx","../i18n/src//helpers/injectVariables.ts","../i18n/src//components/Md.tsx","../i18n/src//main.ts"],"sourcesContent":["import type MarkdownToJsx from 'markdown-to-jsx';\nimport { type ComponentProps, type JSX, type ReactNode, createContext } from 'react';\nimport { type ComponentMapper } from '#types.ts';\n\nexport const I18nContext = createContext<\n {\n componentMapper: ComponentMapper;\n } & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>\n>({\n componentMapper: {},\n});\n\nexport type I18nProviderProps = {\n children: ReactNode;\n componentMapper: ComponentMapper;\n} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;\n\nexport const I18nProvider = ({\n children,\n componentMapper,\n ...markdownToJsxOptions\n}: I18nProviderProps): JSX.Element => {\n return <I18nContext.Provider value={{ componentMapper, ...markdownToJsxOptions }}>{children}</I18nContext.Provider>;\n};\n","const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? // @ts-expect-error lib config needs updating\n markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue) {\n return replacementValue;\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n","import MarkdownToJsx from 'markdown-to-jsx';\nimport { type ComponentProps, type JSX, useContext } from 'react';\nimport { I18nContext } from '#components/I18nProvider.tsx';\nimport { injectVariables } from '#helpers/injectVariables.ts';\nimport { type ComponentMapper } from '#types.ts';\n\nexport type MdProps = {\n // The markdown to convert into JSX.\n children: string;\n // An object of key/value pairs, with the key being the\n // name of the html tag that maps to supported markdown or\n // the name of a React component, and the value being a\n // React component. This prop is designed as an override\n // for the componentMapper passed into the I18nProvider.\n componentMapper?: ComponentMapper;\n // An object of key/value pairs for content with template variables.\n variables?: Record<string, string | number>;\n} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;\n\nexport const Md = ({\n children,\n componentMapper: componentMapperOverrides,\n variables,\n ...markdownToJsxOptionOverrides\n}: MdProps): JSX.Element => {\n const { componentMapper, ...markdownToJsxOptions } = useContext(I18nContext);\n\n return (\n <MarkdownToJsx\n options={{\n overrides: { ...componentMapper, ...componentMapperOverrides },\n ...markdownToJsxOptions,\n ...markdownToJsxOptionOverrides,\n }}\n >\n {injectVariables(children, variables)}\n </MarkdownToJsx>\n );\n};\n","import { type AnyRecord, type Path, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt: <P extends Path<U>>(path: P, variables?: AnyRecord) => {\n const result = reader.read(path, variables);\n\n // This can resolve to a falsy value at runtime\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!result) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n },\n };\n};\n"],"names":["I18nContext","createContext","componentMapper","extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","Error","children","markdownToJsxOptions","_jsx","jsx","Provider","value","componentMapperOverrides","markdownToJsxOptionOverrides","useContext","MarkdownToJsx","options","overrides","i18n","reader","createConfigReader","t","key","read","tt","path","result","String"],"mappings":"iHAIO,MAAMA,EAAcC,EAAAA,cAIzB,CACAC,gBAAiB,CAAA,ICTbC,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EAEID,EAASE,WAAWL,GAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,GAAIE,EACF,OAAOA,EAGT,MAAM,IAAIC,MAAM,2CAA2CH,SAE7DJ,6CDCsB,EAC1BQ,WACAZ,qBACGa,KAEIC,EAAAC,IAACjB,EAAYkB,SAAQ,CAACC,MAAO,CAAEjB,qBAAoBa,GAAyBD,SAAAA,eEHnE,EAChBA,WACAZ,gBAAiBkB,EACjBb,eACGc,MAEH,MAAMnB,gBAAEA,KAAoBa,GAAyBO,EAAAA,WAAWtB,GAEhE,OACEgB,EAAAA,IAACO,EAAAA,CACCC,QAAS,CACPC,UAAW,IAAKvB,KAAoBkB,MACjCL,KACAM,GAGJhB,SAAAA,EAAgBS,EAAUP,yBChCYmB,IAC3C,MAAMC,EAASC,EAAsBF,mBAAAA,GAErC,MAAO,CAGLG,EAAG,CAACC,EAAavB,IAA8CoB,EAAOI,KAAKD,EAAKvB,GAChFyB,GAAI,CAAoBC,EAAS1B,KAC/B,MAAM2B,EAASP,EAAOI,KAAKE,EAAM1B,GAIjC,IAAK2B,EACH,MAAM,IAAIrB,MAAM,SAASsB,OAAOF,kCAGlC,OAAOC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"macros.cjs","sources":["../i18n/src//macros.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('Tagged template expected a path, but received none');\n }\n\n const fullPath = resolve(process.cwd(), path);\n // @ts-expect-error `md` function runs and build time and its return value\n // replaces the `md` invocation. We are forcing the return type to be the\n // input type to aid type inference when using `createI18n` and its\n // `tt` content resolvers.\n return readFileSync(fullPath, { encoding: 'utf8' }).trim();\n};\n"],"names":["path","Error","fullPath","resolve","process","cwd","readFileSync","encoding","trim"],"mappings":"wEAGqCA,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,sDAGlB,MAAMC,EAAWC,EAAAA,QAAQC,QAAQC,MAAOL,GAKxC,OAAOM,EAAAA,aAAaJ,EAAU,CAAEK,SAAU,SAAUC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{jsx as r}from"react/jsx-runtime";import{createContext as e,useContext as o}from"react";import n from"markdown-to-jsx";import{createConfigReader as t}from"zcb";const p=e({componentMapper:{}}),a=({children:e,componentMapper:o,...n})=>r(p.Provider,{value:{componentMapper:o,...n},children:e}),c=/{{[^\s{}]+}}/g,i=/{{ *([^ }]*) *}}/,m=(r,e)=>e?r.replaceAll(c,(r=>{const o=r.replace(i,"$1"),n=e[o];if(n)return n;throw new Error(`Markdown contains un-replaced variable '${o}'`)})):r,l=({children:e,componentMapper:t,variables:a,...c})=>{const{componentMapper:i,...l}=o(p);return r(n,{options:{overrides:{...i,...t},...l,...c},children:m(e,a)})},d=r=>{const e=t(r);return{t:(r,o)=>e.read(r,o),tt:(r,o)=>{const n=e.read(r,o);if(!n)throw new Error(`Path "${String(r)}" did not resolve to a value`);return n}}};export{p as I18nContext,a as I18nProvider,l as Md,d as createI18n};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../i18n/src//components/I18nProvider.tsx","../i18n/src//helpers/injectVariables.ts","../i18n/src//components/Md.tsx","../i18n/src//main.ts"],"sourcesContent":["import type MarkdownToJsx from 'markdown-to-jsx';\nimport { type ComponentProps, type JSX, type ReactNode, createContext } from 'react';\nimport { type ComponentMapper } from '#types.ts';\n\nexport const I18nContext = createContext<\n {\n componentMapper: ComponentMapper;\n } & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>\n>({\n componentMapper: {},\n});\n\nexport type I18nProviderProps = {\n children: ReactNode;\n componentMapper: ComponentMapper;\n} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;\n\nexport const I18nProvider = ({\n children,\n componentMapper,\n ...markdownToJsxOptions\n}: I18nProviderProps): JSX.Element => {\n return <I18nContext.Provider value={{ componentMapper, ...markdownToJsxOptions }}>{children}</I18nContext.Provider>;\n};\n","const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? // @ts-expect-error lib config needs updating\n markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue) {\n return replacementValue;\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n","import MarkdownToJsx from 'markdown-to-jsx';\nimport { type ComponentProps, type JSX, useContext } from 'react';\nimport { I18nContext } from '#components/I18nProvider.tsx';\nimport { injectVariables } from '#helpers/injectVariables.ts';\nimport { type ComponentMapper } from '#types.ts';\n\nexport type MdProps = {\n // The markdown to convert into JSX.\n children: string;\n // An object of key/value pairs, with the key being the\n // name of the html tag that maps to supported markdown or\n // the name of a React component, and the value being a\n // React component. This prop is designed as an override\n // for the componentMapper passed into the I18nProvider.\n componentMapper?: ComponentMapper;\n // An object of key/value pairs for content with template variables.\n variables?: Record<string, string | number>;\n} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;\n\nexport const Md = ({\n children,\n componentMapper: componentMapperOverrides,\n variables,\n ...markdownToJsxOptionOverrides\n}: MdProps): JSX.Element => {\n const { componentMapper, ...markdownToJsxOptions } = useContext(I18nContext);\n\n return (\n <MarkdownToJsx\n options={{\n overrides: { ...componentMapper, ...componentMapperOverrides },\n ...markdownToJsxOptions,\n ...markdownToJsxOptionOverrides,\n }}\n >\n {injectVariables(children, variables)}\n </MarkdownToJsx>\n );\n};\n","import { type AnyRecord, type Path, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt: <P extends Path<U>>(path: P, variables?: AnyRecord) => {\n const result = reader.read(path, variables);\n\n // This can resolve to a falsy value at runtime\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!result) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n },\n };\n};\n"],"names":["I18nContext","createContext","componentMapper","I18nProvider","children","markdownToJsxOptions","_jsx","Provider","value","extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","Error","Md","componentMapperOverrides","markdownToJsxOptionOverrides","useContext","MarkdownToJsx","options","overrides","createI18n","i18n","reader","createConfigReader","t","key","read","tt","path","result","String"],"mappings":"sKAIO,MAAMA,EAAcC,EAIzB,CACAC,gBAAiB,CAAA,IAQNC,EAAe,EAC1BC,WACAF,qBACGG,KAEIC,EAACN,EAAYO,SAAQ,CAACC,MAAO,CAAEN,qBAAoBG,GAAyBD,SAAAA,ICtB/EK,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EAEID,EAASE,WAAWL,GAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,GAAIE,EACF,OAAOA,EAGT,MAAM,IAAIC,MAAM,2CAA2CH,SAE7DJ,ECGOQ,EAAK,EAChBhB,WACAF,gBAAiBmB,EACjBR,eACGS,MAEH,MAAMpB,gBAAEA,KAAoBG,GAAyBkB,EAAWvB,GAEhE,OACEM,EAACkB,EAAAA,CACCC,QAAS,CACPC,UAAW,IAAKxB,KAAoBmB,MACjChB,KACAiB,GAGJX,SAAAA,EAAgBP,EAAUS,MChCpBc,EAAgCC,IAC3C,MAAMC,EAASC,EAAsBF,GAErC,MAAO,CAGLG,EAAG,CAACC,EAAanB,IAA8CgB,EAAOI,KAAKD,EAAKnB,GAChFqB,GAAI,CAAoBC,EAAStB,KAC/B,MAAMuB,EAASP,EAAOI,KAAKE,EAAMtB,GAIjC,IAAKuB,EACH,MAAM,IAAIjB,MAAM,SAASkB,OAAOF,kCAGlC,OAAOC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
-----------------------------
|
|
2
|
+
Rollup File Analysis
|
|
3
|
+
-----------------------------
|
|
4
|
+
bundle size: 530 Bytes
|
|
5
|
+
original size: 593 Bytes
|
|
6
|
+
code reduction: 10.62 %
|
|
7
|
+
module count: 1
|
|
8
|
+
|
|
9
|
+
██████████████████████████████████████████████████
|
|
10
|
+
file: /src/macros.ts
|
|
11
|
+
bundle space: 100 %
|
|
12
|
+
rendered size: 530 Bytes
|
|
13
|
+
original size: 593 Bytes
|
|
14
|
+
code reduction: 10.62 %
|
|
15
|
+
dependents: 0
|
|
16
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type MarkdownToJsx from 'markdown-to-jsx';
|
|
2
|
+
import { type ComponentProps, type JSX, type ReactNode } from 'react';
|
|
3
|
+
import { type ComponentMapper } from '#types.cts';
|
|
4
|
+
export declare const I18nContext: import("react").Context<{
|
|
5
|
+
componentMapper: ComponentMapper;
|
|
6
|
+
} & Omit<Partial<{
|
|
7
|
+
createElement: (tag: Parameters<import("markdown-to-jsx").MarkdownToJSX.CreateElement>[0], props: JSX.IntrinsicAttributes, ...children: ReactNode[]) => ReactNode;
|
|
8
|
+
disableAutoLink: boolean;
|
|
9
|
+
disableParsingRawHTML: boolean;
|
|
10
|
+
enforceAtxHeadings: boolean;
|
|
11
|
+
forceBlock: boolean;
|
|
12
|
+
forceInline: boolean;
|
|
13
|
+
forceWrapper: boolean;
|
|
14
|
+
namedCodesToUnicode: {
|
|
15
|
+
[key: string]: string;
|
|
16
|
+
};
|
|
17
|
+
overrides: import("markdown-to-jsx").MarkdownToJSX.Overrides;
|
|
18
|
+
renderRule: (next: () => ReactNode, node: import("markdown-to-jsx").MarkdownToJSX.ParserResult, renderChildren: import("markdown-to-jsx").MarkdownToJSX.RuleOutput, state: import("markdown-to-jsx").MarkdownToJSX.State) => ReactNode;
|
|
19
|
+
sanitizer: (value: string, tag: import("markdown-to-jsx").MarkdownToJSX.HTMLTags, attribute: string) => string | null;
|
|
20
|
+
slugify: (input: string, defaultFn: (input: string) => string) => string;
|
|
21
|
+
wrapper: import("react").ElementType | null;
|
|
22
|
+
}> | undefined, "overrides">>;
|
|
23
|
+
export type I18nProviderProps = {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
componentMapper: ComponentMapper;
|
|
26
|
+
} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;
|
|
27
|
+
export declare const I18nProvider: ({ children, componentMapper, ...markdownToJsxOptions }: I18nProviderProps) => JSX.Element;
|
|
28
|
+
//# sourceMappingURL=I18nProvider.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"I18nProvider.d.cts","sourceRoot":"","sources":["../../../../src/components/I18nProvider.ctsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,GAAG,EAAE,KAAK,SAAS,EAAiB,MAAM,OAAO,CAAC;AACrF,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,eAAO,MAAM,WAAW;qBAEH,eAAe;;+HAkBomT,GAAI;;;;;;;;;;;;;;;6BAd1oT,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,SAAS,CAAC;IACpB,eAAe,EAAE,eAAe,CAAC;CAClC,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;AAEvE,eAAO,MAAM,YAAY,2DAItB,iBAAiB,KAAG,GAAG,CAAC,OAE1B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import MarkdownToJsx from 'markdown-to-jsx';
|
|
2
|
+
import { type ComponentProps, type JSX } from 'react';
|
|
3
|
+
import { type ComponentMapper } from '#types.cts';
|
|
4
|
+
export type MdProps = {
|
|
5
|
+
children: string;
|
|
6
|
+
componentMapper?: ComponentMapper;
|
|
7
|
+
variables?: Record<string, string | number>;
|
|
8
|
+
} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;
|
|
9
|
+
export declare const Md: ({ children, componentMapper: componentMapperOverrides, variables, ...markdownToJsxOptionOverrides }: MdProps) => JSX.Element;
|
|
10
|
+
//# sourceMappingURL=Md.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Md.d.cts","sourceRoot":"","sources":["../../../../src/components/Md.ctsx"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,GAAG,EAAc,MAAM,OAAO,CAAC;AAGlE,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG;IAEpB,QAAQ,EAAE,MAAM,CAAC;IAMjB,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;CAC7C,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;AAEvE,eAAO,MAAM,EAAE,wGAKZ,OAAO,KAAG,GAAG,CAAC,OAchB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectVariables.d.cts","sourceRoot":"","sources":["../../../../src/helpers/injectVariables.cts"],"names":[],"mappings":"AAGA,eAAO,MAAM,eAAe,aAAc,MAAM,cAAc,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,KAAG,MAalF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/index.cts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"macros.d.cts","sourceRoot":"","sources":["../../../src/macros.cts"],"names":[],"mappings":"AAGA,eAAO,MAAM,EAAE,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,CAW9C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.cts","sourceRoot":"","sources":["../../../src/main.cts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAmBxE,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import { type AnyRecord, type ConfigReader } from 'zcb';
|
|
3
|
+
export type ComponentMapper = Record<string, ComponentType<any>>;
|
|
4
|
+
export type CreateI18nResult<U extends object> = {
|
|
5
|
+
t: (key: string, variables?: AnyRecord) => string | undefined;
|
|
6
|
+
tt: ConfigReader<U>['read'];
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../../src/types.cts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,KAAK,CAAC;AAIxD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAEjE,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;IAI/C,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,SAAS,KAAK,MAAM,GAAG,SAAS,CAAC;IAM9D,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type MarkdownToJsx from 'markdown-to-jsx';
|
|
2
|
+
import { type ComponentProps, type JSX, type ReactNode } from 'react';
|
|
3
|
+
import { type ComponentMapper } from '#types.ts';
|
|
4
|
+
export declare const I18nContext: import("react").Context<{
|
|
5
|
+
componentMapper: ComponentMapper;
|
|
6
|
+
} & Omit<Partial<{
|
|
7
|
+
createElement: (tag: Parameters<import("markdown-to-jsx").MarkdownToJSX.CreateElement>[0], props: JSX.IntrinsicAttributes, ...children: ReactNode[]) => ReactNode;
|
|
8
|
+
disableAutoLink: boolean;
|
|
9
|
+
disableParsingRawHTML: boolean;
|
|
10
|
+
enforceAtxHeadings: boolean;
|
|
11
|
+
forceBlock: boolean;
|
|
12
|
+
forceInline: boolean;
|
|
13
|
+
forceWrapper: boolean;
|
|
14
|
+
namedCodesToUnicode: {
|
|
15
|
+
[key: string]: string;
|
|
16
|
+
};
|
|
17
|
+
overrides: import("markdown-to-jsx").MarkdownToJSX.Overrides;
|
|
18
|
+
renderRule: (next: () => ReactNode, node: import("markdown-to-jsx").MarkdownToJSX.ParserResult, renderChildren: import("markdown-to-jsx").MarkdownToJSX.RuleOutput, state: import("markdown-to-jsx").MarkdownToJSX.State) => ReactNode;
|
|
19
|
+
sanitizer: (value: string, tag: import("markdown-to-jsx").MarkdownToJSX.HTMLTags, attribute: string) => string | null;
|
|
20
|
+
slugify: (input: string, defaultFn: (input: string) => string) => string;
|
|
21
|
+
wrapper: import("react").ElementType | null;
|
|
22
|
+
}> | undefined, "overrides">>;
|
|
23
|
+
export type I18nProviderProps = {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
componentMapper: ComponentMapper;
|
|
26
|
+
} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;
|
|
27
|
+
export declare const I18nProvider: ({ children, componentMapper, ...markdownToJsxOptions }: I18nProviderProps) => JSX.Element;
|
|
28
|
+
//# sourceMappingURL=I18nProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"I18nProvider.d.ts","sourceRoot":"","sources":["../../../../src/components/I18nProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,GAAG,EAAE,KAAK,SAAS,EAAiB,MAAM,OAAO,CAAC;AACrF,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,eAAO,MAAM,WAAW;qBAEH,eAAe;;+HAkBomT,GAAI;;;;;;;;;;;;;;;6BAd1oT,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,SAAS,CAAC;IACpB,eAAe,EAAE,eAAe,CAAC;CAClC,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;AAEvE,eAAO,MAAM,YAAY,2DAItB,iBAAiB,KAAG,GAAG,CAAC,OAE1B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import MarkdownToJsx from 'markdown-to-jsx';
|
|
2
|
+
import { type ComponentProps, type JSX } from 'react';
|
|
3
|
+
import { type ComponentMapper } from '#types.ts';
|
|
4
|
+
export type MdProps = {
|
|
5
|
+
children: string;
|
|
6
|
+
componentMapper?: ComponentMapper;
|
|
7
|
+
variables?: Record<string, string | number>;
|
|
8
|
+
} & Omit<ComponentProps<typeof MarkdownToJsx>['options'], 'overrides'>;
|
|
9
|
+
export declare const Md: ({ children, componentMapper: componentMapperOverrides, variables, ...markdownToJsxOptionOverrides }: MdProps) => JSX.Element;
|
|
10
|
+
//# sourceMappingURL=Md.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Md.d.ts","sourceRoot":"","sources":["../../../../src/components/Md.tsx"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,GAAG,EAAc,MAAM,OAAO,CAAC;AAGlE,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG;IAEpB,QAAQ,EAAE,MAAM,CAAC;IAMjB,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;CAC7C,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,aAAa,CAAC,CAAC,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;AAEvE,eAAO,MAAM,EAAE,wGAKZ,OAAO,KAAG,GAAG,CAAC,OAchB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectVariables.d.ts","sourceRoot":"","sources":["../../../../src/helpers/injectVariables.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,eAAe,aAAc,MAAM,cAAc,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,KAAG,MAalF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"macros.d.ts","sourceRoot":"","sources":["../../../src/macros.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,EAAE,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,CAW9C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAmBxE,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import { type AnyRecord, type ConfigReader } from 'zcb';
|
|
3
|
+
export type ComponentMapper = Record<string, ComponentType<any>>;
|
|
4
|
+
export type CreateI18nResult<U extends object> = {
|
|
5
|
+
t: (key: string, variables?: AnyRecord) => string | undefined;
|
|
6
|
+
tt: ConfigReader<U>['read'];
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,KAAK,CAAC;AAIxD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAEjE,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;IAI/C,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,SAAS,KAAK,MAAM,GAAG,SAAS,CAAC;IAM9D,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC"}
|