@jxsuite/parser 0.25.1 → 0.25.3

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,116 @@
1
+ // src/content.ts
2
+ function evaluateFilterRule(rule, entry) {
3
+ const actual = rule.field === "id" ? entry.id : entry.data[rule.field];
4
+ switch (rule.op) {
5
+ case "==":
6
+ return actual === rule.value;
7
+ case "!=":
8
+ return actual !== rule.value;
9
+ case "empty":
10
+ return actual == null || actual === "" || Array.isArray(actual) && actual.length === 0;
11
+ case "not empty":
12
+ return actual != null && actual !== "" && !(Array.isArray(actual) && actual.length === 0);
13
+ case "contains":
14
+ return typeof actual === "string" && actual.includes(String(rule.value ?? ""));
15
+ case "not contains":
16
+ return typeof actual === "string" && !actual.includes(String(rule.value ?? ""));
17
+ case ">":
18
+ return Number(actual) > Number(rule.value);
19
+ case "<":
20
+ return Number(actual) < Number(rule.value);
21
+ case ">=":
22
+ return Number(actual) >= Number(rule.value);
23
+ case "<=":
24
+ return Number(actual) <= Number(rule.value);
25
+ default:
26
+ return true;
27
+ }
28
+ }
29
+ function queryContentType(entries, query = {}) {
30
+ let result = [...entries];
31
+ if (query.filter) {
32
+ let rules;
33
+ if (Array.isArray(query.filter)) {
34
+ rules = query.filter;
35
+ } else if (typeof query.filter === "object") {
36
+ rules = Object.entries(query.filter).map(([field, value]) => ({ field, op: "==", value }));
37
+ } else {
38
+ rules = [];
39
+ }
40
+ result = result.filter((entry) => rules.every((rule) => evaluateFilterRule(rule, entry)));
41
+ }
42
+ if (query.sort) {
43
+ const sortRules = Array.isArray(query.sort) ? query.sort : [query.sort];
44
+ result.sort((a, b) => {
45
+ for (const { field, order = "asc" } of sortRules) {
46
+ const aVal = field === "id" ? a.id : a.data[field] ?? "";
47
+ const bVal = field === "id" ? b.id : b.data[field] ?? "";
48
+ if (aVal < bVal)
49
+ return order === "asc" ? -1 : 1;
50
+ if (aVal > bVal)
51
+ return order === "asc" ? 1 : -1;
52
+ }
53
+ return 0;
54
+ });
55
+ }
56
+ if (query.limit && query.limit > 0) {
57
+ result = result.slice(0, query.limit);
58
+ }
59
+ return result;
60
+ }
61
+ function findEntry(entries, id) {
62
+ return entries.find((e) => e.id === id) ?? null;
63
+ }
64
+
65
+ class ContentCollection {
66
+ config;
67
+ constructor(config) {
68
+ this.config = config;
69
+ }
70
+ resolve() {
71
+ const { contentType, filter, sort, limit, _project } = this.config;
72
+ const entries = _project?.contentTypes?.get(contentType ?? "");
73
+ if (!entries)
74
+ return [];
75
+ return queryContentType(entries, {
76
+ ...filter != null && {
77
+ filter
78
+ },
79
+ ...sort != null && {
80
+ sort
81
+ },
82
+ ...limit != null && { limit }
83
+ });
84
+ }
85
+ }
86
+
87
+ class ContentEntry {
88
+ config;
89
+ constructor(config) {
90
+ this.config = config;
91
+ }
92
+ resolve() {
93
+ const { contentType, id, field, _project, _document } = this.config;
94
+ const entries = _project?.contentTypes?.get(contentType ?? "");
95
+ if (!entries)
96
+ return null;
97
+ let resolvedId = id;
98
+ if (resolvedId && typeof resolvedId === "object" && resolvedId.$ref?.startsWith("#/$params/")) {
99
+ const paramName = resolvedId.$ref.replace("#/$params/", "");
100
+ resolvedId = _document?.route?._pathParams?.[paramName];
101
+ }
102
+ if (!resolvedId)
103
+ return null;
104
+ if (field && field !== "id") {
105
+ return entries.find((e) => e.data[field] === resolvedId) ?? null;
106
+ }
107
+ return findEntry(entries, resolvedId);
108
+ }
109
+ }
110
+ export {
111
+ queryContentType,
112
+ findEntry,
113
+ evaluateFilterRule,
114
+ ContentEntry,
115
+ ContentCollection
116
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jxsuite/parser",
3
- "version": "0.25.1",
3
+ "version": "0.25.3",
4
4
  "description": "Jx markdown parser and external class integration",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -9,7 +9,8 @@
9
9
  "directory": "packages/parser"
10
10
  },
11
11
  "files": [
12
- "src/"
12
+ "src/",
13
+ "dist"
13
14
  ],
14
15
  "type": "module",
15
16
  "exports": {
@@ -27,12 +28,14 @@
27
28
  "provenance": true
28
29
  },
29
30
  "scripts": {
31
+ "prepare": "bun build src/content.ts --outdir=dist --format=esm --target=node",
32
+ "build": "bun run prepare",
30
33
  "test": "bun test --isolate",
31
34
  "test:coverage": "bun test --isolate --coverage",
32
35
  "upgrade": "bunx npm-check-updates -u && bun install"
33
36
  },
34
37
  "dependencies": {
35
- "@jxsuite/schema": "^0.25.1",
38
+ "@jxsuite/schema": "^0.25.3",
36
39
  "glob": "^13.0.6",
37
40
  "hast-util-from-html": "^2.0.3",
38
41
  "hast-util-whitespace": "^3.0.0",
@@ -50,7 +53,7 @@
50
53
  },
51
54
  "devDependencies": {
52
55
  "@happy-dom/global-registrator": "^20.9.0",
53
- "@jxsuite/runtime": "^0.25.1",
56
+ "@jxsuite/runtime": "^0.25.3",
54
57
  "@types/hast": "^3.0.4"
55
58
  }
56
59
  }
@@ -4,7 +4,7 @@
4
4
  "description": "Query a project content type with filter, sort, and limit",
5
5
  "$prototype": "Class",
6
6
  "extends": "Object",
7
- "$implementation": "./content.ts",
7
+ "$implementation": "../dist/content.js",
8
8
 
9
9
  "$defs": {
10
10
  "parameters": {
@@ -4,7 +4,7 @@
4
4
  "description": "Fetch a single entry from a content type by ID",
5
5
  "$prototype": "Class",
6
6
  "extends": "Object",
7
- "$implementation": "./content.ts",
7
+ "$implementation": "../dist/content.js",
8
8
 
9
9
  "$defs": {
10
10
  "parameters": {