@primate/markdown 0.8.0 → 0.10.0

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.
@@ -0,0 +1,10 @@
1
+ declare const _default: (input?: import("@rcompat/type").Unpack<({
2
+ extensions?: string[] | undefined;
3
+ spa?: boolean | undefined;
4
+ ssr?: boolean | undefined;
5
+ } & {
6
+ marked?: import("marked").MarkedExtension<string, string> | undefined;
7
+ pretransform?: ((text: string) => import("@rcompat/type").MaybePromise<string>) | undefined;
8
+ }) | undefined>) => import("@primate/core/frontend").Module;
9
+ export default _default;
10
+ //# sourceMappingURL=default.d.ts.map
@@ -1,4 +1,5 @@
1
- import Runtime from "#Runtime";
1
+ import init from "#init";
2
+ import frontend from "@primate/core/frontend";
2
3
  import fs from "@rcompat/fs";
3
4
  import string from "@rcompat/string";
4
5
  import { marked } from "marked";
@@ -11,7 +12,7 @@ function slugify(text) {
11
12
  function frontmatter(src) {
12
13
  const input = src.replace(/^\uFEFF/, "");
13
14
  const match = input.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?/);
14
- if (!match)
15
+ if (match === null)
15
16
  return {
16
17
  body: input,
17
18
  meta: null,
@@ -54,12 +55,28 @@ function frontmatter(src) {
54
55
  return { body, meta };
55
56
  }
56
57
  ;
57
- export default class Default extends Runtime {
58
- compile = {
59
- server: async (_, file) => `export { default } from "markdown:${file.path}";`,
60
- };
61
- async build(app, next) {
62
- const { pretransform } = this;
58
+ function toc(body) {
59
+ return marked
60
+ .lexer(body)
61
+ .filter(token => token.type === "heading")
62
+ .map(token => {
63
+ const heading = token;
64
+ return {
65
+ depth: heading.depth,
66
+ slug: slugify(heading.text),
67
+ text: heading.text,
68
+ };
69
+ });
70
+ }
71
+ export default frontend({
72
+ ...init,
73
+ schema: init.schema,
74
+ onBuild(app, options) {
75
+ const pretransform = options.pretransform ?? ((m) => m);
76
+ if (options.marked !== undefined) {
77
+ const renderer = { ...options.marked.renderer ?? {} };
78
+ marked.use({ ...options.marked, renderer });
79
+ }
63
80
  app.plugin("server", {
64
81
  name: "markdown",
65
82
  setup(build) {
@@ -70,21 +87,13 @@ export default class Default extends Runtime {
70
87
  build.onLoad({ filter: /.*/, namespace: "markdown" }, async (args) => {
71
88
  const text = await fs.text(args.path);
72
89
  const { body, meta } = frontmatter(await pretransform(text));
73
- const tokens = marked.lexer(body);
74
- const toc = tokens
75
- .filter(token => token.type === "heading")
76
- .map(token => ({
77
- depth: token.depth,
78
- slug: slugify(token.text),
79
- text: token.text,
80
- }));
81
90
  return {
82
91
  contents: string.dedent `
83
92
  export default {
84
93
  md: ${JSON.stringify(body)},
85
94
  html: ${JSON.stringify(await marked.parse(body))},
86
- toc: JSON.parse(${JSON.stringify(JSON.stringify(toc))}),
87
- meta: ${meta ? JSON.stringify(meta) : "null"},
95
+ toc: JSON.parse(${JSON.stringify(JSON.stringify(toc(body)))}),
96
+ meta: ${meta !== null ? JSON.stringify(meta) : "null"},
88
97
  };
89
98
  `,
90
99
  loader: "js",
@@ -93,7 +102,9 @@ export default class Default extends Runtime {
93
102
  });
94
103
  },
95
104
  });
96
- return super.build(app, next);
97
- }
98
- }
99
- //# sourceMappingURL=Default.js.map
105
+ },
106
+ compile: {
107
+ server: async (_, file) => `export { default } from "markdown:${file.path}";`,
108
+ },
109
+ });
110
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1,16 @@
1
+ import type { Init } from "@primate/core/frontend";
2
+ import type { MaybePromise } from "@rcompat/type";
3
+ import type { MarkedExtension } from "marked";
4
+ type Pretransform = (text: string) => MaybePromise<string>;
5
+ declare const schema: import("pema").ObjectType<import("pema").NormalizeSchemaObject<{
6
+ readonly marked: import("pema").OptionalType<import("pema").PureType<MarkedExtension<string, string>, "PureType">>;
7
+ readonly pretransform: import("pema").OptionalType<import("pema").PureType<Pretransform, "PureType">>;
8
+ }>, {
9
+ marked: MarkedExtension<string, string> | undefined;
10
+ pretransform: Pretransform | undefined;
11
+ }>;
12
+ declare const init: Init<{
13
+ html: string;
14
+ }, typeof schema>;
15
+ export default init;
16
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1,18 @@
1
+ import p from "pema";
2
+ const schema = p({
3
+ marked: p.pure().optional(),
4
+ pretransform: p.pure().optional(),
5
+ });
6
+ function render(component) {
7
+ return { body: component.html };
8
+ }
9
+ const init = {
10
+ name: "markdown",
11
+ extensions: [".md"],
12
+ layouts: false,
13
+ client: false,
14
+ render,
15
+ schema,
16
+ };
17
+ export default init;
18
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1,10 @@
1
+ declare const _default: (input?: import("@rcompat/type").Unpack<({
2
+ extensions?: string[] | undefined;
3
+ spa?: boolean | undefined;
4
+ ssr?: boolean | undefined;
5
+ } & {
6
+ marked?: import("marked").MarkedExtension<string, string> | undefined;
7
+ pretransform?: ((text: string) => import("@rcompat/type").MaybePromise<string>) | undefined;
8
+ }) | undefined>) => import("@primate/core/frontend").Module;
9
+ export default _default;
10
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1,4 @@
1
+ import init from "#init";
2
+ import frontend from "@primate/core/frontend";
3
+ export default frontend(init);
4
+ //# sourceMappingURL=runtime.js.map
@@ -1,4 +1,3 @@
1
- import Default from "#Default";
2
- declare const _default: (config: typeof Default.input) => Default;
3
- export default _default;
1
+ export { default } from "#default";
2
+ export type { default as Component } from "#Component";
4
3
  //# sourceMappingURL=default.d.ts.map
@@ -1,3 +1,2 @@
1
- import Default from "#Default";
2
- export default (config) => new Default(config);
1
+ export { default } from "#default";
3
2
  //# sourceMappingURL=default.js.map
@@ -1,4 +1,3 @@
1
- import Runtime from "#Runtime";
2
- declare const _default: (config: typeof Runtime.input) => Runtime;
3
- export default _default;
1
+ export { default } from "#runtime";
2
+ export type { default as Component } from "#Component";
4
3
  //# sourceMappingURL=runtime.d.ts.map
@@ -1,3 +1,2 @@
1
- import Runtime from "#Runtime";
2
- export default (config) => new Runtime(config);
1
+ export { default } from "#runtime";
3
2
  //# sourceMappingURL=runtime.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/markdown",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Markdown for Primate",
5
5
  "homepage": "https://primate.run/docs/frontend/markdown",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",
@@ -17,21 +17,22 @@
17
17
  "!/**/*.spec.*"
18
18
  ],
19
19
  "dependencies": {
20
- "@rcompat/assert": "^0.6.0",
21
- "@rcompat/string": "^0.13.0",
22
- "marked": "^17.0.2",
23
- "@primate/core": "^0.5.0",
24
- "pema": "^0.5.0"
20
+ "@rcompat/assert": "^0.8.1",
21
+ "@rcompat/string": "^0.16.1",
22
+ "marked": "^17.0.5",
23
+ "pema": "^0.6.0",
24
+ "@primate/core": "^0.6.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@rcompat/type": "^0.9.0"
27
+ "@rcompat/type": "^0.11.1",
28
+ "@primate/test": "^0.0.0"
28
29
  },
29
30
  "peerDependencies": {
30
- "primate": "^0.36.0"
31
+ "primate": "^0.37.0"
31
32
  },
32
33
  "imports": {
33
34
  "#*": {
34
- "apekit": "./src/private/*.ts",
35
+ "@primate/source": "./src/private/*.ts",
35
36
  "default": "./lib/private/*.js"
36
37
  }
37
38
  },
@@ -42,13 +43,14 @@
42
43
  "default": "./lib/public/default.js"
43
44
  },
44
45
  "./*": {
45
- "apekit": "./src/public/*.ts",
46
+ "@primate/source": "./src/public/*.ts",
46
47
  "default": "./lib/public/*.js"
47
48
  }
48
49
  },
49
50
  "scripts": {
50
51
  "build": "npm run clean && tsc",
51
52
  "clean": "rm -rf ./lib",
52
- "lint": "eslint ."
53
+ "lint": "eslint .",
54
+ "test": "npm run build && npx proby"
53
55
  }
54
56
  }
@@ -1,11 +0,0 @@
1
- import Runtime from "#Runtime";
2
- import type BuildApp from "@primate/core/BuildApp";
3
- import type NextBuild from "@primate/core/NextBuild";
4
- import type { FileRef } from "@rcompat/fs";
5
- export default class Default extends Runtime {
6
- compile: {
7
- server: (_: string, file: FileRef) => Promise<string>;
8
- };
9
- build(app: BuildApp, next: NextBuild): Promise<BuildApp>;
10
- }
11
- //# sourceMappingURL=Default.d.ts.map
@@ -1,41 +0,0 @@
1
- import type Component from "#Component";
2
- import FrontendModule from "@primate/core/frontend/Module";
3
- import type { MaybePromise } from "@rcompat/type";
4
- import type { MarkedExtension } from "marked";
5
- declare function render(component: Component): {
6
- body: string;
7
- };
8
- type Pretransform = (text: string) => MaybePromise<string>;
9
- export default class Runtime extends FrontendModule<Component> {
10
- #private;
11
- name: string;
12
- defaultExtensions: string[];
13
- layouts: boolean;
14
- client: boolean;
15
- render: typeof render;
16
- static schema: import("pema").ObjectType<{
17
- fileExtensions: import("pema").OptionalType<import("pema").ArrayType<import("pema").StringType>>;
18
- marked: import("pema").OptionalType<import("pema").PureType<MarkedExtension<string, string>, "PureType">>;
19
- pretransform: import("pema").OptionalType<import("pema").PureType<Pretransform, "PureType">>;
20
- spa: import("pema").DefaultType<import("pema").BooleanType, true>;
21
- ssr: import("pema").DefaultType<import("pema").BooleanType, true>;
22
- }>;
23
- static options: {
24
- fileExtensions: string[] | undefined;
25
- marked: MarkedExtension<string, string> | undefined;
26
- pretransform: Pretransform | undefined;
27
- spa: boolean;
28
- ssr: boolean;
29
- };
30
- static input: {
31
- fileExtensions?: string[] | undefined;
32
- marked?: MarkedExtension<string, string> | undefined;
33
- pretransform?: Pretransform | undefined;
34
- spa?: boolean | undefined;
35
- ssr?: boolean | undefined;
36
- } | undefined;
37
- constructor(config?: typeof Runtime.input);
38
- get pretransform(): Pretransform;
39
- }
40
- export {};
41
- //# sourceMappingURL=Runtime.d.ts.map
@@ -1,37 +0,0 @@
1
- import FrontendModule from "@primate/core/frontend/Module";
2
- import assert from "@rcompat/assert";
3
- import { marked } from "marked";
4
- import p from "pema";
5
- function render(component) {
6
- return { body: component.html };
7
- }
8
- export default class Runtime extends FrontendModule {
9
- name = "markdown";
10
- defaultExtensions = [".md"];
11
- layouts = false;
12
- client = false;
13
- render = render;
14
- #pretransform;
15
- static schema = p({
16
- fileExtensions: p.array(p.string).optional(),
17
- marked: p.pure().optional(),
18
- pretransform: p.pure().optional(),
19
- spa: p.boolean.default(true),
20
- ssr: p.boolean.default(true),
21
- });
22
- static options = Runtime.schema.infer;
23
- static input = Runtime.schema.input;
24
- constructor(config = {}) {
25
- const { marked: marked_options, pretransform, ...super_config } = config;
26
- super(super_config);
27
- assert.maybe.dict(marked_options);
28
- assert.maybe.function(pretransform);
29
- this.#pretransform = (pretransform ?? ((m) => m));
30
- const renderer = { ...marked_options?.renderer ?? {} };
31
- marked.use({ ...marked_options, renderer });
32
- }
33
- get pretransform() {
34
- return this.#pretransform;
35
- }
36
- }
37
- //# sourceMappingURL=Runtime.js.map
@@ -1,2 +0,0 @@
1
- export { default } from "#Component";
2
- //# sourceMappingURL=Component.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=Component.js.map