Repository navigation
feat(connectors): add a website connector that syncs pages from a site's sitemaps #10536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,514
−7
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
08bb7d9
feat(connectors): add a website connector that syncs pages from a sit…
waleedlatif1 84bce92
fix(connectors): parse sitemaps with a streaming XML parser, pin the …
waleedlatif1 8b81e2f
fix(connectors): read only an entry's own sitemap location, reject ma…
waleedlatif1 ff7288f
fix(connectors): rank robots rules by the path they name, without the…
waleedlatif1 ff01199
fix(connectors): split robots.txt on lone carriage returns and bound …
waleedlatif1 b1fb431
docs(connectors): document the website connector's exported types
waleedlatif1 81ad793
fix(connectors): withdraw a website page the HTML parser cannot read …
waleedlatif1 29b4e7b
docs(connectors): document the website connector exports
waleedlatif1 994c7d2
fix(connectors): order website listings by URL and never reconcile a …
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { websiteConnector } from '@/connectors/website/website' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { WebsiteIcon } from '@/components/icons' | ||
| import type { ConnectorMeta } from '@/connectors/types' | ||
|
|
||
| /** Pages indexed per sync when `maxPages` is not configured. */ | ||
| export const WEBSITE_DEFAULT_MAX_PAGES = 500 | ||
|
|
||
| /** Hard ceiling on the configurable `maxPages` value. */ | ||
| export const WEBSITE_MAX_PAGES_LIMIT = 5000 | ||
|
|
||
| /** Client-safe metadata for the website connector: its setup fields, auth and tags. */ | ||
| export const websiteConnectorMeta: ConnectorMeta = { | ||
| id: 'website', | ||
| name: 'Website', | ||
| description: 'Sync the pages of a website from its sitemap', | ||
| version: '1.0.0', | ||
| icon: WebsiteIcon, | ||
|
|
||
| /** | ||
| * Public sites need nothing. A site behind a gateway that accepts a service credential in one | ||
| * header, such as an API key, takes one `Name: value` line. Cookies are refused: a session cookie | ||
| * is a person's identity, and everything it unlocks would become readable to everyone with | ||
| * access to the knowledge base. | ||
| */ | ||
| auth: { | ||
| mode: 'apiKey', | ||
| label: 'Request Header', | ||
| placeholder: 'Only for sites that require a service header, e.g. X-Api-Key: value', | ||
| optional: true, | ||
| }, | ||
|
|
||
| configFields: [ | ||
| { | ||
| id: 'siteUrl', | ||
| title: 'Website URL', | ||
| type: 'short-input', | ||
| placeholder: 'https://www.example.com', | ||
| required: true, | ||
| description: | ||
| 'Pages are discovered from the sitemaps the site lists in robots.txt, or its sitemap.xml. Only pages on this host and under this path are synced, and robots.txt rules are honored.', | ||
| }, | ||
| { | ||
| id: 'sitemapUrl', | ||
| title: 'Sitemap URL', | ||
| type: 'short-input', | ||
| placeholder: 'e.g. https://www.example.com/pages-sitemap.xml', | ||
| required: false, | ||
| description: 'A sitemap on the same host to read instead of the discovered ones', | ||
| }, | ||
| { | ||
| id: 'includePaths', | ||
| title: 'Include Paths', | ||
| type: 'short-input', | ||
| placeholder: 'e.g. /benefits, /help/*/faq', | ||
| required: false, | ||
| description: | ||
| 'Comma-separated path prefixes to sync; * matches any characters. Leave empty to sync every page.', | ||
| }, | ||
| { | ||
| id: 'excludePaths', | ||
| title: 'Exclude Paths', | ||
| type: 'short-input', | ||
| placeholder: 'e.g. /blog, /archive', | ||
| required: false, | ||
| description: 'Comma-separated path prefixes never to sync; * matches any characters.', | ||
| }, | ||
| { | ||
| id: 'maxPages', | ||
| title: 'Max Pages', | ||
| type: 'short-input', | ||
| placeholder: `e.g. 200 (default: ${WEBSITE_DEFAULT_MAX_PAGES}, max: ${WEBSITE_MAX_PAGES_LIMIT})`, | ||
| required: false, | ||
| description: 'Maximum number of pages to index', | ||
| }, | ||
| ], | ||
|
|
||
| tagDefinitions: [ | ||
| { id: 'section', displayName: 'Section', fieldType: 'text' }, | ||
| { id: 'lastModified', displayName: 'Last Modified', fieldType: 'date' }, | ||
| ], | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import * as cheerio from 'cheerio' | ||
| import { | ||
| assertHtmlStringWithinLimits, | ||
| loadedHtmlToStructuredText, | ||
| } from '@/lib/file-parsers/html-parser' | ||
| import { WEBSITE_CRAWLER_TOKEN } from '@/connectors/website/robots' | ||
|
|
||
| /** Page-level chrome repeated on every page, which would make every page's text look alike. */ | ||
| const CHROME_SELECTOR = | ||
| 'body > header, body > footer, nav, [role="navigation"], [role="banner"], [role="contentinfo"]' | ||
|
|
||
| /** What a rendered page contributes to the index: its title, its text, and whether it opts out. */ | ||
| export interface WebsitePageText { | ||
| title: string | ||
| text: string | ||
| /** The page asks not to be indexed (`noindex` or `none` in its robots meta tag). */ | ||
| noindex: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Reads a rendered HTML page as structured text: headings, lists and tables survive, scripts and | ||
| * styles do not, and the `<main>` or `<article>` region is preferred over the whole body. The | ||
| * shared HTML size and markup limits apply before the page is parsed. | ||
| */ | ||
| export function extractPageText(html: string): WebsitePageText { | ||
| assertHtmlStringWithinLimits(html) | ||
| const $ = cheerio.load(html) | ||
| const directives = $(`meta[name="robots" i], meta[name="${WEBSITE_CRAWLER_TOKEN}" i]`) | ||
| .map((_, element) => $(element).attr('content') ?? '') | ||
| .get() | ||
| .join(',') | ||
| const title = | ||
| $('meta[property="og:title"]').attr('content')?.trim() || | ||
| $('title').first().text().trim() || | ||
| $('h1').first().text().trim() | ||
| $(CHROME_SELECTOR).remove() | ||
| const region = ['main', 'article'].find((selector) => $(selector).length > 0) ?? 'body' | ||
| const regionText = loadedHtmlToStructuredText($, region) | ||
|
waleedlatif1 marked this conversation as resolved.
Resolved
|
||
| // A client-rendered page leaves <main>/<article> empty; fall back to the body. | ||
| const text = regionText || region === 'body' ? regionText : loadedHtmlToStructuredText($) | ||
| return { title, text, noindex: /\b(noindex|none)\b/i.test(directives) } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { parseRobots } from '@/connectors/website/robots' | ||
|
|
||
| describe('robots.txt rules for the website connector', () => { | ||
| it('lets the longest matching rule decide, with Allow winning a tie', () => { | ||
| const robots = parseRobots( | ||
| [ | ||
| 'User-agent: *', | ||
| 'Disallow: /private', | ||
| 'Allow: /private/handbook', | ||
| 'Disallow: /tie', | ||
| 'Allow: /tie', | ||
| ].join('\n') | ||
| ) | ||
| expect(robots.allows('/private/salaries')).toBe(false) | ||
| expect(robots.allows('/private/handbook/leave')).toBe(true) | ||
| expect(robots.allows('/tie')).toBe(true) | ||
| expect(parseRobots('User-agent: *\nAllow: /foo\nDisallow: /foo$').allows('/foo')).toBe(true) | ||
| expect(robots.allows('/public')).toBe(true) | ||
| }) | ||
|
|
||
| it('applies only the group naming this crawler when one exists', () => { | ||
| const robots = parseRobots( | ||
| [ | ||
| 'User-agent: *', | ||
| 'Disallow: /', | ||
| '', | ||
| 'User-agent: OtherBot', | ||
| 'User-agent: SimBot', | ||
| 'Disallow: /drafts', | ||
| ].join('\n') | ||
| ) | ||
| expect(robots.allows('/benefits')).toBe(true) | ||
| expect(robots.allows('/drafts/plan')).toBe(false) | ||
| }) | ||
|
|
||
| it('matches wildcards and end anchors against the path and query', () => { | ||
| const robots = parseRobots( | ||
| ['User-agent: *', 'Disallow: /*?print=', 'Disallow: /*.pdf$'].join('\n') | ||
| ) | ||
| expect(robots.allows('/plans?print=1')).toBe(false) | ||
| expect(robots.allows('/guide.pdf')).toBe(false) | ||
| expect(robots.allows('/guide.pdf.html')).toBe(true) | ||
| }) | ||
|
|
||
| it('treats an empty Disallow as allowing everything and collects declared sitemaps', () => { | ||
| const robots = parseRobots( | ||
| ['Sitemap: https://example.com/a.xml', 'User-agent: *', 'Disallow:', '# Disallow: /x'].join( | ||
| '\n' | ||
| ) | ||
| ) | ||
| expect(robots.allows('/x')).toBe(true) | ||
| expect(robots.sitemaps).toEqual(['https://example.com/a.xml']) | ||
| }) | ||
|
|
||
| it('matches rules and paths in one canonical encoding, so an escape cannot slip past a rule', () => { | ||
| const robots = parseRobots( | ||
| ['User-agent: *', 'Disallow: /private', 'Disallow: /café'].join('\n') | ||
| ) | ||
| expect(robots.allows('/%70rivate/salaries')).toBe(false) | ||
| expect(robots.allows('/caf%C3%A9/menu')).toBe(false) | ||
| expect(robots.allows('/a%2Fprivate')).toBe(true) | ||
| }) | ||
|
|
||
| it('matches a pathological wildcard rule against a long path in linear time', () => { | ||
| const robots = parseRobots(`User-agent: *\nDisallow: /${'*a'.repeat(40)}b\n`) | ||
| const started = performance.now() | ||
| expect(robots.allows(`/${'a'.repeat(20_000)}`)).toBe(true) | ||
| expect(performance.now() - started).toBeLessThan(1000) | ||
| }) | ||
|
|
||
| it('reads records separated by carriage returns alone', () => { | ||
| expect(parseRobots('User-agent: *\rDisallow: /\r').allows('/anything')).toBe(false) | ||
| }) | ||
|
|
||
| it('refuses a policy with more rules than it evaluates rather than dropping some', () => { | ||
| const rules = Array.from({ length: 1001 }, (_, index) => `Disallow: /p${index}`) | ||
| expect(() => parseRobots(['User-agent: *', ...rules].join('\n'))).toThrow('more than') | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.