Skip to content

feat(guides): strip numeric ordering prefix from rendered titles #3463

@PierreBrisorgueil

Description

@PierreBrisorgueil

Problem

lib/helpers/guides.jstitleFromPath renders guide filenames verbatim. Projects that prefix guides with a numeric order key (e.g. 00-welcome.md, 01-api-access.md) so the Scalar sidebar stays in the right order end up with ugly titles in the rendered output:

  • 00-welcome.md00 Welcome
  • 01-api-access.md01 Api Access
  • 02-scraping.md02 Scraping

The number is load-bearing for the sort (the loader uses a.title.localeCompare(b.title)), but readers should never see it.

Real-world example: https://github.com/comes-io/trawl_node/pull/(TBD) — trawl_node now renumbers its 5 public guides to get explicit ordering, and lives with "00 Welcome" / "01 Api Access" titles in the sidebar.

Proposal

In titleFromPath, strip a leading ^\d+[-_] from the basename before title-casing. Keep the original filename available for sorting.

const titleFromPath = (filePath) => {
  const base = path.basename(String(filePath), path.extname(String(filePath)));
  const stripped = base.replace(/^\d+[-_]/, '');
  return stripped
    .split(/[-_\s]+/)
    .filter(Boolean)
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
};

Sorting currently happens on the rendered title — which would collapse the intended order if two guides differ only by their numeric prefix. The sort key should be derived from the filename instead (or from the raw basename) so 00-welcome sorts before 01-api-access regardless of their rendered titles.

Alternative

Support an explicit order: frontmatter (YAML or <!-- order: 10 -->) in each guide. More expressive but heavier to implement. The numeric-prefix convention is dead simple and already works — it just needs the title-stripping fix.

Scope

  • lib/helpers/guides.js only
  • Add unit tests in modules/core/tests/core.unit.tests.js for both titleFromPath with numeric prefixes and loadGuides sort order
  • No downstream migration needed — existing projects without prefixes are unaffected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions