Skip to content

Index layout file manually prints braces, quotes, keys, and values to build index.json, but fails to place commas - #557

Open
aniket866 wants to merge 2 commits into
microcks:masterfrom
aniket866:fixing-comma-handling
Open

Index layout file manually prints braces, quotes, keys, and values to build index.json, but fails to place commas #557
aniket866 wants to merge 2 commits into
microcks:masterfrom
aniket866:fixing-comma-handling

Conversation

@aniket866

Copy link
Copy Markdown

Closes #556

Describe the bug

Description

The search index layout file manually prints braces, quotes, keys, and values to build index.json, but fails to place commas between items in the page range loop, or prints trailing commas when pages are excluded.

  • File Link: themes/microcks/layouts/_default/index.json
  • Code Snippet:
    }{{- if eq $item $len -}}{{- else -}},{{- end -}}

Why It Breaks

Without proper comma delimiters between items in the inner loop, the result is malformed JSON. Furthermore, if a page is excluded (e.g., ignoreSearch: true), the hardcoded comma rendering logic results in trailing commas (e.g. [ { ... }, ]) or consecutive commas (e.g., ,,). This causes JSON.parse() to throw a syntax error in the browser and completely breaks the search capability.

How to Reproduce

  1. Configure any documentation page with ignoreSearch = true in frontmatter.
  2. Build the Hugo site: hugo.
  3. Open the output file public/index.json and validate it using a JSON validator (like JSON.parse() in Node.js).
  4. Observe the parsing failure due to syntax error.

Reproduction Flow

graph TD
    A[Start: Build Search Index] --> B[Hugo iterates pages in Group]
    B --> C{Is page ignored?}
    C -- Yes --> D[Page skipped; index incremented]
    C -- No --> E[Object printed]
    D --> F[Prints delimiter comma anyway]
    F --> G[Generates trailing comma at end of list: ', ]']
    G --> H[Client script fetches index.json]
    H --> I[Execute JSON.parse()]
    I --> J[Error: SyntaxError: Unexpected token ] in JSON]
Loading

Signed-off-by: aniket866 <iamaniketkumarmaner@gmail.com>
@aniket866
aniket866 marked this pull request as draft July 31, 2026 19:22
Signed-off-by: aniket866 <iamaniketkumarmaner@gmail.com>
@aniket866
aniket866 marked this pull request as ready for review July 31, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax Corruption and Crash in index.json Search Template

1 participant