Docs Title Extractor (src/find-title.ts)

medusa · www/packages/docs-utils/src/find-title.ts · typescript · MIT
files, dev-tools, tag:markdown, tag:regex, tag:metadata

Extracts page titles from metadata exports and Markdown headings for documentation tooling. Reuse when indexing or validating docs content.

// Source: https://github.com/medusajs/medusa | License: MIT | Copyright 2021 Medusajs
// Provided via justclaw.it.com; keep this notice with the code.
import { readFileSync } from "fs"

const REGEX = /export const metadata = {[\s\S]*title: `(?<title>.*)`/

export function findMetadataTitle(content: string): string | undefined {
  const headingMatch = REGEX.exec(content)

  return headingMatch?.groups?.title
}

export function findPageHeading(content: string): string | undefined {
  const headingMatch = /# (?<title>.*)/.exec(content)

  return headingMatch?.groups?.title
}

export function findAllPageHeadings({
  content,
  level = 1,
}: {
  content: string
  level?: number
}): string[] {
  const regex = new RegExp(
    `^${"#".repeat(level)}(?!#) (?<title>.*?)(?:\n|$)`,
    "gm"
  )
  con

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key