Skip to content

Latest commit

 

History

History
107 lines (81 loc) · 3.94 KB

AGENTS.md

File metadata and controls

107 lines (81 loc) · 3.94 KB

@readstack/parser agent guide

This package finds the article in a web page and converts it to Markdown. It is used by the Readstack parser service. This file is for coding agents. CLAUDE.md is a symlink to it. For the public API, see README.md.

Rules

  • Keep changes tightly scoped. Do not add unrelated refactors, abstractions, optimizations, or features.
  • Read the nearest test before changing behavior. Update existing tests that the change breaks or makes wrong.
  • The public API is the exports of src/index.ts. Everything else is internal. Changing an export is a breaking change for the parser service.
  • Use the terms in Parser detection terms in code and comments.
  • Write commit messages as Conventional Commits with the types feat, fix, perf, docs, style, refactor, test, build, ci, chore or revert. The type decides the release: a breaking change publishes a major version, feat a minor, fix and perf a patch, and other types nothing.

Validation

Before committing, format, lint and typecheck the changed files and run the tests. The scripts are in package.json (test, typecheck, lint, chk, fmt, build). For a targeted check, run the tool on the file:

pnpm exec prettier --check path/to/file
pnpm exec oxlint path/to/file
pnpm exec vitest run path/to/test

Tests

Tests are *.test.ts files beside the code, with Vitest globals. Snapshots live in __snapshots__/ beside each test.

Add new tests only when they protect behavior that matters and that existing tests do not already cover. Ask before adding one, except for detection fixtures (see Default article detection).

Fixture pages in src/parsers/fixtures/ are real third-party pages. Keep them as they are and covered by src/parsers/fixtures/NOTICE.md.

Parser selection

src/parsers/registry.ts tries parsers in order and returns the first success:

  1. site-specific parsers whose canParse(url) matches (currently BbcUkParser, which removes bbc.co.uk byline, metadata, and link blocks before default detection);
  2. DefaultParser;
  3. ReadabilityParser (Mozilla Readability), the last resort.

Every parser extends BaseParser, which purifies the HTML, extracts Open Graph metadata, runs PreProcessHtml, calls the parser's extract(), calculates reading time (src/util/readingTime.ts), and converts the result to Markdown.

Default article detection

DefaultParser measures content patterns instead of trusting a site's semantic HTML. Candidate acceptance rules are the options object in DefaultParser.ts.

  1. Find article elements (paragraphs, headings, lists, images, tables, code blocks).
  2. Walk past single-child wrappers to find possible containers.
  3. Keep candidates that pass the acceptance rules (DefaultParser.ts, with helpers in src/findArticle/candidates.ts).
  4. Choose or build the article container with findArticleContentContainer (src/findArticle/resolve.ts). Its comments document each check in order.
  5. Remove a repeated leading heading and navigation-heavy lists (src/findArticle/filter.ts).
  6. Convert to Markdown.

When changing detection, add a fixture under src/parsers/fixtures/ and a test, and check snapshot changes in __snapshots__/.

Parser detection terms

Candidate: A possible article content Container. Avoid: contender

Container: The DOM element returned for filtering.

Parent, child, sibling: DOM relationships.

Paragraph text, article text: Text that belongs to the article. Avoid: prose

Article elements: The elements counted by candidate checks: paragraphs, headings, lists, images, tables, and code blocks. Avoid: anchor

Comments

  • Use short, plain-language comments, lowercase unless grammar or a proper noun requires otherwise.
  • Explain surprising intent or constraints, not the next line of code.
  • Update comments when the related control flow changes.