@backmatter/core 0.0.9 → 0.0.10

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": "@backmatter/core",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/doc.ts CHANGED
@@ -4,6 +4,8 @@ export type BackmatterDocSummary = {
4
4
  locale: string;
5
5
  type: string;
6
6
  slug: string;
7
+ /** Card-level fields when the loader already parsed the file. */
8
+ frontmatter?: Record<string, unknown>;
7
9
  };
8
10
 
9
11
  export type BackmatterDoc = {
@@ -53,10 +53,20 @@ export async function listDocsFromFs(
53
53
  const rel = path.relative(siteRoot, full).split(path.sep).join("/");
54
54
  const hit = matchAnyModelPath(models, rel);
55
55
  if (!hit) continue;
56
+ const model = models.find((m) => m.type === hit.type);
57
+ let frontmatter: Record<string, unknown> | undefined;
58
+ if (model) {
59
+ try {
60
+ frontmatter = parseDoc(await readFile(full, "utf-8"), modelFormat(model)).frontmatter;
61
+ } catch {
62
+ frontmatter = undefined;
63
+ }
64
+ }
56
65
  docs.push({
57
66
  locale: hit.locale,
58
67
  type: hit.type,
59
68
  slug: hit.slug,
69
+ frontmatter,
60
70
  });
61
71
  }
62
72
  return docs.sort((a, b) => {
package/src/query.ts CHANGED
@@ -103,8 +103,21 @@ export function createQuery<const Models extends readonly ModelDef[]>(
103
103
  docs = doc ? [doc] : [];
104
104
  } else {
105
105
  const summaries = await loader.listDocs({ type, locale: opts?.locale });
106
- const loaded = await Promise.all(summaries.map((s) => get(type, s.locale, s.slug)));
107
- docs = loaded.filter((d): d is BackmatterDoc => d !== null);
106
+ const fromList = summaries.filter((s) => s.frontmatter != null);
107
+ const missing = summaries.filter((s) => s.frontmatter == null);
108
+ const loaded = await Promise.all(missing.map((s) => get(type, s.locale, s.slug)));
109
+ docs = [
110
+ ...fromList.map((s) => ({
111
+ locale: s.locale,
112
+ type: s.type,
113
+ slug: s.slug,
114
+ source: "head" as const,
115
+ frontmatter: normalizeFrontmatter(s.frontmatter ?? {}),
116
+ body: "",
117
+ raw: "",
118
+ })),
119
+ ...loaded.filter((d): d is BackmatterDoc => d !== null),
120
+ ];
108
121
  }
109
122
 
110
123
  const filtered = opts?.where ? docs.filter((d) => matchesWhere(d, opts.where!)) : docs;