@18ways/mdx-translate 0.1.0-alpha.42e08a82e9b3
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/code-samples.d.ts +2 -0
- package/dist/code-samples.d.ts.map +1 -0
- package/dist/code-samples.js +8 -0
- package/dist/code-samples.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +392 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime.d.ts +9 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +36 -0
- package/dist/runtime.js.map +1 -0
- package/index.ts +1 -0
- package/package.json +50 -0
- package/runtime.tsx +1 -0
- package/src/code-samples.ts +7 -0
- package/src/index.ts +675 -0
- package/src/react-runtime-shim.d.ts +9 -0
- package/src/runtime.tsx +61 -0
- package/tests/remark-mdx-translate.test.ts +183 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-samples.d.ts","sourceRoot":"","sources":["../src/code-samples.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,GAC9B,SAAS,MAAM,EACf,WAAW,MAAM,GAAG,IAAI,GAAG,SAAS,EACpC,UAAU,OAAO,KAChB,MAAM,GAAG,IAEX,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformCodeSample = void 0;
|
|
4
|
+
const transformCodeSample = (_source, _language, _options) => {
|
|
5
|
+
return null;
|
|
6
|
+
};
|
|
7
|
+
exports.transformCodeSample = transformCodeSample;
|
|
8
|
+
//# sourceMappingURL=code-samples.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-samples.js","sourceRoot":"","sources":["../src/code-samples.ts"],"names":[],"mappings":";;;AAAO,MAAM,mBAAmB,GAAG,CACjC,OAAe,EACf,SAAoC,EACpC,QAAiB,EACF,EAAE;IACjB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AANW,QAAA,mBAAmB,uBAM9B"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Root } from 'mdast';
|
|
2
|
+
type IncludedPath = RegExp | ((filePath: string) => boolean);
|
|
3
|
+
export interface RemarkMdxTranslateOptions {
|
|
4
|
+
include?: IncludedPath;
|
|
5
|
+
exclude?: IncludedPath;
|
|
6
|
+
attributeNames?: string[];
|
|
7
|
+
translationComponentName?: string;
|
|
8
|
+
translatedNodeComponentName?: string;
|
|
9
|
+
translateHookName?: string;
|
|
10
|
+
reactImportSource?: string;
|
|
11
|
+
runtimeImportSource?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function remarkMdxTranslate(userOptions?: RemarkMdxTranslateOptions): (tree: Root, file?: {
|
|
14
|
+
path?: string;
|
|
15
|
+
}) => void;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EAUL,MAAM,OAAO,CAAC;AAyDf,KAAK,YAAY,GAAG,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC;AAE7D,MAAM,WAAW,yBAAyB;IACxC,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,2BAA2B,CAAC,EAAE,MAAM,CAAC;IACrC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAubD,wBAAgB,kBAAkB,CAAC,WAAW,GAAE,yBAA8B,IAcpE,MAAM,IAAI,EAAE,OAAO;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,UA8I7C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.remarkMdxTranslate = remarkMdxTranslate;
|
|
4
|
+
const code_samples_1 = require("./code-samples");
|
|
5
|
+
const DEFAULT_ATTRIBUTE_NAMES = ['alt', 'title', 'aria-label', 'placeholder'];
|
|
6
|
+
const DEFAULT_REACT_IMPORT_SOURCE = '@18ways/react';
|
|
7
|
+
const DEFAULT_RUNTIME_IMPORT_SOURCE = '@18ways/mdx-translate/runtime';
|
|
8
|
+
const DEFAULT_TRANSLATION_COMPONENT_NAME = 'T';
|
|
9
|
+
const DEFAULT_TRANSLATED_NODE_COMPONENT_NAME = 'TranslatedNode';
|
|
10
|
+
const DEFAULT_TRANSLATE_HOOK_NAME = 'useT';
|
|
11
|
+
const createLiteral = (value) => ({
|
|
12
|
+
type: 'Literal',
|
|
13
|
+
value,
|
|
14
|
+
});
|
|
15
|
+
const createIdentifier = (name) => ({
|
|
16
|
+
type: 'Identifier',
|
|
17
|
+
name,
|
|
18
|
+
});
|
|
19
|
+
const createExpressionStatement = (expression) => ({
|
|
20
|
+
type: 'ExpressionStatement',
|
|
21
|
+
expression,
|
|
22
|
+
});
|
|
23
|
+
const createArrayExpression = (items) => ({
|
|
24
|
+
type: 'ArrayExpression',
|
|
25
|
+
elements: items.map((item) => createLiteral(item)),
|
|
26
|
+
});
|
|
27
|
+
const createExpressionAttribute = (name, value, expression) => ({
|
|
28
|
+
type: 'mdxJsxAttribute',
|
|
29
|
+
name,
|
|
30
|
+
value: {
|
|
31
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
32
|
+
value,
|
|
33
|
+
data: {
|
|
34
|
+
estree: {
|
|
35
|
+
type: 'Program',
|
|
36
|
+
sourceType: 'module',
|
|
37
|
+
body: [createExpressionStatement(expression)],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
const createImportSpecifier = (name) => ({
|
|
43
|
+
type: 'ImportSpecifier',
|
|
44
|
+
imported: createIdentifier(name),
|
|
45
|
+
local: createIdentifier(name),
|
|
46
|
+
});
|
|
47
|
+
const createDefaultImportSpecifier = (name) => ({
|
|
48
|
+
type: 'ImportDefaultSpecifier',
|
|
49
|
+
local: createIdentifier(name),
|
|
50
|
+
});
|
|
51
|
+
const createImportDeclaration = (source, names) => ({
|
|
52
|
+
type: 'ImportDeclaration',
|
|
53
|
+
source: createLiteral(source),
|
|
54
|
+
specifiers: names.map(createImportSpecifier),
|
|
55
|
+
attributes: [],
|
|
56
|
+
});
|
|
57
|
+
const createImportNode = (source, names) => ({
|
|
58
|
+
type: 'mdxjsEsm',
|
|
59
|
+
data: {
|
|
60
|
+
estree: {
|
|
61
|
+
type: 'Program',
|
|
62
|
+
sourceType: 'module',
|
|
63
|
+
body: [createImportDeclaration(source, names)],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
const createDefaultImportNode = (source, localName) => ({
|
|
68
|
+
type: 'mdxjsEsm',
|
|
69
|
+
data: {
|
|
70
|
+
estree: {
|
|
71
|
+
type: 'Program',
|
|
72
|
+
sourceType: 'module',
|
|
73
|
+
body: [
|
|
74
|
+
{
|
|
75
|
+
type: 'ImportDeclaration',
|
|
76
|
+
source: createLiteral(source),
|
|
77
|
+
specifiers: [createDefaultImportSpecifier(localName)],
|
|
78
|
+
attributes: [],
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
const matchesPath = (matcher, filePath) => {
|
|
85
|
+
if (!matcher) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (matcher instanceof RegExp) {
|
|
89
|
+
return matcher.test(filePath);
|
|
90
|
+
}
|
|
91
|
+
return matcher(filePath);
|
|
92
|
+
};
|
|
93
|
+
const shouldProcessFile = (filePath, options) => {
|
|
94
|
+
if (!filePath) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (options.exclude && matchesPath(options.exclude, filePath)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return matchesPath(options.include, filePath);
|
|
101
|
+
};
|
|
102
|
+
const isParent = (node) => Boolean(node && typeof node === 'object' && 'children' in node);
|
|
103
|
+
const visitTree = (node, visitor, parent = null, index = null) => {
|
|
104
|
+
visitor(node, parent, index);
|
|
105
|
+
if (!isParent(node) || !Array.isArray(node.children)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
for (let childIndex = 0; childIndex < node.children.length; childIndex += 1) {
|
|
109
|
+
const child = node.children[childIndex];
|
|
110
|
+
visitTree(child, visitor, node, childIndex);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const isWhitespaceOnlyText = (value) => value.trim().length === 0;
|
|
114
|
+
const isLowercaseHtmlName = (name) => typeof name === 'string' && /^[a-z][a-z0-9-]*$/.test(name);
|
|
115
|
+
const isExistingTranslationNode = (node, translationComponentName, translatedNodeComponentName) => (node.type === 'mdxJsxTextElement' || node.type === 'mdxJsxFlowElement') &&
|
|
116
|
+
(node.name === translationComponentName || node.name === translatedNodeComponentName);
|
|
117
|
+
const childrenHaveTranslatableText = (nodes) => {
|
|
118
|
+
for (const node of nodes) {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case 'text':
|
|
121
|
+
case 'inlineCode':
|
|
122
|
+
if (!isWhitespaceOnlyText(node.value)) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
if (isParent(node) &&
|
|
128
|
+
Array.isArray(node.children) &&
|
|
129
|
+
childrenHaveTranslatableText(node.children)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
};
|
|
137
|
+
const createTranslationWrapper = (children, translationComponentName) => ({
|
|
138
|
+
type: 'mdxJsxTextElement',
|
|
139
|
+
name: translationComponentName,
|
|
140
|
+
attributes: [],
|
|
141
|
+
children: children,
|
|
142
|
+
});
|
|
143
|
+
const unwrapDirectTranslationChildren = (children, translationComponentName) => children.flatMap((child) => {
|
|
144
|
+
if (child.type === 'mdxJsxTextElement' &&
|
|
145
|
+
child.name === translationComponentName &&
|
|
146
|
+
child.attributes.length === 0) {
|
|
147
|
+
return child.children;
|
|
148
|
+
}
|
|
149
|
+
return [child];
|
|
150
|
+
});
|
|
151
|
+
const wrapContainerChildren = (node, options) => {
|
|
152
|
+
const normalizedChildren = unwrapDirectTranslationChildren(node.children, options.translationComponentName);
|
|
153
|
+
if (normalizedChildren.length === 1 &&
|
|
154
|
+
isExistingTranslationNode(normalizedChildren[0], options.translationComponentName, options.translatedNodeComponentName)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (normalizedChildren.some((child) => isExistingTranslationNode(child, options.translationComponentName, options.translatedNodeComponentName))) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
if (!childrenHaveTranslatableText(normalizedChildren)) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
node.children = [
|
|
164
|
+
createTranslationWrapper(normalizedChildren, options.translationComponentName),
|
|
165
|
+
];
|
|
166
|
+
return true;
|
|
167
|
+
};
|
|
168
|
+
const getImportDeclaration = (root, source) => {
|
|
169
|
+
for (const child of root.children) {
|
|
170
|
+
if (child.type !== 'mdxjsEsm' || !child.data?.estree?.body?.length) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const declaration = child.data.estree.body.find((statement) => statement.type === 'ImportDeclaration' &&
|
|
174
|
+
statement.source.type === 'Literal' &&
|
|
175
|
+
statement.source.value === source);
|
|
176
|
+
if (declaration) {
|
|
177
|
+
return declaration;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
};
|
|
182
|
+
const ensureNamedImport = (root, source, importName) => {
|
|
183
|
+
const existingDeclaration = getImportDeclaration(root, source);
|
|
184
|
+
if (existingDeclaration) {
|
|
185
|
+
const alreadyImported = existingDeclaration.specifiers.some((specifier) => specifier.type === 'ImportSpecifier' &&
|
|
186
|
+
specifier.imported.type === 'Identifier' &&
|
|
187
|
+
specifier.imported.name === importName &&
|
|
188
|
+
specifier.local.type === 'Identifier' &&
|
|
189
|
+
specifier.local.name === importName);
|
|
190
|
+
if (!alreadyImported) {
|
|
191
|
+
existingDeclaration.specifiers.push(createImportSpecifier(importName));
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
root.children.unshift(createImportNode(source, [importName]));
|
|
196
|
+
};
|
|
197
|
+
const resolveImageReferenceUrl = (node, definitions) => {
|
|
198
|
+
if (node.type === 'image') {
|
|
199
|
+
return node.url;
|
|
200
|
+
}
|
|
201
|
+
return definitions.get(node.identifier)?.url ?? '';
|
|
202
|
+
};
|
|
203
|
+
const normalizeStaticImportSpecifier = (value) => value.replace(/[^a-zA-Z0-9_$]/g, '_');
|
|
204
|
+
const isExternalUrl = (value) => /^https?:\/\//i.test(value);
|
|
205
|
+
const isRelativeAssetUrl = (value) => Boolean(value) && !isExternalUrl(value) && !value.startsWith('/');
|
|
206
|
+
const createTranslatePropsAttribute = (attributeNames) => createExpressionAttribute('translateProps', `[${attributeNames.map((name) => JSON.stringify(name)).join(', ')}]`, createArrayExpression(attributeNames));
|
|
207
|
+
const createTranslatedNode = ({ originalType, originalName, originalAttributes, originalChildren, translatedNodeComponentName, translateProps, }) => ({
|
|
208
|
+
type: originalType,
|
|
209
|
+
name: translatedNodeComponentName,
|
|
210
|
+
attributes: [
|
|
211
|
+
{
|
|
212
|
+
type: 'mdxJsxAttribute',
|
|
213
|
+
name: 'component',
|
|
214
|
+
value: originalName,
|
|
215
|
+
},
|
|
216
|
+
createTranslatePropsAttribute(translateProps),
|
|
217
|
+
...originalAttributes,
|
|
218
|
+
],
|
|
219
|
+
children: originalType === 'mdxJsxTextElement'
|
|
220
|
+
? originalChildren
|
|
221
|
+
: originalChildren,
|
|
222
|
+
});
|
|
223
|
+
const getTranslatableAttributeNames = (attributes, attributeNames) => attributes
|
|
224
|
+
.filter((attribute) => attributeNames.has(attribute.name) && typeof attribute.value === 'string')
|
|
225
|
+
.map((attribute) => attribute.name);
|
|
226
|
+
const buildImportedImageAttribute = (variableName) => createExpressionAttribute('src', variableName, createIdentifier(variableName));
|
|
227
|
+
const createImageAttributes = ({ srcValue, alt, title, }) => {
|
|
228
|
+
const attributes = [
|
|
229
|
+
{
|
|
230
|
+
type: 'mdxJsxAttribute',
|
|
231
|
+
name: 'src',
|
|
232
|
+
value: srcValue,
|
|
233
|
+
},
|
|
234
|
+
];
|
|
235
|
+
if (typeof alt === 'string') {
|
|
236
|
+
attributes.push({
|
|
237
|
+
type: 'mdxJsxAttribute',
|
|
238
|
+
name: 'alt',
|
|
239
|
+
value: alt,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (typeof title === 'string') {
|
|
243
|
+
attributes.push({
|
|
244
|
+
type: 'mdxJsxAttribute',
|
|
245
|
+
name: 'title',
|
|
246
|
+
value: title,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
return attributes;
|
|
250
|
+
};
|
|
251
|
+
const createTranslatedImageNode = ({ srcValue, alt, title, translatedNodeComponentName, asTextElement, }) => {
|
|
252
|
+
const translateProps = ['alt', 'title'].filter((name) => name === 'alt'
|
|
253
|
+
? typeof alt === 'string' && alt.length > 0
|
|
254
|
+
: typeof title === 'string' && title.length > 0);
|
|
255
|
+
return {
|
|
256
|
+
type: asTextElement ? 'mdxJsxTextElement' : 'mdxJsxFlowElement',
|
|
257
|
+
name: translatedNodeComponentName,
|
|
258
|
+
attributes: [
|
|
259
|
+
{
|
|
260
|
+
type: 'mdxJsxAttribute',
|
|
261
|
+
name: 'component',
|
|
262
|
+
value: 'img',
|
|
263
|
+
},
|
|
264
|
+
createTranslatePropsAttribute(translateProps),
|
|
265
|
+
...createImageAttributes({ srcValue, alt, title }),
|
|
266
|
+
],
|
|
267
|
+
children: [],
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
function remarkMdxTranslate(userOptions = {}) {
|
|
271
|
+
const options = {
|
|
272
|
+
include: userOptions.include,
|
|
273
|
+
exclude: userOptions.exclude,
|
|
274
|
+
attributeNames: new Set(userOptions.attributeNames || DEFAULT_ATTRIBUTE_NAMES),
|
|
275
|
+
translationComponentName: userOptions.translationComponentName || DEFAULT_TRANSLATION_COMPONENT_NAME,
|
|
276
|
+
translatedNodeComponentName: userOptions.translatedNodeComponentName || DEFAULT_TRANSLATED_NODE_COMPONENT_NAME,
|
|
277
|
+
translateHookName: userOptions.translateHookName || DEFAULT_TRANSLATE_HOOK_NAME,
|
|
278
|
+
reactImportSource: userOptions.reactImportSource || DEFAULT_REACT_IMPORT_SOURCE,
|
|
279
|
+
runtimeImportSource: userOptions.runtimeImportSource || DEFAULT_RUNTIME_IMPORT_SOURCE,
|
|
280
|
+
};
|
|
281
|
+
return (tree, file) => {
|
|
282
|
+
const root = tree;
|
|
283
|
+
if (!shouldProcessFile(file?.path, { include: options.include, exclude: options.exclude })) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const definitions = new Map();
|
|
287
|
+
visitTree(root, (node) => {
|
|
288
|
+
if (node.type === 'definition') {
|
|
289
|
+
definitions.set(node.identifier, node);
|
|
290
|
+
}
|
|
291
|
+
if (node.type === 'code') {
|
|
292
|
+
const transformed = (0, code_samples_1.transformCodeSample)(node.value, node.lang, {
|
|
293
|
+
translationComponentName: options.translationComponentName,
|
|
294
|
+
translateHookName: options.translateHookName,
|
|
295
|
+
reactImportSource: options.reactImportSource,
|
|
296
|
+
attributeNames: Array.from(options.attributeNames),
|
|
297
|
+
});
|
|
298
|
+
if (typeof transformed === 'string' && transformed !== node.value) {
|
|
299
|
+
node.value = transformed;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
let needsTranslationComponentImport = false;
|
|
304
|
+
let needsTranslatedNodeImport = false;
|
|
305
|
+
const importedImages = new Map();
|
|
306
|
+
const ensureImportedImageExpression = (url) => {
|
|
307
|
+
if (!isRelativeAssetUrl(url)) {
|
|
308
|
+
return url;
|
|
309
|
+
}
|
|
310
|
+
let variableName = importedImages.get(url);
|
|
311
|
+
if (!variableName) {
|
|
312
|
+
variableName = `__mdxTranslateImg_${normalizeStaticImportSpecifier(String(importedImages.size))}`;
|
|
313
|
+
importedImages.set(url, variableName);
|
|
314
|
+
}
|
|
315
|
+
return buildImportedImageAttribute(variableName).value;
|
|
316
|
+
};
|
|
317
|
+
visitTree(root, (node, parent, index) => {
|
|
318
|
+
if (node.type === 'heading' || node.type === 'paragraph' || node.type === 'tableCell') {
|
|
319
|
+
const didWrap = wrapContainerChildren(node, {
|
|
320
|
+
translationComponentName: options.translationComponentName,
|
|
321
|
+
translatedNodeComponentName: options.translatedNodeComponentName,
|
|
322
|
+
});
|
|
323
|
+
if (didWrap) {
|
|
324
|
+
needsTranslationComponentImport = true;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if ((node.type === 'mdxJsxTextElement' || node.type === 'mdxJsxFlowElement') &&
|
|
328
|
+
node.name !== options.translatedNodeComponentName &&
|
|
329
|
+
node.name !== options.translationComponentName &&
|
|
330
|
+
isLowercaseHtmlName(node.name)) {
|
|
331
|
+
const translateProps = getTranslatableAttributeNames(node.attributes, options.attributeNames);
|
|
332
|
+
if (!translateProps.length) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const translatedNode = createTranslatedNode({
|
|
336
|
+
originalType: node.type,
|
|
337
|
+
originalName: node.name,
|
|
338
|
+
originalAttributes: node.attributes.map((attribute) => {
|
|
339
|
+
if (node.name === 'img' &&
|
|
340
|
+
attribute.name === 'src' &&
|
|
341
|
+
typeof attribute.value === 'string' &&
|
|
342
|
+
isRelativeAssetUrl(attribute.value)) {
|
|
343
|
+
return {
|
|
344
|
+
...attribute,
|
|
345
|
+
value: ensureImportedImageExpression(attribute.value),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
return attribute;
|
|
349
|
+
}),
|
|
350
|
+
originalChildren: node.children,
|
|
351
|
+
translatedNodeComponentName: options.translatedNodeComponentName,
|
|
352
|
+
translateProps,
|
|
353
|
+
});
|
|
354
|
+
Object.assign(node, translatedNode);
|
|
355
|
+
needsTranslatedNodeImport = true;
|
|
356
|
+
}
|
|
357
|
+
if (!parent || index === null) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (node.type !== 'image' && node.type !== 'imageReference') {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const url = resolveImageReferenceUrl(node, definitions);
|
|
364
|
+
const alt = 'alt' in node ? (node.alt ?? null) : null;
|
|
365
|
+
const title = 'title' in node ? (node.title ?? null) : null;
|
|
366
|
+
const translateProps = ['alt', 'title'].filter((name) => name === 'alt'
|
|
367
|
+
? typeof alt === 'string' && alt.length > 0
|
|
368
|
+
: typeof title === 'string' && title.length > 0);
|
|
369
|
+
if (!url || !translateProps.length) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
parent.children[index] = createTranslatedImageNode({
|
|
373
|
+
srcValue: ensureImportedImageExpression(url),
|
|
374
|
+
alt,
|
|
375
|
+
title,
|
|
376
|
+
translatedNodeComponentName: options.translatedNodeComponentName,
|
|
377
|
+
asTextElement: parent.type === 'paragraph' || parent.type === 'heading',
|
|
378
|
+
});
|
|
379
|
+
needsTranslatedNodeImport = true;
|
|
380
|
+
});
|
|
381
|
+
if (needsTranslationComponentImport) {
|
|
382
|
+
ensureNamedImport(root, options.reactImportSource, options.translationComponentName);
|
|
383
|
+
}
|
|
384
|
+
if (needsTranslatedNodeImport) {
|
|
385
|
+
ensureNamedImport(root, options.runtimeImportSource, options.translatedNodeComponentName);
|
|
386
|
+
}
|
|
387
|
+
for (const [url, variableName] of Array.from(importedImages.entries()).reverse()) {
|
|
388
|
+
root.children.unshift(createDefaultImportNode(url, variableName));
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAsgBA,gDA4JC;AA1oBD,iDAAqD;AAyDrD,MAAM,uBAAuB,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC;AAC9E,MAAM,2BAA2B,GAAG,eAAe,CAAC;AACpD,MAAM,6BAA6B,GAAG,+BAA+B,CAAC;AACtE,MAAM,kCAAkC,GAAG,GAAG,CAAC;AAC/C,MAAM,sCAAsC,GAAG,gBAAgB,CAAC;AAChE,MAAM,2BAA2B,GAAG,MAAM,CAAC;AAE3C,MAAM,aAAa,GAAG,CAAC,KAAa,EAAW,EAAE,CAAC,CAAC;IACjD,IAAI,EAAE,SAAS;IACf,KAAK;CACN,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAc,EAAE,CAAC,CAAC;IACtD,IAAI,EAAE,YAAY;IAClB,IAAI;CACL,CAAC,CAAC;AAEH,MAAM,yBAAyB,GAAG,CAAC,UAAsB,EAAuB,EAAE,CAAC,CAAC;IAClF,IAAI,EAAE,qBAAqB;IAC3B,UAAU;CACX,CAAC,CAAC;AAEH,MAAM,qBAAqB,GAAG,CAAC,KAAe,EAAmB,EAAE,CAAC,CAAC;IACnE,IAAI,EAAE,iBAAiB;IACvB,QAAQ,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;CACnD,CAAC,CAA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F,IAAI,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,CAAC;gBACnC,OAAO;YACT,CAAC;YAED,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,yBAAyB,CAAC;gBACjD,QAAQ,EAAE,6BAA6B,CAAC,GAAG,CAAC;gBAC5C,GAAG;gBACH,KAAK;gBACL,2BAA2B,EAAE,OAAO,CAAC,2BAA2B;gBAChE,aAAa,EAAE,MAAM,CAAC,IAAI,KAAK,WAAW,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;aACxE,CAAgB,CAAC;YAElB,yBAAyB,GAAG,IAAI,CAAC;QACnC,CAAC,CAAC,CAAC;QAEH,IAAI,+BAA+B,EAAE,CAAC;YACpC,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,iBAAiB,EAAE,OAAO,CAAC,wBAAwB,CAAC,CAAC;QACvF,CAAC;QAED,IAAI,yBAAyB,EAAE,CAAC;YAC9B,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,mBAAmB,EAAE,OAAO,CAAC,2BAA2B,CAAC,CAAC;QAC5F,CAAC;QAED,KAAK,MAAM,CAAC,GAAG,EAAE,YAAY,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;YACjF,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,uBAAuB,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC;QACpE,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
type TranslatedNodeProps = {
|
|
3
|
+
component: keyof JSX.IntrinsicElements;
|
|
4
|
+
translateProps?: string[];
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
} & Record<string, unknown>;
|
|
7
|
+
export declare function TranslatedNode({ component, translateProps, children, ...props }: TranslatedNodeProps): import("react").DOMElement<Record<string, unknown>, Element>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAiB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAStD,KAAK,mBAAmB,GAAG;IACzB,SAAS,EAAE,MAAM,GAAG,CAAC,iBAAiB,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAU5B,wBAAgB,cAAc,CAAC,EAC7B,SAAS,EACT,cAAmB,EACnB,QAAQ,EACR,GAAG,KAAK,EACT,EAAE,mBAAmB,gEA8BrB"}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.TranslatedNode = TranslatedNode;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const react_2 = require("@18ways/react");
|
|
7
|
+
const isStaticImageLike = (value) => Boolean(value &&
|
|
8
|
+
typeof value === 'object' &&
|
|
9
|
+
'src' in value &&
|
|
10
|
+
typeof value.src === 'string');
|
|
11
|
+
function TranslatedNode({ component, translateProps = [], children, ...props }) {
|
|
12
|
+
const t = (0, react_2.useT)();
|
|
13
|
+
const translatedProps = { ...props };
|
|
14
|
+
for (const propName of translateProps) {
|
|
15
|
+
const value = translatedProps[propName];
|
|
16
|
+
if (typeof value === 'string' && value.trim()) {
|
|
17
|
+
translatedProps[propName] = t(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (component === 'img') {
|
|
21
|
+
const src = translatedProps.src;
|
|
22
|
+
if (isStaticImageLike(src)) {
|
|
23
|
+
translatedProps.src = src.src;
|
|
24
|
+
if (translatedProps.width == null && typeof src.width === 'number') {
|
|
25
|
+
translatedProps.width = src.width;
|
|
26
|
+
}
|
|
27
|
+
if (translatedProps.height == null && typeof src.height === 'number') {
|
|
28
|
+
translatedProps.height = src.height;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
delete translatedProps.placeholder;
|
|
32
|
+
delete translatedProps.blurDataURL;
|
|
33
|
+
}
|
|
34
|
+
return (0, react_1.createElement)(component, translatedProps, children);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AAyBb,wCAmCC;AA1DD,iCAAsD;AACtD,yCAAqC;AAcrC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAA4B,EAAE,CACrE,OAAO,CACL,KAAK;IACL,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,IAAI,KAAK;IACd,OAAQ,KAAyB,CAAC,GAAG,KAAK,QAAQ,CACnD,CAAC;AAEJ,SAAgB,cAAc,CAAC,EAC7B,SAAS,EACT,cAAc,GAAG,EAAE,EACnB,QAAQ,EACR,GAAG,KAAK,EACY;IACpB,MAAM,CAAC,GAAG,IAAA,YAAI,GAAE,CAAC;IACjB,MAAM,eAAe,GAA4B,EAAE,GAAG,KAAK,EAAE,CAAC;IAE9D,KAAK,MAAM,QAAQ,IAAI,cAAc,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;YAC9C,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,CAAW,CAAC;QACjD,CAAC;IACH,CAAC;IAED,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;QACxB,MAAM,GAAG,GAAG,eAAe,CAAC,GAAG,CAAC;QAChC,IAAI,iBAAiB,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,eAAe,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;YAE9B,IAAI,eAAe,CAAC,KAAK,IAAI,IAAI,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;gBACnE,eAAe,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;YACpC,CAAC;YAED,IAAI,eAAe,CAAC,MAAM,IAAI,IAAI,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACrE,eAAe,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;YACtC,CAAC;QACH,CAAC;QAED,OAAO,eAAe,CAAC,WAAW,CAAC;QACnC,OAAO,eAAe,CAAC,WAAW,CAAC;IACrC,CAAC;IAED,OAAO,IAAA,qBAAa,EAAC,SAAS,EAAE,eAAe,EAAE,QAAQ,CAAC,CAAC;AAC7D,CAAC"}
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/index';
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@18ways/mdx-translate",
|
|
3
|
+
"version": "0.1.0-alpha.42e08a82e9b3",
|
|
4
|
+
"description": "Remark-based MDX translation transforms for 18ways",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "./index.ts",
|
|
7
|
+
"types": "./index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.ts",
|
|
11
|
+
"import": "./index.ts",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./runtime": {
|
|
15
|
+
"types": "./runtime.tsx",
|
|
16
|
+
"import": "./runtime.tsx",
|
|
17
|
+
"require": "./dist/runtime.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"dev": "tsc --watch",
|
|
23
|
+
"ts:check": "tsc --noEmit",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@18ways/react": "0.0.0-alpha.42e08a82e9b3",
|
|
29
|
+
"@babel/generator": "^7.28.5",
|
|
30
|
+
"@babel/parser": "^7.28.5",
|
|
31
|
+
"@babel/traverse": "^7.28.5",
|
|
32
|
+
"@babel/types": "^7.28.5"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@mdx-js/mdx": "^3.1.1",
|
|
39
|
+
"@types/estree": "^1.0.8",
|
|
40
|
+
"@types/mdast": "^4.0.4",
|
|
41
|
+
"@types/node": "^22.10.0",
|
|
42
|
+
"@types/react": "^18.3.12",
|
|
43
|
+
"@types/unist": "^3.0.3",
|
|
44
|
+
"react": "^18.3.1",
|
|
45
|
+
"typescript": "^5.6.2",
|
|
46
|
+
"vitest": "^3.2.4"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"packageManager": "bun@1.3.8"
|
|
50
|
+
}
|
package/runtime.tsx
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/runtime';
|