# Site rules

> The data format of an extension site rule: every field, how a rule is matched to a page, the three kinds of selector, and how rules are stored, exported and imported.

Source: https://crawl-docs.pdhq.win/advanced/site-rules/
Last updated: 2026-12-02

A **site rule** tells the extension how to read the article of one website when its automatic reader gets it wrong. It is data only: CSS selectors and a few switches. It never runs code. The code is `apps/extension/src/templates.ts` and the reader that uses it is `extract-article.ts`.

You can make a rule with the mouse (see [Site rules in the extension](https://crawl-docs.pdhq.win/guide/site-rules-in-the-extension.md)) or write it by hand under **Settings → Site rules** in the extension.

## A rule

```json
{
  "name": "My blog",
  "hosts": ["blog.example.com"],
  "paths": "^/posts/",
  "root": "main article",
  "title": "h1.entry-title",
  "author": [".byline a", ".author"],
  "date": "time[datetime]",
  "remove": [".share-bar", ".related", "nav"],
  "map": [
    { "selector": ".callout", "as": "quote" },
    { "selector": ".big-heading", "as": "h2" }
  ],
  "expand": ["details.faq summary"],
  "waitFor": "main article",
  "scroll": true
}
```

A rule needs `hosts` and at least one of `root`, `remove`, `map`, `title`, `author` or `date`.

## Fields

| Field | Type | Meaning |
| --- | --- | --- |
| `id` | text, up to 80 | Unique id. Made for you when you leave it out. |
| `name` | text, up to 100 | A name to recognise it in the settings. |
| `hosts` | 1 to 20 host names | The sites it is for. `example.com` also covers `www.example.com` and every other subdomain. A leading `*.` or `www.` is dropped. |
| `paths` | regular expression, up to 200 | Only pages whose path matches it, written without slashes around it, for example `^/blog/`. |
| `root` | selector or up to 10 selectors | The element(s) that hold the article. Several selectors are several areas: all that are on the page are saved, in page order. |
| `fallbackRoot` | up to 8 selectors | Backup areas, tried in order when none of `root` is on the page (the site changed its markup). |
| `title`, `author`, `date` | a selector or a list of up to 8 | Where to find them when the page's own tags are not enough. A list is a list of fallbacks: the first that finds something wins. |
| `remove` | up to 60 selectors | Parts to leave out: menus, related posts, share bars. |
| `map` | up to 60 `{ selector, as }` | For parts built from plain boxes: say what each is. `as` is one of `h1`, `h2`, `h3`, `h4`, `quote`, `code`, `rule`, `bullet`, `number`, `paragraph`, `file`, `ignore`. |
| `expand` | up to 20 selectors | Parts to click open before reading (toggles whose content is only drawn when opened). They are closed again afterwards. |
| `rows` | selector | Parts the page throws away again when they scroll out of view (Notion). While scrolling they are remembered and put back for reading. Needs `scroll`. |
| `detect` | selector | Recognises pages made with a tool (Notion) whatever their address: the rule then applies there too, unless the site has a rule of its own. |
| `waitFor`, `waitMs` | selector, ms | Wait until the selector exists (the page draws its text late), for up to `waitMs` (default 6000, at most 15000). |
| `scroll` | `true` or a selector | Scroll the page, or the element, once from top to bottom, so lazy content loads. |
| `allowShort` | `true` | Accept articles with less text than the automatic reader would. |
| `keepSmall` | `true` | Keep pictures narrower than 120 px (logos and icons) that the reader leaves out. For pages where every picture is content, such as a README. |

## Selectors

Each selector is one of three kinds:

| Kind | Looks like | Notes |
| --- | --- | --- |
| CSS | `main article` | The default. Checked with the browser's own parser, so a wrong one is refused with the field name. |
| XPath | `xpath://div[@id='x']` | Everything after `xpath:` is the expression. |
| Text | `text:Related posts` | Matches a part by the words in it. |

A selector is at most 300 characters.

## Which rule is used

1. Your own rules come first. A rule you made for a host always beats a built-in one.
2. A rule covers a page when one of its `hosts` equals the page's host (without `www.`) or is its parent domain, and `paths`, if set, matches the path. The **most specific host (the longest) wins**.
3. A rule with `detect` also covers pages on any address where that selector exists, unless a rule names the site.
4. With no rule, the automatic reader runs: the page's `<article>`, or the block with the most text.

Built-in rules live in `BUILTIN_TEMPLATES` in `templates.ts`. They were checked against the sites' markup as known, not on the live pages.

## Storage, export and import

Rules are kept in the browser's `chrome.storage.local`, at most 200. They live in the extension, not on your server. **Export my rules** writes them as JSON, and **Import rules** reads such a file. Every rule is checked again on import by `parseTemplate`, which says what is wrong.

## Write one

1. Open the page, press **Choose the article area** and click the part. The rule is made for you. Open **Site rules** to read it.
2. Add `remove` for the parts that came along that you do not want.
3. Add `map` entries for parts that are headings or quotes drawn as plain boxes.
4. Press **Save rule**, save the page again, and look at the result in the library.

For a page that draws late, add `waitFor`. For lazy pages, add `scroll`.

## Rules for the web app's view are a separate thing

How a saved article is **shown** is set by a layout, not a site rule. See [The layout language](https://crawl-docs.pdhq.win/advanced/layout-language.md).
