@primate/markdown 0.1.1 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/markdown",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Primate Markdown module",
5
5
  "homepage": "https://primatejs.com/modules/markdown",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "marked": "^7.0.4",
20
- "runtime-compat": "^0.25.7"
20
+ "runtime-compat": "^0.25.8"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "primate": "0.22"
package/src/compile.js CHANGED
@@ -1,39 +1,24 @@
1
1
  import {marked} from "marked";
2
2
 
3
3
  const identity_heading = (text, level) => `<h${level}>${text}</h${level}>`;
4
+ const heading = options => options?.renderer?.heading ?? identity_heading;
4
5
 
5
- export default async ({
6
- app,
7
- directory,
8
- options,
9
- }) => {
10
- const {location} = app.config;
11
- const monkeyed_heading = options?.renderer?.heading ?? identity_heading;
12
- const source = app.runpath(directory);
13
- const target = app.runpath(location.server, directory);
14
- await target.file.create();
15
-
16
- return async (filename, input) => {
17
- const toc = [];
18
- const renderer = {
19
- ...options?.renderer ?? {},
20
- heading(text, level) {
21
- const name = text.toLowerCase().replaceAll(/[?{}%]/gu, "");
22
- toc.push({level, text, name});
23
- return monkeyed_heading(text, level);
24
- },
25
- };
26
- marked.use({
27
- ...options,
28
- renderer,
29
- });
6
+ export default (input, options) => {
7
+ const toc = [];
8
+ const renderer = {
9
+ ...options?.renderer ?? {},
10
+ heading(text, level) {
11
+ toc.push({text, level,
12
+ name: text.toLowerCase()
13
+ .replaceAll(/[?{}%]/gu, "")
14
+ .replace(/[^\w]+/gu, "-"),
15
+ });
16
+ return heading(options)(text, level);
17
+ },
18
+ };
19
+ marked.use({...options, renderer});
30
20
 
31
- const content = marked.parse(input);
32
- const html = target.join(`${filename}.html`.replace(source, ""));
33
- await html.directory.file.create();
34
- await html.file.write(content);
21
+ const content = marked.parse(input);
35
22
 
36
- const json = target.join(`${filename}.json`.replace(source, ""));
37
- await json.file.write(JSON.stringify(toc));
38
- };
23
+ return {content, toc};
39
24
  };
package/src/module.js CHANGED
@@ -1,19 +1,18 @@
1
1
  import {Response, Status, MediaType} from "runtime-compat/http";
2
- import precompile from "./compile.js";
2
+ import {stringify} from "runtime-compat/object";
3
+ import compile from "./compile.js";
3
4
 
4
- const respond = (handler, directory) => (...[name, ...rest]) => async app => {
5
- const base = app.runpath(app.config.location.server, directory);
6
- const content = await base.join(`${name}.html`).text();
7
- const toc = await base.join(`${name}.json`).json();
5
+ const respond = (handler, directory) => (...[name, ...rest]) =>
6
+ async (app, ...noapp) => {
7
+ const base = app.runpath(app.config.location.server, directory);
8
+ const content = await base.join(`${name}.html`).text();
9
+ const toc = await base.join(`${name}.json`).json();
8
10
 
9
- return handler({
10
- content,
11
- toc,
12
- }, ...rest);
13
- };
11
+ return handler({content, toc}, ...rest)(app, ...noapp);
12
+ };
14
13
 
15
- const as_html = app => async ({content}, _, {status = Status.OK} = {}) =>
16
- new Response(await app.render({body: content}), {
14
+ const as_html = ({content}, _, {status = Status.OK, page} = {}) => async app =>
15
+ new Response(await app.render({body: content, page}), {
17
16
  status,
18
17
  headers: {...await app.headers(), "Content-Type": MediaType.TEXT_HTML},
19
18
  });
@@ -26,7 +25,6 @@ export default ({
26
25
  } = {}) => {
27
26
  const env = {};
28
27
  const re = new RegExp(`^.*.(?:${extension})$`, "u");
29
- let compile;
30
28
 
31
29
  return {
32
30
  name: "primate:markdown",
@@ -36,7 +34,7 @@ export default ({
36
34
  return next(app);
37
35
  },
38
36
  register(app, next) {
39
- app.register(extension, respond(handler ?? as_html(app), env.directory));
37
+ app.register(extension, respond(handler ?? as_html, env.directory));
40
38
  return next(app);
41
39
  },
42
40
  async compile(app, next) {
@@ -49,19 +47,19 @@ export default ({
49
47
  const target = app.runpath(location.server, env.directory);
50
48
  await target.file.create();
51
49
 
52
- compile = await precompile({
53
- app,
54
- directory: env.directory,
55
- options,
56
- });
50
+ await Promise.all(components.map(async component => {
51
+ const filename = component.path;
52
+ const {content, toc} = compile(await component.text(), options);
57
53
 
58
- await Promise.all(components.map(async component =>
59
- compile(component.path, await component.file.read())));
54
+ const html = target.join(`${filename}.html`.replace(source, ""));
55
+ await html.directory.file.create();
56
+ await html.file.write(content);
57
+
58
+ const json = target.join(`${filename}.json`.replace(source, ""));
59
+ await json.file.write(stringify(toc));
60
+ }));
60
61
 
61
62
  return next(app);
62
63
  },
63
- async handle(request, next) {
64
- return next({...request, markdown: {compile}});
65
- },
66
64
  };
67
65
  };