@weborigami/origami 0.5.4 → 0.5.6

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.
Files changed (131) hide show
  1. package/index.ts +0 -4
  2. package/main.js +2 -9
  3. package/package.json +4 -5
  4. package/src/cli/cli.js +10 -8
  5. package/src/common/documentObject.js +3 -3
  6. package/src/common/loadJsDom.js +13 -0
  7. package/src/common/utilities.d.ts +0 -2
  8. package/src/common/utilities.js +1 -55
  9. package/src/dev/ExplorableSiteTransform.js +2 -6
  10. package/src/dev/OriCommandTransform.js +25 -7
  11. package/src/dev/changes.js +10 -5
  12. package/src/dev/code.js +1 -6
  13. package/src/dev/copy.js +4 -8
  14. package/src/dev/crawler/audit.js +8 -8
  15. package/src/dev/crawler/crawl.js +3 -5
  16. package/src/dev/crawler/findPaths.js +9 -3
  17. package/src/dev/crawler/pathsInHtml.js +4 -3
  18. package/src/dev/debug.js +4 -7
  19. package/src/dev/dev.js +4 -2
  20. package/src/dev/explore.js +17 -39
  21. package/src/dev/help.js +0 -4
  22. package/src/dev/help.yaml +18 -14
  23. package/src/dev/log.js +1 -2
  24. package/src/dev/serve.js +5 -18
  25. package/src/dev/svg.js +5 -5
  26. package/src/dev/treeDot.js +5 -6
  27. package/src/dev/watch.js +8 -12
  28. package/src/initializeBuiltins.js +23 -0
  29. package/src/origami/csv.js +1 -5
  30. package/src/origami/document.js +2 -5
  31. package/src/origami/fetch.js +4 -8
  32. package/src/origami/htmlDom.js +3 -2
  33. package/src/origami/image/format.js +0 -5
  34. package/src/origami/image/resize.js +0 -3
  35. package/src/origami/indexPage.js +4 -4
  36. package/src/origami/inline.js +4 -10
  37. package/src/origami/json.js +2 -6
  38. package/src/origami/jsonKeys.js +4 -10
  39. package/src/origami/jsonParse.js +1 -1
  40. package/src/origami/mdHtml.js +5 -9
  41. package/src/origami/mdOutline.js +3 -3
  42. package/src/origami/once.js +3 -7
  43. package/src/origami/ori.js +21 -24
  44. package/src/origami/origami.js +1 -4
  45. package/src/origami/pack.js +0 -5
  46. package/src/origami/post.js +4 -3
  47. package/src/origami/rss.js +7 -8
  48. package/src/origami/sitemap.js +7 -9
  49. package/src/origami/static.js +7 -9
  50. package/src/origami/string.js +1 -14
  51. package/src/origami/unpack.js +0 -5
  52. package/src/origami/yaml.js +1 -5
  53. package/src/origami/yamlParse.js +1 -1
  54. package/src/server/constructResponse.js +3 -3
  55. package/src/server/server.js +8 -59
  56. package/src/builtinsProgram.js +0 -65
  57. package/src/builtinsShell.js +0 -18
  58. package/src/cli/getConfig.js +0 -11
  59. package/src/common/ConstantTree.js +0 -18
  60. package/src/common/constructHref.js +0 -20
  61. package/src/common/constructSiteTree.js +0 -34
  62. package/src/common/fetchAndHandleExtension.js +0 -27
  63. package/src/handlers/css.handler.js +0 -7
  64. package/src/handlers/csv.handler.js +0 -126
  65. package/src/handlers/handlerBuiltins.js +0 -27
  66. package/src/handlers/handlers.js +0 -33
  67. package/src/handlers/htm.handler.js +0 -2
  68. package/src/handlers/html.handler.js +0 -7
  69. package/src/handlers/jpeg.handler.js +0 -62
  70. package/src/handlers/jpg.handler.js +0 -2
  71. package/src/handlers/js.handler.js +0 -20
  72. package/src/handlers/json.handler.js +0 -27
  73. package/src/handlers/md.handler.js +0 -7
  74. package/src/handlers/mjs.handler.js +0 -2
  75. package/src/handlers/ori.handler.js +0 -55
  76. package/src/handlers/oridocument.handler.js +0 -78
  77. package/src/handlers/parseFrontMatter.js +0 -16
  78. package/src/handlers/processUnpackedContent.js +0 -35
  79. package/src/handlers/ts.handler.js +0 -1
  80. package/src/handlers/txt.handler.js +0 -91
  81. package/src/handlers/wasm.handler.js +0 -17
  82. package/src/handlers/xhtml.handler.js +0 -2
  83. package/src/handlers/yaml.handler.js +0 -36
  84. package/src/handlers/yml.handler.js +0 -2
  85. package/src/origami/config.js +0 -18
  86. package/src/origami/project.js +0 -111
  87. package/src/protocols/explore.js +0 -19
  88. package/src/protocols/files.js +0 -31
  89. package/src/protocols/http.js +0 -18
  90. package/src/protocols/https.js +0 -18
  91. package/src/protocols/httpstree.js +0 -19
  92. package/src/protocols/httptree.js +0 -19
  93. package/src/protocols/js.js +0 -13
  94. package/src/protocols/node.js +0 -13
  95. package/src/protocols/package.js +0 -70
  96. package/src/tree/addNextPrevious.js +0 -22
  97. package/src/tree/cache.js +0 -22
  98. package/src/tree/calendar.js +0 -1
  99. package/src/tree/clear.js +0 -19
  100. package/src/tree/concat.js +0 -17
  101. package/src/tree/constant.js +0 -1
  102. package/src/tree/deepMap.js +0 -32
  103. package/src/tree/deepMerge.js +0 -18
  104. package/src/tree/deepReverse.js +0 -23
  105. package/src/tree/deepTake.js +0 -26
  106. package/src/tree/deepValues.js +0 -22
  107. package/src/tree/defineds.js +0 -30
  108. package/src/tree/filter.js +0 -19
  109. package/src/tree/first.js +0 -19
  110. package/src/tree/fromFn.js +0 -29
  111. package/src/tree/globKeys.js +0 -19
  112. package/src/tree/group.js +0 -26
  113. package/src/tree/inners.js +0 -30
  114. package/src/tree/keys.js +0 -15
  115. package/src/tree/length.js +0 -15
  116. package/src/tree/map.d.ts +0 -11
  117. package/src/tree/map.js +0 -125
  118. package/src/tree/mask.js +0 -19
  119. package/src/tree/match.js +0 -79
  120. package/src/tree/merge.js +0 -41
  121. package/src/tree/paginate.js +0 -20
  122. package/src/tree/parent.js +0 -15
  123. package/src/tree/plain.js +0 -15
  124. package/src/tree/regExpKeys.js +0 -19
  125. package/src/tree/reverse.js +0 -17
  126. package/src/tree/setDeep.js +0 -49
  127. package/src/tree/shuffle.js +0 -57
  128. package/src/tree/sort.js +0 -52
  129. package/src/tree/take.js +0 -19
  130. package/src/tree/tree.js +0 -52
  131. package/src/tree/values.js +0 -15
