-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremark-snackplayer.js
93 lines (81 loc) · 2.64 KB
/
remark-snackplayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const visit = require('unist-util-visit-parents');
const fromEntries = require('object.fromentries');
const parseParams = (paramString = '') => {
const params = fromEntries(new URLSearchParams(paramString));
if (!params.platform) {
params.platform = 'web';
}
return params;
};
function attr(name, value) {
return {
type: 'mdxJsxAttribute',
name,
value,
};
}
async function toJsxNode(node) {
const params = parseParams(node.meta);
// Gather necessary Params
const name = params.name ? decodeURIComponent(params.name) : 'Example';
const description = params.description ? decodeURIComponent(params.description) : 'Example usage';
const ext = params.ext ? decodeURIComponent(params.ext) : 'tsx';
const filename = `App.${ext}`;
const files = encodeURIComponent(
JSON.stringify({
[filename]: {
type: 'CODE',
contents: node.value,
},
}),
);
const dependencies = params.dependencies || '';
const platform = params.platform || 'web';
const supportedPlatforms = params.supportedPlatforms || 'ios,android,web';
const theme = params.theme || 'light';
const preview = params.preview || 'true';
const loading = params.loading || 'lazy';
// Need help constructing this AST node?
// Use the MDX Playground and explore what your output mdast should look like
// https://mdxjs.com/playground/
const jsxNode = {
type: 'mdxJsxTextElement',
name: 'div',
attributes: [
attr('class', 'snack-player'),
attr('data-snack-name', name),
attr('data-snack-description', description),
attr('data-snack-files', files),
attr('data-snack-dependencies', dependencies),
attr('data-snack-platform', platform),
attr('data-snack-supported-platforms', supportedPlatforms),
attr('data-snack-theme', theme),
attr('data-snack-preview', preview),
attr('data-snack-loading', loading),
attr('data-snack-device-frame', 'false'),
],
children: [],
};
// We "replace" the current node by a JSX node
Object.keys(node).forEach((key) => delete node[key]);
Object.keys(jsxNode).forEach((key) => (node[key] = jsxNode[key]));
}
const SnackPlayer = () => {
return async (tree) => {
const nodesToProcess = [];
visit(tree, 'code', (node, parent) => {
if (node.lang === 'SnackPlayer') {
nodesToProcess.push(toJsxNode(node, parent));
}
});
await Promise.all(nodesToProcess);
};
};
module.exports = SnackPlayer;