@ingram-tech/nk-dev 0.21.0 → 0.21.1

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 (2) hide show
  1. package/lib/og-image.js +44 -2
  2. package/package.json +1 -1
package/lib/og-image.js CHANGED
@@ -13,9 +13,51 @@ import { join, resolve } from "node:path";
13
13
 
14
14
  const PAGE_OR_LAYOUT = /^(layout|page)\.(tsx|jsx|ts|js)$/;
15
15
  const IMAGE_FILE = /^opengraph-image\d*\.(tsx|jsx|ts|js|png|jpe?g|gif)$/;
16
- const DECLARES_CARD = /\b(openGraph|twitter)\s*:/;
16
+ const CARD_KEY = /\b(openGraph|twitter)\s*:/g;
17
17
  const CARRIES_IMAGE = /\bimages\s*[:,}]|\bogImageMetadata\s*\(/;
18
18
 
19
+ /** Blank out string and template literals, keeping offsets, so braces inside
20
+ * copy (`"{name}"`) don't confuse the bracket walk below. */
21
+ const blankStrings = (src) =>
22
+ src.replace(/(["'`])(?:\\.|(?!\1)[\s\S])*\1/g, (m) => " ".repeat(m.length));
23
+
24
+ /**
25
+ * Whether the card key at `at` sits in a metadata object the file builds
26
+ * itself (`export const metadata = { ... }`, `return { ... }`, `=> ({ ... })`)
27
+ * rather than in an argument to a call (`createPageMetadata({ openGraph })`).
28
+ * A call can add the image (nk-seo's `createMetadata` merges `defaultImage`
29
+ * into the override), and a text check cannot see what it adds, so only a
30
+ * literal the file returns is provably missing one.
31
+ */
32
+ function inOwnLiteral(src, at) {
33
+ let depth = 0;
34
+ for (let i = at - 1; i >= 0; i--) {
35
+ const c = src[i];
36
+ if (c === ")" || c === "]" || c === "}") {
37
+ depth++;
38
+ continue;
39
+ }
40
+ if (c !== "(" && c !== "[" && c !== "{") continue;
41
+ if (depth > 0) {
42
+ depth--;
43
+ continue;
44
+ }
45
+ if (c === "[") continue; // an element of an array: keep walking out
46
+ const before = src.slice(0, i).trimEnd();
47
+ if (/(=>|\breturn|[^=!<>]=)$/.test(before)) return true;
48
+ if (c === "(") return false; // a call argument
49
+ if (/[:,{(]$/.test(before)) continue; // a nested object: keep walking out
50
+ return true; // a statement or function body: the literal is the file's own
51
+ }
52
+ return true;
53
+ }
54
+
55
+ /** Whether the file returns a card (openGraph/twitter) of its own building. */
56
+ const declaresOwnCard = (src) => {
57
+ const code = blankStrings(src);
58
+ return [...code.matchAll(CARD_KEY)].some((m) => inOwnLiteral(code, m.index));
59
+ };
60
+
19
61
  /** The site's app directory, or null when it has none. */
20
62
  function findAppDir(cwd) {
21
63
  const dir = resolve(cwd, "src/app");
@@ -47,7 +89,7 @@ function collect(cwd, dir, segments, nearest, out) {
47
89
  if (!PAGE_OR_LAYOUT.test(entry.name)) continue;
48
90
  if (here) continue; // the file convention wins in its own segment
49
91
  const src = readFileSync(full, "utf8");
50
- if (!DECLARES_CARD.test(src) || CARRIES_IMAGE.test(src)) continue;
92
+ if (!declaresOwnCard(src) || CARRIES_IMAGE.test(src)) continue;
51
93
  out.push({
52
94
  file: full.slice(cwd.length + 1),
53
95
  route: `/${segments.join("/")}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ingram-tech/nk-dev",
3
- "version": "0.21.0",
3
+ "version": "0.21.1",
4
4
  "description": "The nextkit dev toolchain in one package: the `nk` CLI plus shared oxlint/oxfmt, TypeScript, and Vitest config, the format-on-commit hook, and the AI agent guide. `nk init` scaffolds a site to use it.",
5
5
  "license": "MIT",
6
6
  "type": "module",