@ohos-ports/mdx-rs 0.0.0-stage → 0.6.4-beta.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Web Infra
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 CHANGED
@@ -1,3 +1,104 @@
1
- # Temporary Holding Version
1
+ # @rspress/mdx-rs
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This is a Node.js binding for MDX compilation of [Rspress](https://rspress.dev) which is a modern documentation tool based on [Rspack](https://www.rspack.org/).
4
+
5
+ It can be 10x+ faster than compiler in pure JavaScript version.The [benchmark](./benches/index.mjs) result of `@rspress/mdx-rs` vs `@mdx-js/mdx` is as follows:
6
+
7
+ | Tool | Time Spend |
8
+ | --------------- | ---------- |
9
+ | @rspress/mdx-rs | 977 ms |
10
+ | @mdx-js/mdx | 21300 ms |
11
+
12
+ We forked [mdxjs-rs](https://github.com/wooorm/mdxjs-rs), the Rust version of mdx compiler and customize it for Rspress, adding the following features:
13
+
14
+ | Crate | Description |
15
+ | ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
16
+ | [mdx_rs_binding](./crates/binding) | 🔥 Add Node.js binding so that we can use it in Node.js. |
17
+ | [mdx_plugin_container](./crates/plugin_container) | Implement container grammar like `:::tip` in [@rspress/plugin-container-syntax](https://github.com/web-infra-dev/rspress/blob/main/packages/plugin-container-syntax/src/remarkPlugin.ts). |
18
+ | [mdx_plugin_toc](./crates/plugin_toc) | Generate table of contents. |
19
+ | [mdx_plugin_frontmatter](./crates/plugin_frontmatter) | Parse frontmatter and export it in the esm module. |
20
+ | [mdx_plugin_external_link](./crates/plugin_external_link) | Add `target="_blank"` and `rel="noopener noreferrer"` to external link. |
21
+ | [mdx_plugin_header_anchor](./crates/plugin_header_anchor) | Add anchor for every header. |
22
+ | [mdx_plugin_normalize_link](./crates/plugin_normalize_link) | Normalize link to complete url base on current filepath. |
23
+ | [mdx_plugin_html](./crates/plugin_html) | Serialize hast to html string |
24
+ | [slugger](./crates/slugger) | Generate slug for header, port from [github-slugger](https://github.com/Flet/github-slugger). |
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ # npm
30
+ npm install @rspress/mdx-rs
31
+ # yarn
32
+ yarn add @rspress/mdx-rs
33
+ # pnpm
34
+ pnpm install @rspress/mdx-rs
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ```js
40
+ import { compile } from "@rspress/mdx-rs";
41
+
42
+ async function main() {
43
+ const value = `
44
+ # Hello World
45
+
46
+ This is a demo of @rspress/mdx-rs
47
+ `;
48
+
49
+ const result = await compile({
50
+ // The mdx content
51
+ value,
52
+ // File path of the mdx file, the compiler will determine the different syntax(md/mdx) based on the file extension
53
+ filepath: "xxx.mdx",
54
+ // Whether to enable development mode, default is false
55
+ development: true,
56
+ // Current working directory, can be empty string
57
+ root: "",
58
+ });
59
+
60
+ console.log(result);
61
+ }
62
+ ```
63
+
64
+ Of course, you can also the `compileSync` function to compile mdx synchronously, which is not recommended because it will block the event loop and slow down the compile process.
65
+
66
+ ```js
67
+ import { compileSync } from "@rspress/mdx-rs";
68
+
69
+ function main() {
70
+ const value = `
71
+ # Hello World
72
+
73
+ This is a demo of @rspress/mdx-rs
74
+ `;
75
+
76
+ const result = compileSync({
77
+ // The mdx content
78
+ value,
79
+ // File path of the mdx file, can be empty string
80
+ filepath: "",
81
+ // Whether to enable development mode, default is false
82
+ development: true,
83
+ // Current working directory, can be empty string
84
+ root: "",
85
+ });
86
+
87
+ console.log(result);
88
+ }
89
+ ```
90
+
91
+ ## Credits
92
+
93
+ Thanks to [mdxjs-rs](https://github.com/wooorm/mdxjs-rs), the awesome Rust library authored by [wooorm](https://github.com/wooorm).
94
+
95
+ Also, thanks to [napi-rs](https://github.com/napi-rs/napi-rs), authored by [Brooooooklyn](https://github.com/Brooooooklyn), which is a great solution to help us build Node.js binding for Rust.
96
+
97
+
98
+ ---
99
+
100
+ # OpenHarmony port note
101
+
102
+ This package is the OpenHarmony (openharmony/arm64) port of `@rspress/mdx-rs`, published by the `@ohos-ports` organization as `@ohos-ports/mdx-rs@0.6.4-beta.1`.
103
+
104
+ It is content-identical to the upstream package except for the `package.json` identity/dependency redirects needed to route the runtime dependency chain to OpenHarmony-patched `@ohos-ports` packages.
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export interface Toc {
7
+ text: string
8
+ id: string
9
+ depth: number
10
+ }
11
+ export interface Output {
12
+ code: string
13
+ links: Array<string>
14
+ html: string
15
+ title: string
16
+ toc: Array<Toc>
17
+ languages: Array<string>
18
+ frontmatter: string
19
+ }
20
+ export interface CompileOptions {
21
+ value: string
22
+ filepath: string
23
+ development: boolean
24
+ root: string
25
+ }
26
+ /** Turn MDX into JavaScript. */
27
+ export function compile(options: CompileOptions): Promise<Output>
28
+ export function compileSync(options: CompileOptions): Output
package/index.js ADDED
@@ -0,0 +1,272 @@
1
+ const { existsSync, readFileSync } = require('fs')
2
+ const { join } = require('path')
3
+
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+ let localFileExisted = false
8
+ let loadError = null
9
+
10
+ function isMusl() {
11
+ // For Node 10
12
+ if (!process.report || typeof process.report.getReport !== 'function') {
13
+ try {
14
+ const lddPath = require('child_process').execSync('which ldd').toString().trim();
15
+ return readFileSync(lddPath, 'utf8').includes('musl')
16
+ } catch (e) {
17
+ return true
18
+ }
19
+ } else {
20
+ const { glibcVersionRuntime } = process.report.getReport().header
21
+ return !glibcVersionRuntime
22
+ }
23
+ }
24
+
25
+ switch (platform) {
26
+ case 'android':
27
+ switch (arch) {
28
+ case 'arm64':
29
+ localFileExisted = existsSync(join(__dirname, 'mdx-binding-core.android-arm64.node'))
30
+ try {
31
+ if (localFileExisted) {
32
+ nativeBinding = require('./mdx-binding-core.android-arm64.node')
33
+ } else {
34
+ nativeBinding = require('@rspress/mdx-rs-android-arm64')
35
+ }
36
+ } catch (e) {
37
+ loadError = e
38
+ }
39
+ break
40
+ case 'arm':
41
+ localFileExisted = existsSync(join(__dirname, 'mdx-binding-core.android-arm-eabi.node'))
42
+ try {
43
+ if (localFileExisted) {
44
+ nativeBinding = require('./mdx-binding-core.android-arm-eabi.node')
45
+ } else {
46
+ nativeBinding = require('@rspress/mdx-rs-android-arm-eabi')
47
+ }
48
+ } catch (e) {
49
+ loadError = e
50
+ }
51
+ break
52
+ default:
53
+ throw new Error(`Unsupported architecture on Android ${arch}`)
54
+ }
55
+ break
56
+ case 'win32':
57
+ switch (arch) {
58
+ case 'x64':
59
+ localFileExisted = existsSync(
60
+ join(__dirname, 'mdx-binding-core.win32-x64-msvc.node')
61
+ )
62
+ try {
63
+ if (localFileExisted) {
64
+ nativeBinding = require('./mdx-binding-core.win32-x64-msvc.node')
65
+ } else {
66
+ nativeBinding = require('@rspress/mdx-rs-win32-x64-msvc')
67
+ }
68
+ } catch (e) {
69
+ loadError = e
70
+ }
71
+ break
72
+ case 'ia32':
73
+ localFileExisted = existsSync(
74
+ join(__dirname, 'mdx-binding-core.win32-ia32-msvc.node')
75
+ )
76
+ try {
77
+ if (localFileExisted) {
78
+ nativeBinding = require('./mdx-binding-core.win32-ia32-msvc.node')
79
+ } else {
80
+ nativeBinding = require('@rspress/mdx-rs-win32-ia32-msvc')
81
+ }
82
+ } catch (e) {
83
+ loadError = e
84
+ }
85
+ break
86
+ case 'arm64':
87
+ localFileExisted = existsSync(
88
+ join(__dirname, 'mdx-binding-core.win32-arm64-msvc.node')
89
+ )
90
+ try {
91
+ if (localFileExisted) {
92
+ nativeBinding = require('./mdx-binding-core.win32-arm64-msvc.node')
93
+ } else {
94
+ nativeBinding = require('@rspress/mdx-rs-win32-arm64-msvc')
95
+ }
96
+ } catch (e) {
97
+ loadError = e
98
+ }
99
+ break
100
+ default:
101
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
102
+ }
103
+ break
104
+ case 'darwin':
105
+ localFileExisted = existsSync(join(__dirname, 'mdx-binding-core.darwin-universal.node'))
106
+ try {
107
+ if (localFileExisted) {
108
+ nativeBinding = require('./mdx-binding-core.darwin-universal.node')
109
+ } else {
110
+ nativeBinding = require('@rspress/mdx-rs-darwin-universal')
111
+ }
112
+ break
113
+ } catch {}
114
+ switch (arch) {
115
+ case 'x64':
116
+ localFileExisted = existsSync(join(__dirname, 'mdx-binding-core.darwin-x64.node'))
117
+ try {
118
+ if (localFileExisted) {
119
+ nativeBinding = require('./mdx-binding-core.darwin-x64.node')
120
+ } else {
121
+ nativeBinding = require('@rspress/mdx-rs-darwin-x64')
122
+ }
123
+ } catch (e) {
124
+ loadError = e
125
+ }
126
+ break
127
+ case 'arm64':
128
+ localFileExisted = existsSync(
129
+ join(__dirname, 'mdx-binding-core.darwin-arm64.node')
130
+ )
131
+ try {
132
+ if (localFileExisted) {
133
+ nativeBinding = require('./mdx-binding-core.darwin-arm64.node')
134
+ } else {
135
+ nativeBinding = require('@rspress/mdx-rs-darwin-arm64')
136
+ }
137
+ } catch (e) {
138
+ loadError = e
139
+ }
140
+ break
141
+ default:
142
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
143
+ }
144
+ break
145
+ case 'freebsd':
146
+ if (arch !== 'x64') {
147
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
148
+ }
149
+ localFileExisted = existsSync(join(__dirname, 'mdx-binding-core.freebsd-x64.node'))
150
+ try {
151
+ if (localFileExisted) {
152
+ nativeBinding = require('./mdx-binding-core.freebsd-x64.node')
153
+ } else {
154
+ nativeBinding = require('@rspress/mdx-rs-freebsd-x64')
155
+ }
156
+ } catch (e) {
157
+ loadError = e
158
+ }
159
+ break
160
+ case 'linux':
161
+ switch (arch) {
162
+ case 'x64':
163
+ if (isMusl()) {
164
+ localFileExisted = existsSync(
165
+ join(__dirname, 'mdx-binding-core.linux-x64-musl.node')
166
+ )
167
+ try {
168
+ if (localFileExisted) {
169
+ nativeBinding = require('./mdx-binding-core.linux-x64-musl.node')
170
+ } else {
171
+ nativeBinding = require('@rspress/mdx-rs-linux-x64-musl')
172
+ }
173
+ } catch (e) {
174
+ loadError = e
175
+ }
176
+ } else {
177
+ localFileExisted = existsSync(
178
+ join(__dirname, 'mdx-binding-core.linux-x64-gnu.node')
179
+ )
180
+ try {
181
+ if (localFileExisted) {
182
+ nativeBinding = require('./mdx-binding-core.linux-x64-gnu.node')
183
+ } else {
184
+ nativeBinding = require('@rspress/mdx-rs-linux-x64-gnu')
185
+ }
186
+ } catch (e) {
187
+ loadError = e
188
+ }
189
+ }
190
+ break
191
+ case 'arm64':
192
+ if (isMusl()) {
193
+ localFileExisted = existsSync(
194
+ join(__dirname, 'mdx-binding-core.linux-arm64-musl.node')
195
+ )
196
+ try {
197
+ if (localFileExisted) {
198
+ nativeBinding = require('./mdx-binding-core.linux-arm64-musl.node')
199
+ } else {
200
+ nativeBinding = require('@rspress/mdx-rs-linux-arm64-musl')
201
+ }
202
+ } catch (e) {
203
+ loadError = e
204
+ }
205
+ } else {
206
+ localFileExisted = existsSync(
207
+ join(__dirname, 'mdx-binding-core.linux-arm64-gnu.node')
208
+ )
209
+ try {
210
+ if (localFileExisted) {
211
+ nativeBinding = require('./mdx-binding-core.linux-arm64-gnu.node')
212
+ } else {
213
+ nativeBinding = require('@rspress/mdx-rs-linux-arm64-gnu')
214
+ }
215
+ } catch (e) {
216
+ loadError = e
217
+ }
218
+ }
219
+ break
220
+ case 'arm':
221
+ localFileExisted = existsSync(
222
+ join(__dirname, 'mdx-binding-core.linux-arm-gnueabihf.node')
223
+ )
224
+ try {
225
+ if (localFileExisted) {
226
+ nativeBinding = require('./mdx-binding-core.linux-arm-gnueabihf.node')
227
+ } else {
228
+ nativeBinding = require('@rspress/mdx-rs-linux-arm-gnueabihf')
229
+ }
230
+ } catch (e) {
231
+ loadError = e
232
+ }
233
+ break
234
+ default:
235
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
236
+ }
237
+ break
238
+ case 'openharmony':
239
+ switch (arch) {
240
+ case 'arm64':
241
+ localFileExisted = existsSync(
242
+ join(__dirname, 'mdx-binding-core.openharmony-arm64.node')
243
+ )
244
+ try {
245
+ if (localFileExisted) {
246
+ nativeBinding = require('./mdx-binding-core.openharmony-arm64.node')
247
+ } else {
248
+ nativeBinding = require('@rspress/mdx-rs-openharmony-arm64')
249
+ }
250
+ } catch (e) {
251
+ loadError = e
252
+ }
253
+ break
254
+ default:
255
+ throw new Error(`Unsupported architecture on OpenHarmony: ${arch}`)
256
+ }
257
+ break
258
+ default:
259
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
260
+ }
261
+
262
+ if (!nativeBinding) {
263
+ if (loadError) {
264
+ throw loadError
265
+ }
266
+ throw new Error(`Failed to load native binding`)
267
+ }
268
+
269
+ const { compile, compileSync } = nativeBinding
270
+
271
+ module.exports.compile = compile
272
+ module.exports.compileSync = compileSync
package/package.json CHANGED
@@ -1,6 +1,25 @@
1
1
  {
2
2
  "name": "@ohos-ports/mdx-rs",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "MDX compilation binding for Rspress",
4
+ "version": "0.6.4-beta.1",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts",
10
+ "*.node"
11
+ ],
12
+ "license": "MIT",
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
20
+ "directory": "ports/mdx-rs/0.6.4"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
24
+ }
25
+ }