Skip to content

Commit 428f30e

Browse files
authored
feat(connectors): add a website connector that syncs pages from a site's sitemaps (#10536)
* feat(connectors): add a website connector that syncs pages from a site's sitemaps * fix(connectors): parse sitemaps with a streaming XML parser, pin the scheme, withdraw excluded pages, one request header * fix(connectors): read only an entry's own sitemap location, reject malformed sitemaps, match robots in linear time, pin the port * fix(connectors): rank robots rules by the path they name, without the end anchor * fix(connectors): split robots.txt on lone carriage returns and bound the rules it evaluates * docs(connectors): document the website connector's exported types * fix(connectors): withdraw a website page the HTML parser cannot read instead of failing it * docs(connectors): document the website connector exports * fix(connectors): order website listings by URL and never reconcile a listing rebuilt mid-sync
1 parent 0c4ac4a commit 428f30e

16 files changed

Lines changed: 1514 additions & 7 deletions

File tree

‎apps/docs/components/icons.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4898,6 +4898,27 @@ export function WealthboxIcon(props: SVGProps<SVGSVGElement>) {
48984898
)
48994899
}
49004900

4901+
export function WebsiteIcon(props: SVGProps<SVGSVGElement>) {
4902+
return (
4903+
<svg
4904+
{...props}
4905+
xmlns='http://www.w3.org/2000/svg'
4906+
width='24'
4907+
height='24'
4908+
viewBox='0 0 24 24'
4909+
fill='none'
4910+
stroke='currentColor'
4911+
strokeWidth='2'
4912+
strokeLinecap='round'
4913+
strokeLinejoin='round'
4914+
>
4915+
<circle cx='12' cy='12' r='10' />
4916+
<path d='M2 12h20' />
4917+
<path d='M12 2a15 15 0 0 1 4 10 15 15 0 0 1-4 10 15 15 0 0 1-4-10 15 15 0 0 1 4-10z' />
4918+
</svg>
4919+
)
4920+
}
4921+
49014922
export function WebhookIcon(props: SVGProps<SVGSVGElement>) {
49024923
return (
49034924
<svg

‎apps/sim/components/icons.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4898,6 +4898,27 @@ export function WealthboxIcon(props: SVGProps<SVGSVGElement>) {
48984898
)
48994899
}
49004900

4901+
export function WebsiteIcon(props: SVGProps<SVGSVGElement>) {
4902+
return (
4903+
<svg
4904+
{...props}
4905+
xmlns='http://www.w3.org/2000/svg'
4906+
width='24'
4907+
height='24'
4908+
viewBox='0 0 24 24'
4909+
fill='none'
4910+
stroke='currentColor'
4911+
strokeWidth='2'
4912+
strokeLinecap='round'
4913+
strokeLinejoin='round'
4914+
>
4915+
<circle cx='12' cy='12' r='10' />
4916+
<path d='M2 12h20' />
4917+
<path d='M12 2a15 15 0 0 1 4 10 15 15 0 0 1-4 10 15 15 0 0 1-4-10 15 15 0 0 1 4-10z' />
4918+
</svg>
4919+
)
4920+
}
4921+
49014922
export function WebhookIcon(props: SVGProps<SVGSVGElement>) {
49024923
return (
49034924
<svg

‎apps/sim/connectors/registry.server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { trelloConnector } from '@/connectors/trello'
6161
import { typeformConnector } from '@/connectors/typeform'
6262
import type { ConnectorRegistry } from '@/connectors/types'
6363
import { webflowConnector } from '@/connectors/webflow'
64+
import { websiteConnector } from '@/connectors/website'
6465
import { wordpressConnector } from '@/connectors/wordpress'
6566
import { workdayConnector } from '@/connectors/workday'
6667
import { xConnector } from '@/connectors/x'
@@ -139,6 +140,7 @@ export const CONNECTOR_REGISTRY: ConnectorRegistry = {
139140
trello: trelloConnector,
140141
typeform: typeformConnector,
141142
webflow: webflowConnector,
143+
website: websiteConnector,
142144
wordpress: wordpressConnector,
143145
workday: workdayConnector,
144146
x: xConnector,

‎apps/sim/connectors/registry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { trelloConnectorMeta } from '@/connectors/trello/meta'
6161
import { typeformConnectorMeta } from '@/connectors/typeform/meta'
6262
import type { ConnectorMeta, ConnectorMetaRegistry } from '@/connectors/types'
6363
import { webflowConnectorMeta } from '@/connectors/webflow/meta'
64+
import { websiteConnectorMeta } from '@/connectors/website/meta'
6465
import { wordpressConnectorMeta } from '@/connectors/wordpress/meta'
6566
import { workdayConnectorMeta } from '@/connectors/workday/meta'
6667
import { xConnectorMeta } from '@/connectors/x/meta'
@@ -139,6 +140,7 @@ export const CONNECTOR_META_REGISTRY: ConnectorMetaRegistry = {
139140
trello: trelloConnectorMeta,
140141
typeform: typeformConnectorMeta,
141142
webflow: webflowConnectorMeta,
143+
website: websiteConnectorMeta,
142144
wordpress: wordpressConnectorMeta,
143145
workday: workdayConnectorMeta,
144146
x: xConnectorMeta,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { websiteConnector } from '@/connectors/website/website'
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { WebsiteIcon } from '@/components/icons'
2+
import type { ConnectorMeta } from '@/connectors/types'
3+
4+
/** Pages indexed per sync when `maxPages` is not configured. */
5+
export const WEBSITE_DEFAULT_MAX_PAGES = 500
6+
7+
/** Hard ceiling on the configurable `maxPages` value. */
8+
export const WEBSITE_MAX_PAGES_LIMIT = 5000
9+
10+
/** Client-safe metadata for the website connector: its setup fields, auth and tags. */
11+
export const websiteConnectorMeta: ConnectorMeta = {
12+
id: 'website',
13+
name: 'Website',
14+
description: 'Sync the pages of a website from its sitemap',
15+
version: '1.0.0',
16+
icon: WebsiteIcon,
17+
18+
/**
19+
* Public sites need nothing. A site behind a gateway that accepts a service credential in one
20+
* header, such as an API key, takes one `Name: value` line. Cookies are refused: a session cookie
21+
* is a person's identity, and everything it unlocks would become readable to everyone with
22+
* access to the knowledge base.
23+
*/
24+
auth: {
25+
mode: 'apiKey',
26+
label: 'Request Header',
27+
placeholder: 'Only for sites that require a service header, e.g. X-Api-Key: value',
28+
optional: true,
29+
},
30+
31+
configFields: [
32+
{
33+
id: 'siteUrl',
34+
title: 'Website URL',
35+
type: 'short-input',
36+
placeholder: 'https://www.example.com',
37+
required: true,
38+
description:
39+
'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.',
40+
},
41+
{
42+
id: 'sitemapUrl',
43+
title: 'Sitemap URL',
44+
type: 'short-input',
45+
placeholder: 'e.g. https://www.example.com/pages-sitemap.xml',
46+
required: false,
47+
description: 'A sitemap on the same host to read instead of the discovered ones',
48+
},
49+
{
50+
id: 'includePaths',
51+
title: 'Include Paths',
52+
type: 'short-input',
53+
placeholder: 'e.g. /benefits, /help/*/faq',
54+
required: false,
55+
description:
56+
'Comma-separated path prefixes to sync; * matches any characters. Leave empty to sync every page.',
57+
},
58+
{
59+
id: 'excludePaths',
60+
title: 'Exclude Paths',
61+
type: 'short-input',
62+
placeholder: 'e.g. /blog, /archive',
63+
required: false,
64+
description: 'Comma-separated path prefixes never to sync; * matches any characters.',
65+
},
66+
{
67+
id: 'maxPages',
68+
title: 'Max Pages',
69+
type: 'short-input',
70+
placeholder: `e.g. 200 (default: ${WEBSITE_DEFAULT_MAX_PAGES}, max: ${WEBSITE_MAX_PAGES_LIMIT})`,
71+
required: false,
72+
description: 'Maximum number of pages to index',
73+
},
74+
],
75+
76+
tagDefinitions: [
77+
{ id: 'section', displayName: 'Section', fieldType: 'text' },
78+
{ id: 'lastModified', displayName: 'Last Modified', fieldType: 'date' },
79+
],
80+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as cheerio from 'cheerio'
2+
import {
3+
assertHtmlStringWithinLimits,
4+
loadedHtmlToStructuredText,
5+
} from '@/lib/file-parsers/html-parser'
6+
import { WEBSITE_CRAWLER_TOKEN } from '@/connectors/website/robots'
7+
8+
/** Page-level chrome repeated on every page, which would make every page's text look alike. */
9+
const CHROME_SELECTOR =
10+
'body > header, body > footer, nav, [role="navigation"], [role="banner"], [role="contentinfo"]'
11+
12+
/** What a rendered page contributes to the index: its title, its text, and whether it opts out. */
13+
export interface WebsitePageText {
14+
title: string
15+
text: string
16+
/** The page asks not to be indexed (`noindex` or `none` in its robots meta tag). */
17+
noindex: boolean
18+
}
19+
20+
/**
21+
* Reads a rendered HTML page as structured text: headings, lists and tables survive, scripts and
22+
* styles do not, and the `<main>` or `<article>` region is preferred over the whole body. The
23+
* shared HTML size and markup limits apply before the page is parsed.
24+
*/
25+
export function extractPageText(html: string): WebsitePageText {
26+
assertHtmlStringWithinLimits(html)
27+
const $ = cheerio.load(html)
28+
const directives = $(`meta[name="robots" i], meta[name="${WEBSITE_CRAWLER_TOKEN}" i]`)
29+
.map((_, element) => $(element).attr('content') ?? '')
30+
.get()
31+
.join(',')
32+
const title =
33+
$('meta[property="og:title"]').attr('content')?.trim() ||
34+
$('title').first().text().trim() ||
35+
$('h1').first().text().trim()
36+
$(CHROME_SELECTOR).remove()
37+
const region = ['main', 'article'].find((selector) => $(selector).length > 0) ?? 'body'
38+
const regionText = loadedHtmlToStructuredText($, region)
39+
// A client-rendered page leaves <main>/<article> empty; fall back to the body.
40+
const text = regionText || region === 'body' ? regionText : loadedHtmlToStructuredText($)
41+
return { title, text, noindex: /\b(noindex|none)\b/i.test(directives) }
42+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { parseRobots } from '@/connectors/website/robots'
3+
4+
describe('robots.txt rules for the website connector', () => {
5+
it('lets the longest matching rule decide, with Allow winning a tie', () => {
6+
const robots = parseRobots(
7+
[
8+
'User-agent: *',
9+
'Disallow: /private',
10+
'Allow: /private/handbook',
11+
'Disallow: /tie',
12+
'Allow: /tie',
13+
].join('\n')
14+
)
15+
expect(robots.allows('/private/salaries')).toBe(false)
16+
expect(robots.allows('/private/handbook/leave')).toBe(true)
17+
expect(robots.allows('/tie')).toBe(true)
18+
expect(parseRobots('User-agent: *\nAllow: /foo\nDisallow: /foo$').allows('/foo')).toBe(true)
19+
expect(robots.allows('/public')).toBe(true)
20+
})
21+
22+
it('applies only the group naming this crawler when one exists', () => {
23+
const robots = parseRobots(
24+
[
25+
'User-agent: *',
26+
'Disallow: /',
27+
'',
28+
'User-agent: OtherBot',
29+
'User-agent: SimBot',
30+
'Disallow: /drafts',
31+
].join('\n')
32+
)
33+
expect(robots.allows('/benefits')).toBe(true)
34+
expect(robots.allows('/drafts/plan')).toBe(false)
35+
})
36+
37+
it('matches wildcards and end anchors against the path and query', () => {
38+
const robots = parseRobots(
39+
['User-agent: *', 'Disallow: /*?print=', 'Disallow: /*.pdf$'].join('\n')
40+
)
41+
expect(robots.allows('/plans?print=1')).toBe(false)
42+
expect(robots.allows('/guide.pdf')).toBe(false)
43+
expect(robots.allows('/guide.pdf.html')).toBe(true)
44+
})
45+
46+
it('treats an empty Disallow as allowing everything and collects declared sitemaps', () => {
47+
const robots = parseRobots(
48+
['Sitemap: https://example.com/a.xml', 'User-agent: *', 'Disallow:', '# Disallow: /x'].join(
49+
'\n'
50+
)
51+
)
52+
expect(robots.allows('/x')).toBe(true)
53+
expect(robots.sitemaps).toEqual(['https://example.com/a.xml'])
54+
})
55+
56+
it('matches rules and paths in one canonical encoding, so an escape cannot slip past a rule', () => {
57+
const robots = parseRobots(
58+
['User-agent: *', 'Disallow: /private', 'Disallow: /café'].join('\n')
59+
)
60+
expect(robots.allows('/%70rivate/salaries')).toBe(false)
61+
expect(robots.allows('/caf%C3%A9/menu')).toBe(false)
62+
expect(robots.allows('/a%2Fprivate')).toBe(true)
63+
})
64+
65+
it('matches a pathological wildcard rule against a long path in linear time', () => {
66+
const robots = parseRobots(`User-agent: *\nDisallow: /${'*a'.repeat(40)}b\n`)
67+
const started = performance.now()
68+
expect(robots.allows(`/${'a'.repeat(20_000)}`)).toBe(true)
69+
expect(performance.now() - started).toBeLessThan(1000)
70+
})
71+
72+
it('reads records separated by carriage returns alone', () => {
73+
expect(parseRobots('User-agent: *\rDisallow: /\r').allows('/anything')).toBe(false)
74+
})
75+
76+
it('refuses a policy with more rules than it evaluates rather than dropping some', () => {
77+
const rules = Array.from({ length: 1001 }, (_, index) => `Disallow: /p${index}`)
78+
expect(() => parseRobots(['User-agent: *', ...rules].join('\n'))).toThrow('more than')
79+
})
80+
})

0 commit comments

Comments
 (0)