@@ -1,126 +0,0 @@
1
- import { symbols, toString } from "@weborigami/async-tree";
2
-
3
- export default {
4
- mediaType: "text/csv",
5
-
6
- unpack(packed, options = {}) {
7
- const parent = options.parent ?? null;
8
- const text = toString(packed);
9
- const data = csvParse(text);
10
- // Define `parent` as non-enumerable property
11
- Object.defineProperty(data, symbols.parent, {
12
- configurable: true,
13
- enumerable: false,
14
- value: parent,
15
- writable: true,
16
- });
17
- return data;
18
- },
19
- };
20
-
21
- /**
22
- * Parse text as CSV following RFC 4180
23
- *
24
- * This assumes the presence of a header row, and accepts both CRLF and LF line
25
- * endings.
26
- *
27
- * @param {string} text
28
- * @returns {any[]}
29
- */
30
- function csvParse(text) {
31
- const rows = [];
32
- let currentRow = [];
33
- let currentField = "";
34
-
35
- const pushField = () => {
36
- // Push the completed field and reset for the next field.
37
- currentRow.push(currentField);
38
- currentField = "";
39
- };
40
-
41
- const pushRow = () => {
42
- // Push the row if there is at least one field (accounts for potential trailing newline)
43
- rows.push(currentRow);
44
- currentRow = [];
45
- };
46
-
47
- // Main state machine
48
- let i = 0;
49
- let inQuotes = false;
50
- while (i < text.length) {
51
- const char = text[i];
52
-
53
- if (inQuotes) {
54
- // In a quoted field
55
- if (char === '"') {
56
- // Check if next character is also a quote
57
- if (i + 1 < text.length && text[i + 1] === '"') {
58
- // Append a literal double quote and skip the next character
59
- currentField += '"';
60
- i += 2;
61
- continue;
62
- } else {
63
- // End of the quoted field
64
- inQuotes = false;
65
- i++;
66
- continue;
67
- }
68
- } else {
69
- // All other characters within quotes are taken literally.
70
- currentField += char;
71
- i++;
72
- continue;
73
- }
74
- } else if (char === '"') {
75
- // Start of a quoted field
76
- inQuotes = true;
77
- i++;
78
- continue;
79
- } else if (char === ",") {
80
- // End of field
81
- pushField();
82
- i++;
83
- continue;
84
- } else if (char === "\n" || (char === "\r" && text[i + 1] === "\n")) {
85
- // End of row: push the last field, then row.
86
- pushField();
87
- pushRow();
88
- if (char === "\r" && text[i + 1] === "\n") {
89
- i++; // Handle CRLF line endings
90
- }
91
- i++;
92
- continue;
93
- } else {
94
- // Regular character
95
- currentField += char;
96
- i++;
97
- continue;
98
- }
99
- }
100
-
101
- // Handle any remaining data after the loop.
102
- // This will capture the last field/row if the text did not end with a newline.
103
- if (inQuotes) {
104
- // Mismatched quotes: you might choose to throw an error or handle it gracefully.
105
- throw new Error("CSV parsing error: unmatched quote in the input.");
106
- }
107
- if (currentField !== "" || text.at(-1) === ",") {
108
- pushField();
109
- }
110
- if (currentRow.length > 0) {
111
- pushRow();
112
- }
113
-
114
- // The first row is assumed to be the header.
115
- if (rows.length === 0) {
116
- return [];
117
- }
118
-
119
- const header = rows.shift();
120
-
121
- const data = rows.map((row) =>
122
- Object.fromEntries(row.map((value, index) => [header[index], value]))
123
- );
124
-
125
- return data;
126
- }
@@ -1,27 +0,0 @@
1
- import * as handlers from "./handlers.js";
2
-
3
- export default function handlerBuiltins() {
4
- return {
5
- "css.handler": handlers.cssHandler,
6
- "csv.handler": handlers.csvHandler,
7
- "htm.handler": handlers.htmHandler,
8
- "html.handler": handlers.htmlHandler,
9
- "jpeg.handler": handlers.jpegHandler,
10
- "jpg.handler": handlers.jpgHandler,
11
- "js.handler": handlers.jsHandler,
12
- // TODO: Remove deprecated JSE extensions
13
- "jse.handler": handlers.oriHandler,
14
- "jsedocument.handler": handlers.oridocumentHandler,
15
- "json.handler": handlers.jsonHandler,
16
- "md.handler": handlers.mdHandler,
17
- "mjs.handler": handlers.mjsHandler,
18
- "ori.handler": handlers.oriHandler,
19
- "oridocument.handler": handlers.oridocumentHandler,
20
- "ts.handler": handlers.tsHandler,
21
- "txt.handler": handlers.txtHandler,
22
- "wasm.handler": handlers.wasmHandler,
23
- "xhtml.handler": handlers.xhtmlHandler,
24
- "yaml.handler": handlers.yamlHandler,
25
- "yml.handler": handlers.ymlHandler,
26
- };
27
- }
@@ -1,33 +0,0 @@
1
- //
2
- // This library includes a number of modules with circular dependencies. This
3
- // module exists to explicitly set the loading order for those modules. To
4
- // enforce use of this loading order, other modules should only load the modules
5
- // below via this module.
6
- //
7
- // About this pattern:
8
- // https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de
9
- //
10
- // Note: to avoid having VS Code auto-sort the imports, keep lines between them.
11
-
12
- export { default as jsHandler } from "./js.handler.js";
13
- export { default as tsHandler } from "./ts.handler.js";
14
-
15
- export { default as oriHandler } from "./ori.handler.js";
16
-
17
- export { default as oridocumentHandler } from "./oridocument.handler.js";
18
-
19
- export { default as txtHandler } from "./txt.handler.js";
20
-
21
- export { default as cssHandler } from "./css.handler.js";
22
- export { default as csvHandler } from "./csv.handler.js";
23
- export { default as htmHandler } from "./htm.handler.js";
24
- export { default as htmlHandler } from "./html.handler.js";
25
- export { default as jpegHandler } from "./jpeg.handler.js";
26
- export { default as jpgHandler } from "./jpg.handler.js";
27
- export { default as jsonHandler } from "./json.handler.js";
28
- export { default as mdHandler } from "./md.handler.js";
29
- export { default as mjsHandler } from "./mjs.handler.js";
30
- export { default as wasmHandler } from "./wasm.handler.js";
31
- export { default as xhtmlHandler } from "./xhtml.handler.js";
32
- export { default as yamlHandler } from "./yaml.handler.js";
33
- export { default as ymlHandler } from "./yml.handler.js";
@@ -1,2 +0,0 @@
1
- // .htm is a synonynm for .html
2
- export { htmlHandler as default } from "./handlers.js";
@@ -1,7 +0,0 @@
1
- // .html files use the .txt loader
2
- import { txtHandler } from "./handlers.js";
3
-
4
- export default {
5
- ...txtHandler,
6
- mediaType: "text/html",
7
- };
@@ -1,62 +0,0 @@
1
- import exifParser from "exif-parser";
2
-
3
- const exifDateTags = [
4
- "ModifyDate",
5
- "MDPrepDate",
6
- "DateTimeOriginal",
7
- "CreateDate",
8
- "PreviewDateTime",
9
- "GPSDateStamp",
10
- ];
11
-
12
- /**
13
- * A JPEG file with possible Exif metadata
14
- */
15
- export default {
16
- mediaType: "image/jpeg",
17
-
18
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
19
- async unpack(packed, options) {
20
- if (packed instanceof Uint8Array) {
21
- // Downgrade to old Node Buffer for exif-parser.
22
- packed = Buffer.from(packed);
23
- }
24
- const parser = exifParser.create(packed);
25
- parser.enableTagNames(true);
26
- parser.enableSimpleValues(true);
27
- const parsed = await parser.parse();
28
-
29
- // The exif-parser `enableSimpleValues` option should convert dates to
30
- // JavaScript Date objects, but that doesn't seem to work. Ensure dates are
31
- // Date objects.
32
- const exif = parsed.tags;
33
- for (const tag of exifDateTags) {
34
- if (typeof exif[tag] === "number") {
35
- exif[tag] = new Date(exif[tag] * 1000);
36
- }
37
- }
38
-
39
- const result = {
40
- height: parsed.imageSize.height,
41
- width: parsed.imageSize.width,
42
- exif,
43
- };
44
-
45
- // Promote some Exif properties to the top level.
46
- const tagsToPromote = {
47
- ImageDescription: "caption",
48
- ModifyDate: "modified",
49
- Orientation: "orientation",
50
- };
51
- for (const [tag, key] of Object.entries(tagsToPromote)) {
52
- if (exif[tag] !== undefined) {
53
- result[key] = exif[tag];
54
- }
55
- }
56
-
57
- // Add aspect ratio for use with `aspect-ratio` CSS.
58
- result.aspectRatio = result.width / result.height;
59
-
60
- return result;
61
- },
62
- };
@@ -1,2 +0,0 @@
1
- // .jpg is a synonym for .jpeg
2
- export { jpegHandler as default } from "./handlers.js";
@@ -1,20 +0,0 @@
1
- import processUnpackedContent from "./processUnpackedContent.js";
2
-
3
- /**
4
- * A JavaScript file
5
- *
6
- * Unpacking a JavaScript file returns its default export, or its set of exports
7
- * if there is more than one.
8
- */
9
- export default {
10
- mediaType: "application/javascript",
11
-
12
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
13
- async unpack(packed, options = {}) {
14
- const { key, parent } = options;
15
- if (parent && "import" in parent) {
16
- const content = await /** @type {any} */ (parent).import?.(key);
17
- return processUnpackedContent(content, parent);
18
- }
19
- },
20
- };
@@ -1,27 +0,0 @@
1
- import { symbols } from "@weborigami/async-tree";
2
- import * as utilities from "../common/utilities.js";
3
-
4
- /**
5
- * A JSON file
6
- *
7
- * Unpacking a JSON file returns the parsed data.
8
- */
9
- export default {
10
- mediaType: "application/json",
11
-
12
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
13
- unpack(packed) {
14
- const json = utilities.toString(packed);
15
- if (!json) {
16
- throw new Error("Tried to parse something as JSON but it wasn't text.");
17
- }
18
- const data = JSON.parse(json);
19
- if (data && typeof data === "object" && Object.isExtensible(data)) {
20
- Object.defineProperty(data, symbols.deep, {
21
- enumerable: false,
22
- value: true,
23
- });
24
- }
25
- return data;
26
- },
27
- };
@@ -1,7 +0,0 @@
1
- // .md files use the .txt loader
2
- import { txtHandler } from "./handlers.js";
3
-
4
- export default {
5
- ...txtHandler,
6
- mediaType: "text/markdown",
7
- };
@@ -1,2 +0,0 @@
1
- // .mjs is a synonynm for .js
2
- export { jsHandler as default } from "./handlers.js";
@@ -1,55 +0,0 @@
1
- import { getParent } from "@weborigami/async-tree";
2
- import { compile } from "@weborigami/language";
3
- import builtinsProgram from "../builtinsProgram.js";
4
- import getConfig from "../cli/getConfig.js";
5
- import * as utilities from "../common/utilities.js";
6
- import processUnpackedContent from "./processUnpackedContent.js";
7
-
8
- /**
9
- * An Origami expression file
10
- *
11
- * Unpacking an Origami file returns the result of evaluating the expression.
12
- */
13
- export default {
14
- mediaType: "text/plain",
15
-
16
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
17
- async unpack(packed, options = {}) {
18
- const parent = getParent(packed, options);
19
-
20
- // Construct an object to represent the source code.
21
- const sourceName = options.key;
22
- let url;
23
- if (sourceName && /** @type {any} */ (parent)?.url) {
24
- let parentHref = /** @type {any} */ (parent).url.href;
25
- if (!parentHref.endsWith("/")) {
26
- parentHref += "/";
27
- }
28
- url = new URL(sourceName, parentHref);
29
- }
30
-
31
- const source = {
32
- text: utilities.toString(packed),
33
- name: options.key,
34
- url,
35
- };
36
-
37
- // Compile the source code as an Origami program and evaluate it.
38
- const compiler = options.compiler ?? compile.program;
39
-
40
- const config = getConfig(parent) ?? {};
41
- const globals = {
42
- ...(options.globals ?? builtinsProgram()),
43
- ...config,
44
- };
45
-
46
- const fn = compiler(source, {
47
- globals,
48
- mode: "program",
49
- });
50
-
51
- let result = await fn.call(parent);
52
-
53
- return processUnpackedContent(result, parent);
54
- },
55
- };
@@ -1,78 +0,0 @@
1
- import { extension, getParent, trailingSlash } from "@weborigami/async-tree";
2
- import { compile } from "@weborigami/language";
3
- import builtinsProgram from "../builtinsProgram.js";
4
- import getConfig from "../cli/getConfig.js";
5
- import { toString } from "../common/utilities.js";
6
- import processUnpackedContent from "./processUnpackedContent.js";
7
-
8
- /**
9
- * An Origami template document: a plain text file that contains Origami
10
- * expressions.
11
- */
12
- export default {
13
- mediaType: "text/plain",
14
-
15
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
16
- async unpack(packed, options = {}) {
17
- const parent = getParent(packed, options);
18
-
19
- // Unpack as a text document
20
- const text = toString(packed);
21
-
22
- // See if we can construct a URL to use in error messages
23
- const key = options.key;
24
- let url;
25
- if (key && /** @type {any} */ (parent)?.url) {
26
- let parentHref = /** @type {any} */ (parent).url.href;
27
- if (!parentHref.endsWith("/")) {
28
- parentHref += "/";
29
- }
30
- url = new URL(key, parentHref);
31
- }
32
-
33
- // Compile the text as an Origami template document
34
- const source = {
35
- name: key,
36
- text,
37
- url,
38
- };
39
-
40
- const config = getConfig(parent) ?? {};
41
- const globals = {
42
- ...(options.globals ?? builtinsProgram()),
43
- ...config,
44
- };
45
-
46
- const defineFn = compile.templateDocument(source, {
47
- front: options.front,
48
- globals,
49
- mode: "program",
50
- });
51
-
52
- // Invoke the definition to get back the template function
53
- const result = await defineFn.call(parent);
54
-
55
- const resultExtension = key ? extension.extname(key) : null;
56
- if (resultExtension && Object.isExtensible(result)) {
57
- // Add sidecar function so this template can be used in a map.
58
- result.key = addExtension(resultExtension);
59
- }
60
-
61
- return processUnpackedContent(result, parent);
62
- },
63
- };
64
-
65
- // Return a function that adds the given extension
66
- function addExtension(resultExtension) {
67
- return (sourceKey) => {
68
- if (sourceKey === undefined) {
69
- return undefined;
70
- }
71
- const normalizedKey = trailingSlash.remove(sourceKey);
72
- const sourceExtension = extension.extname(normalizedKey);
73
- const resultKey = sourceExtension
74
- ? extension.replace(normalizedKey, sourceExtension, resultExtension)
75
- : normalizedKey + resultExtension;
76
- return resultKey;
77
- };
78
- }
@@ -1,16 +0,0 @@
1
- import { isOrigamiFrontMatter } from "@weborigami/language";
2
-
3
- export default function parseFrontMatter(text) {
4
- const regex =
5
- /^(---\r?\n(?<frontText>[\s\S]*?\r?\n?)---\r?\n)(?<body>[\s\S]*$)/;
6
- const match = regex.exec(text);
7
- if (!match?.groups) {
8
- return null;
9
- }
10
- const isOrigami = isOrigamiFrontMatter(match.groups.frontText);
11
- return {
12
- body: match.groups.body,
13
- frontText: match.groups.frontText,
14
- isOrigami,
15
- };
16
- }
@@ -1,35 +0,0 @@
1
- import { symbols, Tree } from "@weborigami/async-tree";
2
-
3
- /**
4
- * Perform any necessary post-processing on the unpacked content of a file. This
5
- * lets treat the contents of various file types consistently.
6
- *
7
- * @typedef {import("@weborigami/types").AsyncTree} AsyncTree
8
- *
9
- * @param {any} content
10
- * @param {AsyncTree|null} parent
11
- * @returns
12
- */
13
- export default function processUnpackedContent(content, parent) {
14
- if (typeof content === "function") {
15
- // Bind the function to the parent as the `this` context.
16
- const result = content.bind(parent);
17
- // Copy over any properties that were attached to the function
18
- Object.assign(result, content);
19
- return result;
20
- } else if (Tree.isAsyncTree(content) && !content.parent) {
21
- const result = Object.create(content);
22
- result.parent = parent;
23
- return result;
24
- } else if (Object.isExtensible(content) && !content[symbols.parent]) {
25
- Object.defineProperty(content, symbols.parent, {
26
- configurable: true,
27
- enumerable: false,
28
- value: parent,
29
- writable: true,
30
- });
31
- return content;
32
- } else {
33
- return content;
34
- }
35
- }
@@ -1 +0,0 @@
1
- export { jsHandler as default } from "./handlers.js";
@@ -1,91 +0,0 @@
1
- import { isPacked, symbols } from "@weborigami/async-tree";
2
- import { compile } from "@weborigami/language";
3
- import { parseYaml, toYaml } from "../common/serialize.js";
4
- import { toString } from "../common/utilities.js";
5
- import parseFrontMatter from "./parseFrontMatter.js";
6
-
7
- /**
8
- * A text file with possible front matter
9
- *
10
- * The unpacking process will parse out any YAML or JSON front matter and attach
11
- * it to the document as data. The first line of the text must be "---",
12
- * followed by a block of JSON or YAML, followed by another line of "---". Any
13
- * lines following will be treated as the document text.
14
- *
15
- * If there is no front matter, the document will be treated as plain text and
16
- * returned as a String object.
17
- *
18
- * If there is front matter, any Origami expressions in the front matter will be
19
- * evaluated. The result will be a plain JavaScript object with the evaluated
20
- * data and a `_body` property containing the document text.
21
- */
22
- export default {
23
- mediaType: "text/plain",
24
-
25
- /**
26
- * If the input is already in some packed format, it will be returned as is.
27
- *
28
- * Otherwise, the properties of the object will be formatted as YAML. If the
29
- * object has a `_body` property, that will be used as the body of the text
30
- * document; otherwise, an empty string will be used.
31
- *
32
- * @param {any} object
33
- * @returns {Promise<import("@weborigami/async-tree").Packed>}
34
- */
35
- async pack(object) {
36
- if (isPacked(object)) {
37
- return object;
38
- } else if (!object || typeof object !== "object") {
39
- throw new TypeError("The input to pack must be a JavaScript object.");
40
- }
41
-
42
- const text = object._body ?? "";
43
-
44
- /** @type {any} */
45
- const dataWithoutText = Object.assign({}, object);
46
- delete dataWithoutText._body;
47
- if (Object.keys(dataWithoutText).length > 0) {
48
- const frontMatter = (await toYaml(dataWithoutText)).trimEnd();
49
- return `---\n${frontMatter}\n---\n${text}`;
50
- } else {
51
- return text;
52
- }
53
- },
54
-
55
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
56
- async unpack(packed, options = {}) {
57
- const parent = options.parent ?? null;
58
- const text = toString(packed);
59
- if (text === null) {
60
- throw new Error("Tried to treat a file as text but it wasn't text.");
61
- }
62
-
63
- const parsed = parseFrontMatter(text);
64
- let unpacked;
65
- if (parsed) {
66
- // Document object with front matter
67
- const { body, frontText, isOrigami } = parsed;
68
- let frontData;
69
- if (isOrigami) {
70
- const compiled = compile.expression(frontText.trim());
71
- frontData = await compiled.call(parent);
72
- } else {
73
- frontData = parseYaml(frontText);
74
- }
75
- unpacked = { ...frontData };
76
- Object.defineProperty(unpacked, "_body", {
77
- configurable: true,
78
- enumerable: true,
79
- value: body,
80
- writable: true,
81
- });
82
- } else {
83
- // Plain text
84
- unpacked = new String(text);
85
- }
86
-
87
- unpacked[symbols.parent] = parent;
88
-
89
- return unpacked;
90
- },
91
- };
@@ -1,17 +0,0 @@
1
- import processUnpackedContent from "./processUnpackedContent.js";
2
-
3
- /**
4
- * A WebAssembly module
5
- *
6
- * Unpacking a WebAssembly module returns its exports.
7
- */
8
- export default {
9
- mediaType: "application/wasm",
10
-
11
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
12
- async unpack(packed, options = {}) {
13
- const wasmModule = await WebAssembly.instantiate(packed);
14
- // @ts-ignore TypeScript thinks wasmModule is already an Instance.
15
- return processUnpackedContent(wasmModule.instance.exports, options.parent);
16
- },
17
- };
@@ -1,2 +0,0 @@
1
- // .xhtml is a synonynm for .html
2
- export { htmlHandler as default } from "./handlers.js";
@@ -1,36 +0,0 @@
1
- import { symbols } from "@weborigami/async-tree";
2
- import * as YAMLModule from "yaml";
3
- import { parseYaml } from "../common/serialize.js";
4
- import * as utilities from "../common/utilities.js";
5
- import processUnpackedContent from "./processUnpackedContent.js";
6
-
7
- // See notes at serialize.js
8
- // @ts-ignore
9
- const YAML = YAMLModule.default ?? YAMLModule.YAML;
10
-
11
- /**
12
- * A YAML file
13
- *
14
- * Unpacking a YAML file returns the parsed data.
15
- *
16
- */
17
- export default {
18
- mediaType: "application/yaml",
19
-
20
- /** @type {import("@weborigami/async-tree").UnpackFunction} */
21
- unpack(packed, options = {}) {
22
- const parent = options.parent ?? null;
23
- const yaml = utilities.toString(packed);
24
- if (!yaml) {
25
- throw new Error("Tried to parse something as YAML but it wasn't text.");
26
- }
27
- const data = parseYaml(yaml);
28
- if (data && typeof data === "object" && Object.isExtensible(data)) {
29
- Object.defineProperty(data, symbols.deep, {
30
- enumerable: false,
31
- value: true,
32
- });
33
- }
34
- return processUnpackedContent(data, parent);
35
- },
36
- };
@@ -1,2 +0,0 @@
1
- // .yml is a synonym for .yaml
2
- export { yamlHandler as default } from "./handlers.js";