@next-box/i18n 0.0.8 → 0.0.10
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 +22 -4
- package/dist/cjs/macros.cjs +1 -1
- package/dist/cjs/macros.cjs.map +1 -1
- package/dist/esm/macros.mjs +1 -1
- package/dist/esm/macros.mjs.map +1 -1
- package/dist/production.analysis.txt +27 -9
- package/dist/types/cjs/constants.d.cts +2 -0
- package/dist/types/cjs/constants.d.cts.map +1 -0
- package/dist/types/cjs/helpers/loadConfig.d.cts +3 -0
- package/dist/types/cjs/helpers/loadConfig.d.cts.map +1 -0
- package/dist/types/cjs/macros.d.cts.map +1 -1
- package/dist/types/cjs/types.d.cts +3 -0
- package/dist/types/cjs/types.d.cts.map +1 -1
- package/dist/types/esm/constants.d.ts +2 -0
- package/dist/types/esm/constants.d.ts.map +1 -0
- package/dist/types/esm/helpers/loadConfig.d.ts +3 -0
- package/dist/types/esm/helpers/loadConfig.d.ts.map +1 -0
- package/dist/types/esm/macros.d.ts.map +1 -1
- package/dist/types/esm/types.d.ts +3 -0
- package/dist/types/esm/types.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/constants.ts +1 -0
- package/src/helpers/loadConfig.ts +20 -0
- package/src/macros.test.ts +7 -1
- package/src/macros.ts +12 -6
- package/src/types.ts +9 -0
package/README.md
CHANGED
|
@@ -61,17 +61,17 @@ export const content = {
|
|
|
61
61
|
// ./.env.d.ts
|
|
62
62
|
declare namespace NodeJS {
|
|
63
63
|
interface ProcessEnv {
|
|
64
|
-
|
|
64
|
+
NEXT_PUBLIC_LANGUAGE_CODE: 'en' | 'fr';
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
```ts
|
|
70
70
|
// ./content/index.ts
|
|
71
|
-
const {
|
|
71
|
+
const { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;
|
|
72
72
|
let language: typeof import('./en/index.ts') | typeof import('./fr/index.ts');
|
|
73
73
|
|
|
74
|
-
switch (
|
|
74
|
+
switch (NEXT_PUBLIC_LANGUAGE_CODE) {
|
|
75
75
|
case 'en': {
|
|
76
76
|
language = await import('./en/index.ts');
|
|
77
77
|
break;
|
|
@@ -150,13 +150,31 @@ import { md } from '@next-box/i18n/macros' with { type: 'macro' }; // This is im
|
|
|
150
150
|
|
|
151
151
|
const content = {
|
|
152
152
|
en: {
|
|
153
|
-
contactUs: md('./markdown/contactUs.md'),
|
|
153
|
+
contactUs: md('./markdown/contactUs.md'),
|
|
154
154
|
},
|
|
155
155
|
} as const;
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
> 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/).
|
|
159
159
|
|
|
160
|
+
By default, the path you pass into the `md` macro is relative to the current working directory. You can change this via the `.i18n-macro.config.json` config file. Create one in your project root and add a `markdownDir` property with a relative path from the current working directory to the directory where your markdown files live.
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"markdownDir": "./src/content/markdown"
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
The `markdownDir` path supports a template variable `NEXT_PUBLIC_LANGUAGE_CODE`, which is populated by setting an environment variable with the same name. You can use this if you support multiple languages. Please note, the multi-language content directory structure must be the same for this approach to work correctly.
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"markdownDir": "./src/content/{{NEXT_PUBLIC_LANGUAGE_CODE}}/markdown"
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### Markdown to JSX
|
|
177
|
+
|
|
160
178
|
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`.
|
|
161
179
|
|
|
162
180
|
> Under the hood, the library uses the `markdown-to-jsx` npm package to transform markdown into JSX.
|
package/dist/cjs/macros.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("
|
|
1
|
+
"use strict";var e=require("node:fs"),r=require("node:path");const{NEXT_PUBLIC_LANGUAGE_CODE:n}=process.env;exports.md=o=>{if(!o)throw new Error("md expected a path, but received none");let c=process.cwd(),{markdownDir:t}=(()=>{let n={};try{n=JSON.parse(e.readFileSync(r.resolve(process.cwd(),".i18n-macro.config.json"),{encoding:"utf8"}))}catch{}return n})();t&&(n&&(t=t.replaceAll("{{NEXT_PUBLIC_LANGUAGE_CODE}}",n)),c=r.resolve(c,t));const s=r.resolve(c,o);return e.readFileSync(s,{encoding:"utf8"}).trim()};
|
|
2
2
|
//# sourceMappingURL=macros.cjs.map
|
package/dist/cjs/macros.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"macros.cjs","sources":["../i18n/src//macros.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"macros.cjs","sources":["../i18n/src//constants.ts","../i18n/src//macros.ts","../i18n/src//helpers/loadConfig.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\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","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\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(\n readFileSync(resolve(process.cwd(), CONFIG_FILENAME), { encoding: 'utf8' }),\n ) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","path","Error","dir","cwd","markdownDir","config","JSON","parse","readFileSync","resolve","encoding","loadConfig","replaceAll","fullPath","trim"],"mappings":"6DAAO,MCIDA,0BAAEA,GAA8BC,QAAQC,eAETC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAMJ,QAAQK,OACdC,YAAEA,GCPkB,MACxB,IAAIC,EAAgC,CAAC,EAErC,IAGEA,EAASC,KAAKC,MACZC,EAAAA,aAAaC,UAAQX,QAAQK,MFZJ,2BEY6B,CAAEO,SAAU,SAEtE,CAAE,MAEF,CAEA,OAAOL,GDNeM,GAElBP,IACEP,IACFO,EAAcA,EAAYQ,WAAW,gCAAiCf,IAGxEK,EAAMO,EAAAA,QAAQP,EAAKE,IAGrB,MAAMS,EAAWJ,EAAAA,QAAQP,EAAKF,GAK9B,OAAOQ,EAAAA,aAAaK,EAAU,CAAEH,SAAU,SAAUI"}
|
package/dist/esm/macros.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{readFileSync as e}from"node:fs";import{resolve as r}from"node:path";const{NEXT_PUBLIC_LANGUAGE_CODE:o}=process.env,t=t=>{if(!t)throw new Error("md expected a path, but received none");let n=process.cwd(),{markdownDir:c}=(()=>{let o={};try{o=JSON.parse(e(r(process.cwd(),".i18n-macro.config.json"),{encoding:"utf8"}))}catch{}return o})();c&&(o&&(c=c.replaceAll("{{NEXT_PUBLIC_LANGUAGE_CODE}}",o)),n=r(n,c));const p=r(n,t);return e(p,{encoding:"utf8"}).trim()};export{t as md};
|
|
2
2
|
//# sourceMappingURL=macros.mjs.map
|
package/dist/esm/macros.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"macros.mjs","sources":["../i18n/src//macros.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"macros.mjs","sources":["../i18n/src//constants.ts","../i18n/src//macros.ts","../i18n/src//helpers/loadConfig.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\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","import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\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(\n readFileSync(resolve(process.cwd(), CONFIG_FILENAME), { encoding: 'utf8' }),\n ) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","md","path","Error","dir","cwd","markdownDir","config","JSON","parse","readFileSync","resolve","encoding","loadConfig","replaceAll","fullPath","trim"],"mappings":"2EAAO,MCIDA,0BAAEA,GAA8BC,QAAQC,IAEjCC,EAAwBC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAML,QAAQM,OACdC,YAAEA,GCPkB,MACxB,IAAIC,EAAgC,CAAC,EAErC,IAGEA,EAASC,KAAKC,MACZC,EAAaC,EAAQZ,QAAQM,MFZJ,2BEY6B,CAAEO,SAAU,SAEtE,CAAE,MAEF,CAEA,OAAOL,GDNeM,GAElBP,IACER,IACFQ,EAAcA,EAAYQ,WAAW,gCAAiChB,IAGxEM,EAAMO,EAAQP,EAAKE,IAGrB,MAAMS,EAAWJ,EAAQP,EAAKF,GAK9B,OAAOQ,EAAaK,EAAU,CAAEH,SAAU,SAAUI"}
|
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
-----------------------------
|
|
2
2
|
Rollup File Analysis
|
|
3
3
|
-----------------------------
|
|
4
|
-
bundle size:
|
|
5
|
-
original size:
|
|
6
|
-
code reduction:
|
|
7
|
-
module count:
|
|
4
|
+
bundle size: 1.3 KB
|
|
5
|
+
original size: 1.601 KB
|
|
6
|
+
code reduction: 18.8 %
|
|
7
|
+
module count: 3
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
█████████████████████████████████░░░░░░░░░░░░░░░░░
|
|
10
10
|
file: /src/macros.ts
|
|
11
|
-
bundle space:
|
|
12
|
-
rendered size:
|
|
13
|
-
original size:
|
|
14
|
-
code reduction:
|
|
11
|
+
bundle space: 66.77 %
|
|
12
|
+
rendered size: 868 Bytes
|
|
13
|
+
original size: 952 Bytes
|
|
14
|
+
code reduction: 8.82 %
|
|
15
15
|
dependents: 0
|
|
16
16
|
|
|
17
|
+
██████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
18
|
+
file: /src/helpers/loadConfig.ts
|
|
19
|
+
bundle space: 29.38 %
|
|
20
|
+
rendered size: 382 Bytes
|
|
21
|
+
original size: 591 Bytes
|
|
22
|
+
code reduction: 35.36 %
|
|
23
|
+
dependents: 1
|
|
24
|
+
- /src/macros.ts
|
|
25
|
+
|
|
26
|
+
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
27
|
+
file: /src/constants.ts
|
|
28
|
+
bundle space: 3.85 %
|
|
29
|
+
rendered size: 50 Bytes
|
|
30
|
+
original size: 58 Bytes
|
|
31
|
+
code reduction: 13.79 %
|
|
32
|
+
dependents: 1
|
|
33
|
+
- /src/helpers/loadConfig.ts
|
|
34
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../../../src/constants.cts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,4BAA4B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadConfig.d.cts","sourceRoot":"","sources":["../../../../src/helpers/loadConfig.cts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAE9C,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,YAAY,CAcjD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"macros.d.cts","sourceRoot":"","sources":["../../../src/macros.cts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"macros.d.cts","sourceRoot":"","sources":["../../../src/macros.cts"],"names":[],"mappings":"AAMA,eAAO,MAAM,EAAE,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,CAsB9C,CAAC"}
|
|
@@ -1 +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"}
|
|
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;AAEF,MAAM,MAAM,YAAY,GAAG;IAMzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,4BAA4B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../../../src/helpers/loadConfig.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAE9C,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,YAAY,CAcjD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"macros.d.ts","sourceRoot":"","sources":["../../../src/macros.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"macros.d.ts","sourceRoot":"","sources":["../../../src/macros.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,EAAE,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,CAsB9C,CAAC"}
|
|
@@ -1 +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"}
|
|
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;AAEF,MAAM,MAAM,YAAY,GAAG;IAMzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC"}
|