@mdrs/core 1.0.0-alpha1 → 1.0.0-alpha3
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 +60 -77
- package/index.d.ts +21 -18
- package/index.js +11 -11
- package/index.native.d.ts +32 -14
- package/index.native.js +620 -378
- package/package.json +23 -12
package/README.md
CHANGED
|
@@ -1,119 +1,102 @@
|
|
|
1
1
|
# MDRS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://dl.circleci.com/status-badge/redirect/circleci/stZqvYiNAK17m5zy7ERCb/EGBRyRQ9CWeyLiSDGguM5Z/tree/main) [](https://codecov.io/gh/farhnkrnapratma/mdrs) [](https://www.npmjs.com/package/@mdrs/core) [](https://crates.io/crates/mdrs) [](https://docs.rs/mdrs) 
|
|
4
|
+
|
|
5
|
+
**MDRS (Markdown Rich Syntax)** is a superset of Markdown. A CommonMark or GitHub Flavored Markdown document renders as it would anywhere else, and MDRS adds callouts, collapsible sections, steps, tabs, math, diagrams, extended task lists and document variables on top. This repository is the MDRS renderer: it turns MDRS into sanitized HTML, is written in Rust and is available as a crate and as an npm package for Node.js and Bun.
|
|
4
6
|
|
|
5
7
|
MDRS files use the `.mdrs` extension.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
> **Alpha.** The API may still change before 1.0.0. On npm, install the `alpha` tag.
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## A taste
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
```mdrs
|
|
14
|
+
:::define
|
|
15
|
+
project: "MDRS"
|
|
16
|
+
:::
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
| --- | --- |
|
|
15
|
-
| crates.io | [`mdrs`](https://crates.io/crates/mdrs) |
|
|
16
|
-
| npm | [`@mdrs/core`](https://www.npmjs.com/package/@mdrs/core) |
|
|
18
|
+
# Hello from ${project}
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
Plain **Markdown** works as usual.
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
:::tip
|
|
23
|
+
Directives open with `:::name` and close with a bare `:::`.
|
|
24
|
+
:::
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
:::details summary="What is rendered on the server?"
|
|
27
|
+
Everything except diagrams and charts, including math: $$ e^{i\pi} + 1 = 0 $$
|
|
28
|
+
:::
|
|
25
29
|
```
|
|
26
30
|
|
|
27
|
-
The
|
|
31
|
+
The [MDRS syntax guide](https://github.com/farhnkrnapratma/idstead/tree/main/docs/008-mdrs) describes every construct. It is written for the original TypeScript engine; see [Limitations](#limitations) for what the Rust renderer does not support yet.
|
|
28
32
|
|
|
29
|
-
##
|
|
33
|
+
## Install
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
```sh
|
|
36
|
+
npm install @mdrs/core@alpha # or: bun add @mdrs/core@alpha
|
|
37
|
+
cargo add mdrs # Rust
|
|
38
|
+
```
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
use std::path::Path;
|
|
40
|
+
The npm package ships prebuilt native addons as optional dependencies, so nothing is compiled on install:
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
| OS | Architectures |
|
|
43
|
+
| ------- | --------------------------------------------------------------- |
|
|
44
|
+
| Linux | x64 and arm64 (glibc and musl), armv7, ppc64le, s390x, riscv64 |
|
|
45
|
+
| macOS | arm64, x64 |
|
|
46
|
+
| Windows | x64, arm64, ia32 |
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
```
|
|
48
|
+
## Usage
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
### Node.js and Bun
|
|
43
51
|
|
|
44
52
|
```ts
|
|
45
|
-
import {
|
|
53
|
+
import { prepareMarkdownPage, renderMarkdown, renderMarkdownWithData } from "@mdrs/core";
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
isMdrsPath('docs/intro.mdrs'); // true
|
|
49
|
-
renderMarkdown('# Hello\n\nA **rich** document.');
|
|
50
|
-
```
|
|
55
|
+
const html = renderMarkdown("# Hello\n\nA **rich** document.");
|
|
51
56
|
|
|
52
|
-
|
|
57
|
+
const page = prepareMarkdownPage(source, { internalOrigins: ["https://example.com"] });
|
|
58
|
+
page.html; // the rendered HTML
|
|
59
|
+
page.headings; // [{ id, text, level }] for a table of contents
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
cargo run --example showcase_render -- docs/intro.mdrs > intro.html
|
|
61
|
+
renderMarkdownWithData("Hello ${name}!", JSON.stringify({ name: "Ada" }));
|
|
56
62
|
```
|
|
57
63
|
|
|
58
|
-
|
|
64
|
+
| Function | Returns |
|
|
65
|
+
| --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
|
|
66
|
+
| `renderMarkdown(source, options?)` | HTML |
|
|
67
|
+
| `prepareMarkdownPage(source, options?)` | `{ html, headings }` |
|
|
68
|
+
| `resolveDefines(source)` | The source with `:::define` variables substituted |
|
|
69
|
+
| `renderMarkdownWithData`, `prepareMarkdownPageWithData`, `resolveDefinesWithData` | The same, with a JSON string of values that expressions can read |
|
|
70
|
+
| `isMdrsPath(path)`, `FILE_EXTENSION` | Whether a path ends in `.mdrs`, and the extension itself |
|
|
59
71
|
|
|
60
|
-
|
|
61
|
-
src/
|
|
62
|
-
lib.rs public API
|
|
63
|
-
markdown.rs CommonMark / GFM rendering
|
|
64
|
-
containers.rs block directives (:::note, :::tabs, …)
|
|
65
|
-
define.rs :::define variables, expressions, conditionals
|
|
66
|
-
headings.rs heading ids and structure
|
|
67
|
-
sanitize.rs HTML sanitization
|
|
68
|
-
bindings/ Node.js (napi) and WebAssembly (wasm) glue
|
|
69
|
-
examples/ showcase_render: renders a .mdrs file to HTML
|
|
70
|
-
benches/ rendering baseline
|
|
71
|
-
scripts/ binding checks and parity tooling
|
|
72
|
-
index.js JavaScript entry (re-exports the generated native loader)
|
|
73
|
-
```
|
|
72
|
+
`internalOrigins` lists origins whose links open in the same tab; other links get `target="_blank"`. Invalid definitions and expressions, such as a variable a `:::define` block does not declare, throw an error that names the problem.
|
|
74
73
|
|
|
75
|
-
|
|
74
|
+
### Rust
|
|
76
75
|
|
|
77
|
-
|
|
76
|
+
```rust
|
|
77
|
+
use mdrs::{RenderOptions, prepare_markdown_page, render_markdown};
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
cargo test --locked # Rust tests
|
|
82
|
-
cargo check --features napi
|
|
83
|
-
cargo check --target wasm32-unknown-unknown --features wasm
|
|
84
|
-
bun run build:release # Node artifact for this platform
|
|
85
|
-
bun run test:bindings # offline binding checks
|
|
86
|
-
bun run test:native # build:release + test:bindings
|
|
79
|
+
let html = render_markdown("# Hello\n\nA **rich** document.")?;
|
|
80
|
+
let page = prepare_markdown_page("# Hello", &RenderOptions::default())?;
|
|
87
81
|
```
|
|
88
82
|
|
|
89
|
-
|
|
83
|
+
The Rust API mirrors the JavaScript one with snake_case names. Documents are limited to 500,000 bytes of input and 2,000,000 bytes of output.
|
|
90
84
|
|
|
91
|
-
|
|
85
|
+
## Output
|
|
92
86
|
|
|
93
|
-
|
|
94
|
-
bun scripts/showcase-parity.ts # compare the showcase with the TypeScript mirror (needs the idstead checkout)
|
|
95
|
-
cargo bench --locked --offline --bench render
|
|
96
|
-
```
|
|
87
|
+
The HTML is sanitized: raw HTML is reduced to a safe subset, event handlers are stripped and unsafe URLs are removed. Generated classes start with `mdrs-`. Math is typeset to KaTeX HTML on the server. Mermaid diagrams and charts are emitted as placeholders that carry their source, for a browser-side script to render.
|
|
97
88
|
|
|
98
|
-
|
|
89
|
+
## Limitations
|
|
99
90
|
|
|
100
|
-
|
|
91
|
+
The Rust renderer is a port of the TypeScript engine described in the syntax guide and does not match it byte for byte yet. Known gaps:
|
|
92
|
+
|
|
93
|
+
- GitHub alert blockquotes (`> [!NOTE]`) render as plain blockquotes; use `:::note` and the other callout directives.
|
|
94
|
+
- Live data (`get(url, path)`) and other asynchronous resolvers are not available, because the bindings are synchronous.
|
|
101
95
|
|
|
102
96
|
## Contributing
|
|
103
97
|
|
|
104
|
-
|
|
98
|
+
Bug reports and pull requests are welcome. [`CONTRIBUTING.md`](https://github.com/farhnkrnapratma/mdrs/blob/main/CONTRIBUTING.md) explains how to build, test and submit changes.
|
|
105
99
|
|
|
106
100
|
## License
|
|
107
101
|
|
|
108
|
-
Licensed under either of
|
|
109
|
-
|
|
110
|
-
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
|
|
111
|
-
- MIT License ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)
|
|
112
|
-
|
|
113
|
-
at your option.
|
|
114
|
-
|
|
115
|
-
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
116
|
-
for inclusion in this project by you, as defined in the Apache-2.0 license,
|
|
117
|
-
shall be dual licensed as above, without any additional terms or conditions.
|
|
118
|
-
|
|
119
|
-
Copyright © 2026 Farhan Kurnia Pratama
|
|
102
|
+
Licensed under either of the [Apache License, Version 2.0](https://github.com/farhnkrnapratma/mdrs/blob/main/LICENSE-APACHE) or the [MIT License](https://github.com/farhnkrnapratma/mdrs/blob/main/LICENSE-MIT), at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
package/index.d.ts
CHANGED
|
@@ -1,46 +1,49 @@
|
|
|
1
1
|
/** The file extension used by MDRS documents, without the leading dot. */
|
|
2
|
-
export declare const FILE_EXTENSION:
|
|
2
|
+
export declare const FILE_EXTENSION: "mdrs";
|
|
3
3
|
|
|
4
4
|
/** Returns `true` when `path` ends in the `.mdrs` extension (case-insensitive). */
|
|
5
5
|
export declare function isMdrsPath(path: string): boolean;
|
|
6
6
|
|
|
7
7
|
export interface RenderOptions {
|
|
8
|
-
|
|
8
|
+
internalOrigins?: string[];
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export interface HeadingItem {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
id: string;
|
|
13
|
+
text: string;
|
|
14
|
+
level: number;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface PreparedMarkdownPage {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
html: string;
|
|
19
|
+
headings: HeadingItem[];
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export declare function renderMarkdown(
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
markdown: string,
|
|
24
|
+
options?: RenderOptions,
|
|
25
25
|
): string;
|
|
26
26
|
|
|
27
27
|
export declare function renderMarkdownWithData(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
markdown: string,
|
|
29
|
+
dataJson: string,
|
|
30
|
+
options?: RenderOptions,
|
|
31
31
|
): string;
|
|
32
32
|
|
|
33
33
|
export declare function prepareMarkdownPage(
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
markdown: string,
|
|
35
|
+
options?: RenderOptions,
|
|
36
36
|
): PreparedMarkdownPage;
|
|
37
37
|
|
|
38
38
|
export declare function prepareMarkdownPageWithData(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
markdown: string,
|
|
40
|
+
dataJson: string,
|
|
41
|
+
options?: RenderOptions,
|
|
42
42
|
): PreparedMarkdownPage;
|
|
43
43
|
|
|
44
44
|
export declare function resolveDefines(markdown: string): string;
|
|
45
45
|
|
|
46
|
-
export declare function resolveDefinesWithData(
|
|
46
|
+
export declare function resolveDefinesWithData(
|
|
47
|
+
markdown: string,
|
|
48
|
+
dataJson: string,
|
|
49
|
+
): string;
|
package/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export const FILE_EXTENSION =
|
|
1
|
+
export const FILE_EXTENSION = "mdrs";
|
|
2
2
|
|
|
3
3
|
export function isMdrsPath(path) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const name = String(path).split(/[\\/]/).pop() ?? "";
|
|
5
|
+
const dot = name.lastIndexOf(".");
|
|
6
|
+
return dot > 0 && name.slice(dot + 1).toLowerCase() === FILE_EXTENSION;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from
|
|
10
|
+
prepareMarkdownPage,
|
|
11
|
+
prepareMarkdownPageWithData,
|
|
12
|
+
renderMarkdown,
|
|
13
|
+
renderMarkdownWithData,
|
|
14
|
+
resolveDefines,
|
|
15
|
+
resolveDefinesWithData,
|
|
16
|
+
} from "./index.native.js";
|
package/index.native.d.ts
CHANGED
|
@@ -7,33 +7,51 @@
|
|
|
7
7
|
* flavor napi-rs can build is listed, because `NAPI_RS_NATIVE_LIBRARY_PATH`
|
|
8
8
|
* can point the loader at a WASI artifact this package does not build itself.
|
|
9
9
|
*/
|
|
10
|
-
export declare const __napiBindingTarget:
|
|
10
|
+
export declare const __napiBindingTarget:
|
|
11
|
+
"native" | "wasm32-wasi" | "wasm32-wasip1";
|
|
11
12
|
|
|
12
13
|
export interface BindingHeadingItem {
|
|
13
|
-
id: string
|
|
14
|
-
text: string
|
|
15
|
-
level: number
|
|
14
|
+
id: string;
|
|
15
|
+
text: string;
|
|
16
|
+
level: number;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export interface BindingPreparedMarkdownPage {
|
|
19
|
-
html: string
|
|
20
|
-
headings: Array<BindingHeadingItem
|
|
20
|
+
html: string;
|
|
21
|
+
headings: Array<BindingHeadingItem>;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export interface BindingRenderOptions {
|
|
24
|
-
internalOrigins?: Array<string
|
|
25
|
+
internalOrigins?: Array<string>;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
export declare function isMdrsPath(path: string): boolean
|
|
28
|
+
export declare function isMdrsPath(path: string): boolean;
|
|
28
29
|
|
|
29
|
-
export declare function prepareMarkdownPage(
|
|
30
|
+
export declare function prepareMarkdownPage(
|
|
31
|
+
markdown: string,
|
|
32
|
+
options?: BindingRenderOptions | undefined | null,
|
|
33
|
+
): BindingPreparedMarkdownPage;
|
|
30
34
|
|
|
31
|
-
export declare function prepareMarkdownPageWithData(
|
|
35
|
+
export declare function prepareMarkdownPageWithData(
|
|
36
|
+
markdown: string,
|
|
37
|
+
dataJson: string,
|
|
38
|
+
options?: BindingRenderOptions | undefined | null,
|
|
39
|
+
): BindingPreparedMarkdownPage;
|
|
32
40
|
|
|
33
|
-
export declare function renderMarkdown(
|
|
41
|
+
export declare function renderMarkdown(
|
|
42
|
+
markdown: string,
|
|
43
|
+
options?: BindingRenderOptions | undefined | null,
|
|
44
|
+
): string;
|
|
34
45
|
|
|
35
|
-
export declare function renderMarkdownWithData(
|
|
46
|
+
export declare function renderMarkdownWithData(
|
|
47
|
+
markdown: string,
|
|
48
|
+
dataJson: string,
|
|
49
|
+
options?: BindingRenderOptions | undefined | null,
|
|
50
|
+
): string;
|
|
36
51
|
|
|
37
|
-
export declare function resolveDefines(markdown: string): string
|
|
52
|
+
export declare function resolveDefines(markdown: string): string;
|
|
38
53
|
|
|
39
|
-
export declare function resolveDefinesWithData(
|
|
54
|
+
export declare function resolveDefinesWithData(
|
|
55
|
+
markdown: string,
|
|
56
|
+
dataJson: string,
|
|
57
|
+
): string;
|