@next-box/i18n 0.1.1 → 0.1.2
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/README.md +3 -1
- package/dist/cjs/client/client.cjs +3 -0
- package/dist/cjs/client/client.cjs.map +1 -0
- package/dist/cjs/client/components/I18nProvider.cjs +3 -0
- package/dist/cjs/client/components/I18nProvider.cjs.map +1 -0
- package/dist/cjs/client/components/Md.cjs +3 -0
- package/dist/cjs/client/components/Md.cjs.map +1 -0
- package/dist/cjs/client/helpers/injectVariables.cjs +2 -0
- package/dist/cjs/client/helpers/injectVariables.cjs.map +1 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/macros/constants.cjs +2 -0
- package/dist/cjs/macros/constants.cjs.map +1 -0
- package/dist/cjs/macros/helpers/loadConfig.cjs +2 -0
- package/dist/cjs/macros/helpers/loadConfig.cjs.map +1 -0
- package/dist/cjs/macros/macros.cjs +2 -0
- package/dist/cjs/macros/macros.cjs.map +1 -0
- package/dist/cjs/main.cjs +2 -0
- package/dist/cjs/main.cjs.map +1 -0
- package/dist/esm/client/client.mjs +3 -0
- package/dist/esm/client/client.mjs.map +1 -0
- package/dist/esm/client/components/I18nProvider.mjs +3 -0
- package/dist/esm/client/components/I18nProvider.mjs.map +1 -0
- package/dist/esm/client/components/Md.mjs +3 -0
- package/dist/esm/client/components/Md.mjs.map +1 -0
- package/dist/esm/client/helpers/injectVariables.mjs +2 -0
- package/dist/esm/client/helpers/injectVariables.mjs.map +1 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/macros/constants.mjs +2 -0
- package/dist/esm/macros/constants.mjs.map +1 -0
- package/dist/esm/macros/helpers/loadConfig.mjs +2 -0
- package/dist/esm/macros/helpers/loadConfig.mjs.map +1 -0
- package/dist/esm/macros/macros.mjs +2 -0
- package/dist/esm/macros/macros.mjs.map +1 -0
- package/dist/esm/main.mjs +2 -0
- package/dist/esm/main.mjs.map +1 -0
- package/dist/production.analysis.txt +10 -10
- package/dist/types/cjs/client.d.cts.map +1 -1
- package/dist/types/cjs/components/I18nProvider.d.cts.map +1 -1
- package/dist/types/cjs/components/Md.d.cts.map +1 -1
- package/dist/types/cjs/helpers/injectVariables.d.cts.map +1 -1
- package/dist/types/cjs/main.d.cts.map +1 -1
- package/dist/types/esm/client.d.ts.map +1 -1
- package/dist/types/esm/components/I18nProvider.d.ts.map +1 -1
- package/dist/types/esm/components/Md.d.ts.map +1 -1
- package/dist/types/esm/helpers/injectVariables.d.ts.map +1 -1
- package/dist/types/esm/main.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +13 -12
- package/rollup.config.mjs +2 -2
- package/src/client.ts +2 -0
- package/src/components/I18nProvider.tsx +2 -0
- package/src/components/Md.test.tsx +34 -32
- package/src/components/Md.tsx +2 -0
- package/src/helpers/injectVariables.test.ts +37 -0
- package/src/helpers/injectVariables.ts +3 -4
- package/src/macros.test.ts +34 -21
- package/src/main.test.ts +98 -62
- package/src/main.ts +0 -2
- package/src/types.ts +1 -1
- package/tsconfig.json +3 -0
- package/dist/cjs/client.cjs +0 -2
- package/dist/cjs/client.cjs.map +0 -1
- package/dist/cjs/macros.cjs +0 -2
- package/dist/cjs/macros.cjs.map +0 -1
- package/dist/esm/client.mjs +0 -2
- package/dist/esm/client.mjs.map +0 -1
- package/dist/esm/macros.mjs +0 -2
- package/dist/esm/macros.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-box/i18n",
|
|
3
3
|
"description": "A type-safe content resolver with content path auto-complete and value preview.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"author": "miami-man",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/badbatch/next-box",
|
|
@@ -52,23 +52,24 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"callsites": "^4.2.0",
|
|
55
|
-
"markdown-to-jsx": "^9.10.
|
|
56
|
-
"type-fest": "^5.
|
|
57
|
-
"zcb": "^0.3.
|
|
55
|
+
"markdown-to-jsx": "^9.10.4",
|
|
56
|
+
"type-fest": "^5.10.0",
|
|
57
|
+
"zcb": "^0.3.10"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": "<20",
|
|
61
61
|
"react-dom": "<20"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@jest/globals": "^30.
|
|
65
|
-
"@testing-library/react": "^16.3.
|
|
66
|
-
"@types/
|
|
67
|
-
"@types/react
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"react
|
|
64
|
+
"@jest/globals": "^30.5.2",
|
|
65
|
+
"@testing-library/react": "^16.3.3",
|
|
66
|
+
"@types/node": "^26.6.6",
|
|
67
|
+
"@types/react": "^19.3.0",
|
|
68
|
+
"@types/react-dom": "^19.3.0",
|
|
69
|
+
"cts-types": "^0.0.17",
|
|
70
|
+
"del-cli": "^8.0.1",
|
|
71
|
+
"react": "^19.3.1",
|
|
72
|
+
"react-dom": "^19.3.1"
|
|
72
73
|
},
|
|
73
74
|
"scripts": {
|
|
74
75
|
"build": "pnpm run clean:dist && pnpm run compile",
|
package/rollup.config.mjs
CHANGED
|
@@ -11,7 +11,7 @@ export default [
|
|
|
11
11
|
input: input.replace('index', 'client'),
|
|
12
12
|
output: {
|
|
13
13
|
...output,
|
|
14
|
-
|
|
14
|
+
dir: `${output.dir}/client`,
|
|
15
15
|
},
|
|
16
16
|
},
|
|
17
17
|
{
|
|
@@ -19,7 +19,7 @@ export default [
|
|
|
19
19
|
input: input.replace('index', 'macros'),
|
|
20
20
|
output: {
|
|
21
21
|
...output,
|
|
22
|
-
|
|
22
|
+
dir: `${output.dir}/macros`,
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
25
|
];
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/** @jest-environment @repodog/jest-config/FailOnJsdomConsoleEnvironment.mjs */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from '@jest/globals';
|
|
4
4
|
import { render } from '@testing-library/react';
|
|
5
5
|
import { type MarkdownToJSX } from 'markdown-to-jsx';
|
|
6
6
|
import { type ReactNode } from 'react';
|
|
@@ -18,14 +18,17 @@ const componentMapper: MarkdownToJSX.Overrides = {
|
|
|
18
18
|
p: ({ children }: ComponentProps) => <p data-disaplay-name="p">{children}</p>,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
describe('
|
|
22
|
-
it('
|
|
23
|
-
|
|
21
|
+
describe('Md', () => {
|
|
22
|
+
it('When markdown contains mapped components, then it renders the mapped React components', () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
const wrapper = ({ children }: ComponentProps) => (
|
|
24
25
|
<I18nProvider componentMapper={componentMapper}>{children}</I18nProvider>
|
|
25
26
|
);
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
// Act
|
|
29
|
+
const { container } = render(<Md>{'<CustomComponent><p>content</p></CustomComponent>'}</Md>, { wrapper });
|
|
28
30
|
|
|
31
|
+
// Assert
|
|
29
32
|
expect(container).toMatchInlineSnapshot(`
|
|
30
33
|
<div>
|
|
31
34
|
<div
|
|
@@ -41,34 +44,33 @@ describe('MarkdownToJsx', () => {
|
|
|
41
44
|
`);
|
|
42
45
|
});
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
it('When markdown contains template variables, then it injects them before rendering mapped components', () => {
|
|
48
|
+
// Arrange
|
|
49
|
+
const wrapper = ({ children }: ComponentProps) => (
|
|
50
|
+
<I18nProvider componentMapper={componentMapper}>{children}</I18nProvider>
|
|
51
|
+
);
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
</
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
);
|
|
53
|
+
// Act
|
|
54
|
+
const { container } = render(
|
|
55
|
+
<Md variables={{ adjective: 'great' }}>
|
|
56
|
+
{"<CustomComponent><p>{'Have a {{adjective}} day'}</p></CustomComponent>"}
|
|
57
|
+
</Md>,
|
|
58
|
+
{ wrapper },
|
|
59
|
+
);
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
// Assert
|
|
62
|
+
expect(container).toMatchInlineSnapshot(`
|
|
63
|
+
<div>
|
|
64
|
+
<div
|
|
65
|
+
data-display-name="CustomComponent"
|
|
66
|
+
>
|
|
67
|
+
<p
|
|
68
|
+
data-disaplay-name="p"
|
|
63
69
|
>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
>
|
|
67
|
-
{'Have a great day'}
|
|
68
|
-
</p>
|
|
69
|
-
</div>
|
|
70
|
+
{'Have a great day'}
|
|
71
|
+
</p>
|
|
70
72
|
</div>
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
</div>
|
|
74
|
+
`);
|
|
73
75
|
});
|
|
74
76
|
});
|
package/src/components/Md.tsx
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
2
|
+
import { injectVariables } from './injectVariables.ts';
|
|
3
|
+
|
|
4
|
+
describe('injectVariables', () => {
|
|
5
|
+
it('When a variable is zero, then it interpolates the numeric value', () => {
|
|
6
|
+
// Arrange
|
|
7
|
+
const markdown = 'There are {{count}} items';
|
|
8
|
+
|
|
9
|
+
// Act
|
|
10
|
+
const result = injectVariables(markdown, { count: 0 });
|
|
11
|
+
|
|
12
|
+
// Assert
|
|
13
|
+
expect(result).toBe('There are 0 items');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('When a variable is an empty string, then it interpolates the empty value', () => {
|
|
17
|
+
// Arrange
|
|
18
|
+
const markdown = 'Hello{{name}}!';
|
|
19
|
+
|
|
20
|
+
// Act
|
|
21
|
+
const result = injectVariables(markdown, { name: '' });
|
|
22
|
+
|
|
23
|
+
// Assert
|
|
24
|
+
expect(result).toBe('Hello!');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('When a variable is missing, then it throws an error containing the variable name', () => {
|
|
28
|
+
// Arrange
|
|
29
|
+
const markdown = 'Hello {{name}}!';
|
|
30
|
+
|
|
31
|
+
// Act
|
|
32
|
+
const injectMissingVariable = () => injectVariables(markdown, {});
|
|
33
|
+
|
|
34
|
+
// Assert
|
|
35
|
+
expect(injectMissingVariable).toThrow("Markdown contains un-replaced variable 'name'");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -3,13 +3,12 @@ const extractVarName = /{{ *([^ }]*) *}}/;
|
|
|
3
3
|
|
|
4
4
|
export const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>
|
|
5
5
|
variables
|
|
6
|
-
?
|
|
7
|
-
markdown.replaceAll(extractTemplateVariables, (match: string) => {
|
|
6
|
+
? markdown.replaceAll(extractTemplateVariables, (match: string) => {
|
|
8
7
|
const varName = match.replace(extractVarName, '$1');
|
|
9
8
|
const replacementValue = variables[varName];
|
|
10
9
|
|
|
11
|
-
if (replacementValue) {
|
|
12
|
-
return replacementValue;
|
|
10
|
+
if (replacementValue !== undefined) {
|
|
11
|
+
return String(replacementValue);
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
throw new Error(`Markdown contains un-replaced variable '${varName}'`);
|
package/src/macros.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jest } from '@jest/globals';
|
|
1
|
+
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
2
|
|
|
3
3
|
jest.unstable_mockModule('#helpers/loadConfig.ts', () => ({
|
|
4
4
|
loadConfig: jest.fn().mockReturnValue({ markdownDir: './packages/i18n/src' }),
|
|
@@ -7,35 +7,48 @@ jest.unstable_mockModule('#helpers/loadConfig.ts', () => ({
|
|
|
7
7
|
const { md } = await import('#macros.ts');
|
|
8
8
|
|
|
9
9
|
describe('md', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
it('When a valid markdown path is provided, then it returns the file contents', () => {
|
|
11
|
+
// Arrange
|
|
12
|
+
const path = './__testUtils__/content.md';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
// Act
|
|
15
|
+
const result = md(path);
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
// Assert
|
|
18
|
+
expect(result).toMatchInlineSnapshot(`
|
|
19
|
+
"# Page title
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
## Section title
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
### Section subtitle
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
> Inset text
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
This is a paragraph.
|
|
28
|
+
|
|
29
|
+
For more info, see [this link](http://localhost)"
|
|
30
|
+
`);
|
|
28
31
|
});
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
it('When no markdown path is provided, then it throws an error describing the missing path', () => {
|
|
34
|
+
// Arrange
|
|
35
|
+
const path = '';
|
|
36
|
+
|
|
37
|
+
// Act
|
|
38
|
+
const readMarkdown = () => md(path);
|
|
39
|
+
|
|
40
|
+
// Assert
|
|
41
|
+
expect(readMarkdown).toThrow('md expected a path, but received none');
|
|
34
42
|
});
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
it('When the markdown path does not resolve to a file, then it throws a file system error', () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
const path = './__testUtils__/no-content.md';
|
|
47
|
+
|
|
48
|
+
// Act
|
|
49
|
+
const readMarkdown = () => md(path);
|
|
50
|
+
|
|
51
|
+
// Assert
|
|
52
|
+
expect(readMarkdown).toThrow();
|
|
40
53
|
});
|
|
41
54
|
});
|
package/src/main.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
1
2
|
import { createI18n } from './main.ts';
|
|
2
3
|
|
|
3
4
|
const content = {
|
|
@@ -19,93 +20,128 @@ const content = {
|
|
|
19
20
|
} as const;
|
|
20
21
|
|
|
21
22
|
describe('createI18n', () => {
|
|
22
|
-
it('
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
it('When content is provided, then it returns typed and untyped resolver functions', () => {
|
|
24
|
+
// Arrange
|
|
25
|
+
|
|
26
|
+
// Act
|
|
27
|
+
const result = createI18n(content);
|
|
28
|
+
|
|
29
|
+
// Assert
|
|
30
|
+
expect(result).toEqual({ t: expect.any(Function), tt: expect.any(Function) });
|
|
29
31
|
});
|
|
30
32
|
});
|
|
31
33
|
|
|
32
34
|
describe('tt', () => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
it('When a path does not resolve to a value, then it throws an error containing the path', () => {
|
|
36
|
+
// Arrange
|
|
37
|
+
const { tt } = createI18n(content);
|
|
38
|
+
|
|
39
|
+
// Act
|
|
40
|
+
// @ts-expect-error Intentionally pass an invalid path to test runtime behavior.
|
|
41
|
+
const resolveInvalidPath = () => tt('zebra');
|
|
42
|
+
|
|
43
|
+
// Assert
|
|
44
|
+
expect(resolveInvalidPath).toThrow('Path "zebra" did not resolve to a value');
|
|
39
45
|
});
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
it('When a path resolves to a top-level value, then it returns that value', () => {
|
|
48
|
+
// Arrange
|
|
49
|
+
const { tt } = createI18n(content);
|
|
50
|
+
|
|
51
|
+
// Act
|
|
52
|
+
const result = tt('alpha');
|
|
53
|
+
|
|
54
|
+
// Assert
|
|
55
|
+
expect(result).toBe('alpha');
|
|
46
56
|
});
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
it('When a path resolves through nested objects and arrays, then it returns the nested value', () => {
|
|
59
|
+
// Arrange
|
|
60
|
+
const { tt } = createI18n(content);
|
|
61
|
+
|
|
62
|
+
// Act
|
|
63
|
+
const result = tt('echo.golf.0.hotel.india');
|
|
64
|
+
|
|
65
|
+
// Assert
|
|
66
|
+
expect(result).toBe('india');
|
|
53
67
|
});
|
|
54
68
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
it('When variables are provided for a template, then it returns the interpolated value', () => {
|
|
70
|
+
// Arrange
|
|
71
|
+
const { tt } = createI18n(content);
|
|
72
|
+
|
|
73
|
+
// Act
|
|
74
|
+
const result = tt('charlie', { adjective: 'good' });
|
|
75
|
+
|
|
76
|
+
// Assert
|
|
77
|
+
expect(result).toBe('charlie is a good boy');
|
|
60
78
|
});
|
|
61
79
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
80
|
+
it('When multiple variables are provided, then it interpolates each variable', () => {
|
|
81
|
+
// Arrange
|
|
82
|
+
const { tt } = createI18n(content);
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
// Act
|
|
85
|
+
const result = tt('echo.golf.0.hotel.julia', { activity: 'gymnast', adjective: 'great' });
|
|
86
|
+
|
|
87
|
+
// Assert
|
|
88
|
+
expect(result).toBe('julia is a great gymnast');
|
|
70
89
|
});
|
|
71
90
|
});
|
|
72
91
|
|
|
73
92
|
describe('t', () => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
93
|
+
it('When a path does not resolve to a value, then it returns undefined', () => {
|
|
94
|
+
// Arrange
|
|
95
|
+
const { t } = createI18n(content);
|
|
96
|
+
|
|
97
|
+
// Act
|
|
98
|
+
const result = t('zebra');
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(result).toBeUndefined();
|
|
79
102
|
});
|
|
80
103
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
104
|
+
it('When a path resolves to a top-level value, then it returns that value', () => {
|
|
105
|
+
// Arrange
|
|
106
|
+
const { t } = createI18n(content);
|
|
107
|
+
|
|
108
|
+
// Act
|
|
109
|
+
const result = t('alpha');
|
|
110
|
+
|
|
111
|
+
// Assert
|
|
112
|
+
expect(result).toBe('alpha');
|
|
86
113
|
});
|
|
87
114
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
it('When a path resolves through nested objects and arrays, then it returns the nested value', () => {
|
|
116
|
+
// Arrange
|
|
117
|
+
const { t } = createI18n(content);
|
|
118
|
+
|
|
119
|
+
// Act
|
|
120
|
+
const result = t('echo.golf.0.hotel.india');
|
|
121
|
+
|
|
122
|
+
// Assert
|
|
123
|
+
expect(result).toBe('india');
|
|
93
124
|
});
|
|
94
125
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
126
|
+
it('When variables are provided for a template, then it returns the interpolated value', () => {
|
|
127
|
+
// Arrange
|
|
128
|
+
const { t } = createI18n(content);
|
|
129
|
+
|
|
130
|
+
// Act
|
|
131
|
+
const result = t('charlie', { adjective: 'good' });
|
|
132
|
+
|
|
133
|
+
// Assert
|
|
134
|
+
expect(result).toBe('charlie is a good boy');
|
|
100
135
|
});
|
|
101
136
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
137
|
+
it('When multiple variables are provided, then it interpolates each variable', () => {
|
|
138
|
+
// Arrange
|
|
139
|
+
const { t } = createI18n(content);
|
|
140
|
+
|
|
141
|
+
// Act
|
|
142
|
+
const result = t('echo.golf.0.hotel.julia', { activity: 'gymnast', adjective: 'great' });
|
|
105
143
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
);
|
|
109
|
-
});
|
|
144
|
+
// Assert
|
|
145
|
+
expect(result).toBe('julia is a great gymnast');
|
|
110
146
|
});
|
|
111
147
|
});
|
package/src/main.ts
CHANGED
|
@@ -12,8 +12,6 @@ export const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {
|
|
|
12
12
|
tt: <P extends Path<U>>(path: P, variables?: AnyRecord): Get<U, P> => {
|
|
13
13
|
const result = reader.read(path, variables);
|
|
14
14
|
|
|
15
|
-
// This can resolve to a falsy value at runtime
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
17
15
|
if (result === undefined) {
|
|
18
16
|
throw new Error(`Path "${String(path)}" did not resolve to a value`);
|
|
19
17
|
}
|
package/src/types.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type CreateI18nResult<U extends object> = {
|
|
|
8
8
|
// A typed content lookup. Function takes a string that is type-checked
|
|
9
9
|
// against all the possible paths in the i18n object passed into
|
|
10
10
|
// createI18n. The value that path resolves to can be previewed through
|
|
11
|
-
// IDE's inbuilt
|
|
11
|
+
// IDE's inbuilt TypeScript type preview. If the function resolves no
|
|
12
12
|
// value, then it throws an error.
|
|
13
13
|
tt: ConfigReader<U>['read'];
|
|
14
14
|
};
|
package/tsconfig.json
CHANGED
package/dist/cjs/client.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var e=require("react/jsx-runtime"),r=require("react"),n=require("markdown-to-jsx");const o=r.createContext({componentMapper:{}}),t=/{{[^\s{}]+}}/g,p=/{{ *([^ }]*) *}}/,a=(e,r)=>r?e.replaceAll(t,e=>{const n=e.replace(p,"$1"),o=r[n];if(o)return o;throw new Error(`Markdown contains un-replaced variable '${n}'`)}):e;exports.I18nContext=o,exports.I18nProvider=({children:r,componentMapper:n,...t})=>e.jsx(o,{value:{componentMapper:n,...t},children:r}),exports.Md=({children:t,componentMapper:p,variables:c,...s})=>{const{componentMapper:i,...l}=r.use(o);return e.jsx(n,{options:{overrides:{...i,...p},...l,...s},children:a(t,c)})};
|
|
2
|
-
//# sourceMappingURL=client.cjs.map
|
package/dist/cjs/client.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.cjs","sources":["../../src/components/I18nProvider.tsx","../../src/helpers/injectVariables.ts","../../src/components/Md.tsx"],"sourcesContent":["import { type MarkdownToJSX } from 'markdown-to-jsx';\nimport { type FC, type ReactNode, createContext } from 'react';\n\nexport const I18nContext = createContext<\n {\n componentMapper: MarkdownToJSX.Overrides;\n } & Omit<MarkdownToJSX.Options, 'overrides'>\n>({\n componentMapper: {},\n});\n\nexport type I18nProviderProps = {\n children: ReactNode;\n componentMapper: MarkdownToJSX.Overrides;\n} & Omit<MarkdownToJSX.Options, 'overrides'>;\n\nexport const I18nProvider: FC<I18nProviderProps> = ({ children, componentMapper, ...markdownToJsxOptions }) => {\n return <I18nContext value={{ componentMapper, ...markdownToJsxOptions }}>{children}</I18nContext>;\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, { type MarkdownToJSX } from 'markdown-to-jsx';\nimport { type FC, use } from 'react';\nimport { I18nContext } from '#components/I18nProvider.tsx';\nimport { injectVariables } from '#helpers/injectVariables.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?: MarkdownToJSX.Overrides;\n // An object of key/value pairs for content with template variables.\n variables?: Record<string, string | number>;\n} & Omit<MarkdownToJSX.Options, 'overrides'>;\n\nexport const Md: FC<MdProps> = ({\n children,\n componentMapper: componentMapperOverrides,\n variables,\n ...markdownToJsxOptionOverrides\n}) => {\n const { componentMapper, ...markdownToJsxOptions } = use(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"],"names":["I18nContext","createContext","componentMapper","extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","Error","children","markdownToJsxOptions","_jsx","value","componentMapperOverrides","markdownToJsxOptionOverrides","use","MarkdownToJsx","options","overrides"],"mappings":"gGAGO,MAAMA,EAAcC,EAAAA,cAIzB,CACAC,gBAAiB,CAAA,ICRbC,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EAEID,EAASE,WAAWL,EAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,GAAIE,EACF,OAAOA,EAGT,MAAM,IAAIC,MAAM,2CAA2CH,QAE7DJ,6CDA6C,EAAGQ,WAAUZ,qBAAoBa,KAC3EC,EAAAA,IAAChB,EAAAA,CAAYiB,MAAO,CAAEf,qBAAoBa,GAAyBD,SAAAA,eEC7C,EAC7BA,WACAZ,gBAAiBgB,EACjBX,eACGY,MAEH,MAAMjB,gBAAEA,KAAoBa,GAAyBK,EAAAA,IAAIpB,GAEzD,OACEgB,EAAAA,IAACK,EAAAA,CACCC,QAAS,CACPC,UAAW,IAAKrB,KAAoBgB,MACjCH,KACAI,GAGJd,SAAAA,EAAgBS,EAAUP"}
|
package/dist/cjs/macros.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var e=require("node:fs"),r=require("node:path");const n=process.cwd(),{NEXT_PUBLIC_LANGUAGE_CODE:o}=process.env;exports.md=c=>{if(!c)throw new Error("md expected a path, but received none");let t=process.cwd(),{markdownDir:s}=(()=>{let o={};try{o=JSON.parse(e.readFileSync(r.resolve(n,".i18n-macro.config.json"),{encoding:"utf8"}))}catch{}return o})();s&&(o&&(s=s.replaceAll("{{NEXT_PUBLIC_LANGUAGE_CODE}}",()=>o)),t=r.resolve(t,s));const d=r.resolve(t,c);return e.readFileSync(d,{encoding:"utf8"}).trim()};
|
|
2
|
-
//# sourceMappingURL=macros.cjs.map
|
package/dist/cjs/macros.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"macros.cjs","sources":["../../src/constants.ts","../../src/helpers/loadConfig.ts","../../src/macros.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, 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":["cwd","process","NEXT_PUBLIC_LANGUAGE_CODE","env","path","Error","dir","markdownDir","config","JSON","parse","readFileSync","resolve","encoding","loadConfig","replaceAll","fullPath","trim"],"mappings":"6DAAO,MCKDA,EAAMC,QAAQD,OCDdE,0BAAEA,GAA8BD,QAAQE,eAETC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAML,QAAQD,OACdO,YAAEA,GDLkB,MACxB,IAAIC,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAAA,aAAaC,EAAAA,QAAQZ,EDbd,2BCaqC,CAAEa,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAOL,GCNeM,GAElBP,IACEL,IACFK,EAAcA,EAAYQ,WAAW,gCAAiC,IAAMb,IAG9EI,EAAMM,EAAAA,QAAQN,EAAKC,IAGrB,MAAMS,EAAWJ,EAAAA,QAAQN,EAAKF,GAK9B,OAAOO,EAAAA,aAAaK,EAAU,CAAEH,SAAU,SAAUI"}
|
package/dist/esm/client.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{jsx as r}from"react/jsx-runtime";import{createContext as e,use as o}from"react";import n from"markdown-to-jsx";const p=e({componentMapper:{}}),t=({children:e,componentMapper:o,...n})=>r(p,{value:{componentMapper:o,...n},children:e}),a=/{{[^\s{}]+}}/g,c=/{{ *([^ }]*) *}}/,i=(r,e)=>e?r.replaceAll(a,r=>{const o=r.replace(c,"$1"),n=e[o];if(n)return n;throw new Error(`Markdown contains un-replaced variable '${o}'`)}):r,m=({children:e,componentMapper:t,variables:a,...c})=>{const{componentMapper:m,...l}=o(p);return r(n,{options:{overrides:{...m,...t},...l,...c},children:i(e,a)})};export{p as I18nContext,t as I18nProvider,m as Md};
|
|
2
|
-
//# sourceMappingURL=client.mjs.map
|
package/dist/esm/client.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.mjs","sources":["../../src/components/I18nProvider.tsx","../../src/helpers/injectVariables.ts","../../src/components/Md.tsx"],"sourcesContent":["import { type MarkdownToJSX } from 'markdown-to-jsx';\nimport { type FC, type ReactNode, createContext } from 'react';\n\nexport const I18nContext = createContext<\n {\n componentMapper: MarkdownToJSX.Overrides;\n } & Omit<MarkdownToJSX.Options, 'overrides'>\n>({\n componentMapper: {},\n});\n\nexport type I18nProviderProps = {\n children: ReactNode;\n componentMapper: MarkdownToJSX.Overrides;\n} & Omit<MarkdownToJSX.Options, 'overrides'>;\n\nexport const I18nProvider: FC<I18nProviderProps> = ({ children, componentMapper, ...markdownToJsxOptions }) => {\n return <I18nContext value={{ componentMapper, ...markdownToJsxOptions }}>{children}</I18nContext>;\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, { type MarkdownToJSX } from 'markdown-to-jsx';\nimport { type FC, use } from 'react';\nimport { I18nContext } from '#components/I18nProvider.tsx';\nimport { injectVariables } from '#helpers/injectVariables.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?: MarkdownToJSX.Overrides;\n // An object of key/value pairs for content with template variables.\n variables?: Record<string, string | number>;\n} & Omit<MarkdownToJSX.Options, 'overrides'>;\n\nexport const Md: FC<MdProps> = ({\n children,\n componentMapper: componentMapperOverrides,\n variables,\n ...markdownToJsxOptionOverrides\n}) => {\n const { componentMapper, ...markdownToJsxOptions } = use(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"],"names":["I18nContext","createContext","componentMapper","I18nProvider","children","markdownToJsxOptions","_jsx","value","extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","Error","Md","componentMapperOverrides","markdownToJsxOptionOverrides","use","MarkdownToJsx","options","overrides"],"mappings":"sHAGO,MAAMA,EAAcC,EAIzB,CACAC,gBAAiB,CAAA,IAQNC,EAAsC,EAAGC,WAAUF,qBAAoBG,KAC3EC,EAACN,EAAAA,CAAYO,MAAO,CAAEL,qBAAoBG,GAAyBD,SAAAA,ICjBtEI,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EAEID,EAASE,WAAWL,EAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,GAAIE,EACF,OAAOA,EAGT,MAAM,IAAIC,MAAM,2CAA2CH,QAE7DJ,ECEOQ,EAAkB,EAC7Bf,WACAF,gBAAiBkB,EACjBR,eACGS,MAEH,MAAMnB,gBAAEA,KAAoBG,GAAyBiB,EAAItB,GAEzD,OACEM,EAACiB,EAAAA,CACCC,QAAS,CACPC,UAAW,IAAKvB,KAAoBkB,MACjCf,KACAgB,GAGJX,SAAAA,EAAgBN,EAAUQ"}
|
package/dist/esm/macros.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{readFileSync as e}from"node:fs";import{resolve as r}from"node:path";const o=process.cwd(),{NEXT_PUBLIC_LANGUAGE_CODE:t}=process.env,n=n=>{if(!n)throw new Error("md expected a path, but received none");let c=process.cwd(),{markdownDir:p}=(()=>{let t={};try{t=JSON.parse(e(r(o,".i18n-macro.config.json"),{encoding:"utf8"}))}catch{}return t})();p&&(t&&(p=p.replaceAll("{{NEXT_PUBLIC_LANGUAGE_CODE}}",()=>t)),c=r(c,p));const s=r(c,n);return e(s,{encoding:"utf8"}).trim()};export{n as md};
|
|
2
|
-
//# sourceMappingURL=macros.mjs.map
|
package/dist/esm/macros.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"macros.mjs","sources":["../../src/constants.ts","../../src/helpers/loadConfig.ts","../../src/macros.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, 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":["cwd","process","NEXT_PUBLIC_LANGUAGE_CODE","env","md","path","Error","dir","markdownDir","config","JSON","parse","readFileSync","resolve","encoding","loadConfig","replaceAll","fullPath","trim"],"mappings":"2EAAO,MCKDA,EAAMC,QAAQD,OCDdE,0BAAEA,GAA8BD,QAAQE,IAEjCC,EAAwBC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAMN,QAAQD,OACdQ,YAAEA,GDLkB,MACxB,IAAIC,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAaC,EAAQb,EDbd,2BCaqC,CAAEc,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAOL,GCNeM,GAElBP,IACEN,IACFM,EAAcA,EAAYQ,WAAW,gCAAiC,IAAMd,IAG9EK,EAAMM,EAAQN,EAAKC,IAGrB,MAAMS,EAAWJ,EAAQN,EAAKF,GAK9B,OAAOO,EAAaK,EAAU,CAAEH,SAAU,SAAUI"}
|