feat: internal links widget with counts and suggestions

- E10 golem15-seo-links in the SEO tab after the graph preview (types
  with a body): inbound and outbound counts from the last finished crawl,
  the orphan warning, up to 10 pages to link to (Copy address) and 10
  that could link here
- classes/linkgraph.go builds the report from the crawl's pages and
  links; the crawler's orphan suggestions share NormalText
- GET /_seo/api/v1/links and seo_get_link_suggestions; widget action
  seo_links mapped in AdminOperations
- element bundle rebuilt
This commit is contained in:
Jakub Zych
2026-11-04 16:40:16 +01:00
parent ab4e83416d
commit 49cc241f58
20 changed files with 1131 additions and 12 deletions

2
assets/dist/seo.css vendored

File diff suppressed because one or more lines are too long

4
assets/dist/seo.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,7 @@
import './styles.css'
import { GRAPH_TAG, SeoGraphElement } from './graph'
import { IMAGE_TAG, SeoImageElement } from './image'
import { LINKS_TAG, SeoLinksElement } from './links'
import { PARENT_TAG, SeoParentElement } from './parent'
import { ROBOTS_TAG, SeoRobotsElement } from './robots'
import { ROBOTSTESTER_TAG, SeoRobotsTesterElement } from './robotstester'
@@ -24,6 +25,7 @@ const elements: Array<[string, CustomElementConstructor]> = [
['golem15-seo-schema', SeoSchemaElement],
['golem15-seo-graph', SeoGraphElement],
['golem15-seo-parent', SeoParentElement],
['golem15-seo-links', SeoLinksElement],
]
for (const [tag, ctor] of elements) {
if (!customElements.get(tag)) {
@@ -34,9 +36,11 @@ for (const [tag, ctor] of elements) {
export {
GRAPH_TAG,
IMAGE_TAG,
LINKS_TAG,
PARENT_TAG,
SCHEMA_TAG,
SeoGraphElement,
SeoLinksElement,
SeoParentElement,
SeoSchemaElement,
ROBOTS_TAG,

View File

@@ -1,5 +1,6 @@
// Element copy of the SEO field elements (UI-SPEC copywriting E1 to E6,
// E7 for the social image, E8 and E9 for structured data), chosen by the ui-locale attribute. Plural
// E7 for the social image, E8 and E9 for structured data, E10 for internal
// links), chosen by the ui-locale attribute. Plural
// texts are CLDR maps; placeholders use :name.
/** A CLDR plural map: one/other in English, one/few/many/other in Polish. */
@@ -184,6 +185,22 @@ export interface Labels {
layerNodes: string
graphAction: string
graphFailed: string
// E10 golem15-seo-links
linksInbound: string
linksOutbound: string
linksCrawled: string
linksNotCrawled: string
linksOrphanTitle: string
linksOrphanText: string
linksOut: string
linksIn: string
linksPhrase: string
linksCopy: string
linksCopied: string
linksNone: string
linksNoRecord: string
linksChecking: string
linksFailed: string
// golem15-seo-parent
primaryDefault: string
}
@@ -353,6 +370,21 @@ const en: Labels = {
layerNodes: 'The site may add nodes of its own, such as FAQPage. The site audit checks the rendered result.',
graphAction: 'Check structured data',
graphFailed: 'The structured data could not be built. Try again.',
linksInbound: 'Links to this page',
linksOutbound: 'Links from this page',
linksCrawled: 'From the crawl of :time.',
linksNotCrawled: 'Link counts appear after the first crawl (SEO › Site audit).',
linksOrphanTitle: 'No page links here',
linksOrphanText: 'Search engines and visitors may not find this page. Link to it from one of the pages below.',
linksOut: 'Pages you could link to from this text',
linksIn: 'Pages that could link here',
linksPhrase: '“:phrase” → :title',
linksCopy: 'Copy address',
linksCopied: 'Address copied.',
linksNone: 'No suggestions. They come from titles and focus keyphrases found in the saved text.',
linksNoRecord: 'Suggestions appear after the first save.',
linksChecking: 'Loading the internal links…',
linksFailed: 'The internal links could not be loaded. Try again.',
primaryDefault: 'Default (first category)',
}
@@ -531,6 +563,21 @@ const pl: Labels = {
layerNodes: 'Witryna może dodać własne węzły, np. FAQPage. Audyt witryny sprawdza wynik na stronie.',
graphAction: 'Sprawdź dane strukturalne',
graphFailed: 'Nie udało się zbudować danych strukturalnych. Spróbuj ponownie.',
linksInbound: 'Linki do tej strony',
linksOutbound: 'Linki z tej strony',
linksCrawled: 'Według skanowania z :time.',
linksNotCrawled: 'Liczby linków pojawią się po pierwszym skanowaniu (SEO › Audyt witryny).',
linksOrphanTitle: 'Żadna strona tu nie linkuje',
linksOrphanText: 'Wyszukiwarki i odwiedzający mogą nie znaleźć tej strony. Dodaj do niej link z jednej ze stron poniżej.',
linksOut: 'Strony, do których możesz dodać link w tym tekście',
linksIn: 'Strony, które mogą tu linkować',
linksPhrase: '„:phrase” → :title',
linksCopy: 'Kopiuj adres',
linksCopied: 'Skopiowano adres.',
linksNone: 'Brak propozycji. Powstają z tytułów i fraz kluczowych znalezionych w zapisanym tekście.',
linksNoRecord: 'Propozycje pojawią się po pierwszym zapisie.',
linksChecking: 'Wczytywanie linków wewnętrznych…',
linksFailed: 'Nie udało się wczytać linków wewnętrznych. Spróbuj ponownie.',
primaryDefault: 'Domyślna (pierwsza kategoria)',
}

View File

@@ -0,0 +1,261 @@
import { ActionQueue } from './actions'
import { el, fmt, icon, labelsFor, num, uiLocaleOf, type Labels } from './labels'
export const LINKS_TAG = 'golem15-seo-links'
/** A page this text could link to. */
export interface LinkOut {
phrase: string
title: string
url: string
}
/** A page that could link here. */
export interface LinkIn {
phrase: string
title: string
adminUrl: string
}
/** The seo_links action's answer (UI-SPEC C19). */
export interface LinksData {
crawledAt: string | null
inbound: number | null
outbound: number | null
orphan: boolean
out: LinkOut[]
in: LinkIn[]
}
interface Payload {
op: 'links'
locale: string
}
/** Whether the page can write to the clipboard (a secure context). */
function canCopy(): boolean {
return typeof navigator !== 'undefined' && !!navigator.clipboard && typeof navigator.clipboard.writeText === 'function'
}
/** A crawl time in the ui-locale. */
export function crawlTime(value: string, locale: string): string {
const d = new Date(value)
if (Number.isNaN(d.getTime())) {
return value
}
try {
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }).format(d)
} catch {
return d.toISOString()
}
}
/**
* E10 golem15-seo-links (UI-SPEC C19, D-82): the page's inbound and
* outbound link counts from the last crawl, the orphan warning, up to 10
* pages this text could link to (with Copy address) and 10 pages that
* could link here. It asks through summer-action only ({op: links,
* locale}), on connect and on the ML locale switch, and changes nothing
* itself.
*/
export class SeoLinksElement extends HTMLElement {
static observedAttributes = ['busy', 'state', 'ui-locale', 'disabled']
private data: LinksData | null = null
private failed = false
private pending = false
private shownLocale = ''
private copied = ''
private copiedTimer: ReturnType<typeof setTimeout> | undefined
private connected = false
private readonly actions = new ActionQueue<Payload>(this, () => this.fail())
private readonly onResult = (ev: Event): void => {
const detail = (ev as CustomEvent).detail as { data?: unknown } | undefined
this.actions.settle()
const data = detail?.data as LinksData | undefined
if (data && Array.isArray(data.out) && Array.isArray(data.in)) {
this.data = data
this.failed = false
}
this.pending = this.actions.current !== null
this.render()
}
private readonly onLocale = (ev: Event): void => {
const locale = (ev as CustomEvent).detail
if (typeof locale === 'string' && locale !== '' && locale !== this.shownLocale) {
this.shownLocale = locale
this.load()
}
}
connectedCallback(): void {
this.connected = true
this.addEventListener('summer-result', this.onResult)
window.addEventListener('summer:ml-locale', this.onLocale)
this.render()
queueMicrotask(() => {
if (this.connected) {
this.load()
}
})
}
disconnectedCallback(): void {
this.connected = false
this.removeEventListener('summer-result', this.onResult)
window.removeEventListener('summer:ml-locale', this.onLocale)
clearTimeout(this.copiedTimer)
this.actions.reset()
this.pending = false
}
attributeChangedCallback(name: string, _old: string | null, value: string | null): void {
if (!this.connected) {
return
}
if (name === 'busy' && value === null) {
this.actions.flush()
return
}
if (name === 'state' && value === 'error') {
if (this.actions.settle()) {
this.fail()
}
return
}
this.render()
}
private labels(): Labels {
return labelsFor(uiLocaleOf(this))
}
/** Asks for the links of the shown locale (one queued request at most). */
load(): void {
this.pending = true
this.actions.request({ op: 'links', locale: this.shownLocale })
this.render()
}
private fail(): void {
this.failed = true
this.pending = this.actions.current !== null
this.render()
}
/** Writes url to the clipboard and says so for 2 s. */
private copy(url: string): void {
if (!canCopy()) {
return
}
void navigator.clipboard.writeText(url).then(() => {
this.copied = url
clearTimeout(this.copiedTimer)
this.copiedTimer = setTimeout(() => {
this.copied = ''
this.render()
}, 2000)
this.render()
}, () => undefined)
}
/** The suggestion line “:phrase” → :title with the title as a link (or text). */
private line(labels: Labels, phrase: string, title: string, href: string): HTMLElement {
const line = el('span', 'seo-links__line')
const [before, after = ''] = labels.linksPhrase.split(':title')
line.append(fmt(before ?? '', { phrase }))
if (href) {
const a = el('a', 'seo-links__title', title)
a.href = href
a.target = '_blank'
a.rel = 'noopener'
line.append(a)
} else {
line.append(el('span', 'seo-links__title', title))
}
line.append(fmt(after, { phrase }))
return line
}
private render(): void {
if (!this.connected) {
return
}
const labels = this.labels()
const locale = uiLocaleOf(this)
const sections: HTMLElement[] = []
this.setAttribute('aria-busy', String(this.pending))
if (this.pending) {
const line = el('p', 'seo-links__meta seo-links__checking', labels.linksChecking)
line.setAttribute('role', 'status')
sections.push(line)
}
const data = this.data
if (data) {
const counts = el('div', 'seo-links__counts')
if (data.crawledAt !== null && data.inbound !== null && data.outbound !== null) {
const stats = el('div', 'summer-stats')
for (const [value, label] of [
[data.inbound, labels.linksInbound],
[data.outbound, labels.linksOutbound],
] as Array<[number, string]>) {
const stat = el('div', 'summer-stat')
stat.append(el('span', 'summer-stat__value', num(value, locale)), el('span', 'summer-stat__label', label))
stats.append(stat)
}
counts.append(stats, el('p', 'seo-links__meta', fmt(labels.linksCrawled, { time: crawlTime(data.crawledAt, locale) })))
} else {
counts.append(el('p', 'seo-links__meta seo-links__not-crawled', labels.linksNotCrawled))
}
sections.push(counts)
if (data.orphan) {
const callout = el('div', 'summer-callout summer-callout--warning seo-links__orphan')
callout.append(el('p', 'summer-callout__title', labels.linksOrphanTitle), el('p', 'summer-callout__text', labels.linksOrphanText))
sections.push(callout)
}
const copyable = canCopy()
const out = el('div', 'seo-links__list seo-links__list--out')
out.append(el('p', 'seo-links__heading', labels.linksOut))
for (const s of data.out.slice(0, 10)) {
const row = el('div', 'seo-links__row')
row.append(this.line(labels, s.phrase, s.title, s.url))
if (copyable && !this.hasAttribute('disabled')) {
const button = el('button', 'seo-links__copy', labels.linksCopy)
button.type = 'button'
button.addEventListener('click', () => this.copy(s.url))
row.append(button)
} else {
row.append(el('code', 'seo-links__url', s.url))
}
out.append(row)
}
if (data.out.length === 0) {
out.append(el('p', 'seo-links__meta seo-links__none', labels.linksNone))
}
sections.push(out)
const inbound = el('div', 'seo-links__list seo-links__list--in')
inbound.append(el('p', 'seo-links__heading', labels.linksIn))
for (const s of data.in.slice(0, 10)) {
const row = el('div', 'seo-links__row')
row.append(this.line(labels, s.phrase, s.title, s.adminUrl))
inbound.append(row)
}
if (data.in.length === 0) {
inbound.append(el('p', 'seo-links__meta seo-links__none', labels.linksNone))
}
sections.push(inbound)
}
if (this.failed) {
const line = el('p', 'seo-links__error')
line.setAttribute('role', 'status')
line.append(icon('circle-alert'), el('span', '', labels.linksFailed))
sections.push(line)
}
const status = el('p', 'seo-links__status', this.copied ? labels.linksCopied : '')
status.setAttribute('role', 'status')
sections.push(status)
this.replaceChildren(...sections)
}
}

View File

@@ -1126,3 +1126,116 @@ golem15-seo-graph .seo-graph__button {
golem15-seo-graph .seo-graph__button:hover {
background: var(--c-subtle);
}
/* E10 golem15-seo-links (UI-SPEC C19) */
golem15-seo-links {
display: flex;
flex-direction: column;
gap: 24px;
}
golem15-seo-links .seo-links__counts,
golem15-seo-links .seo-links__list {
display: flex;
flex-direction: column;
gap: 8px;
}
golem15-seo-links .seo-links__counts {
gap: 16px;
}
golem15-seo-links .seo-links__meta {
margin: 0;
font-size: 13px;
line-height: 1.5;
color: var(--c-muted);
}
golem15-seo-links .seo-links__heading {
margin: 0;
font-size: 14px;
font-weight: 600;
line-height: 1.5;
color: var(--c-text);
}
golem15-seo-links .seo-links__row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
font-size: 14px;
line-height: 1.5;
color: var(--c-text);
}
@media (max-width: 600px) {
golem15-seo-links .seo-links__row {
flex-direction: column;
align-items: flex-start;
}
}
golem15-seo-links .seo-links__line {
min-width: 0;
overflow-wrap: anywhere;
}
golem15-seo-links .seo-links__title {
color: var(--c-text);
text-decoration: underline;
}
golem15-seo-links .seo-links__url {
font-family: 'DM Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
color: var(--c-muted);
user-select: all;
overflow-wrap: anywhere;
}
golem15-seo-links .seo-links__copy {
flex: none;
height: 34px;
padding: 0 12px;
border: 1px solid var(--c-border-strong);
border-radius: 8px;
background: var(--c-surface);
color: var(--c-text);
font: inherit;
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background-color 150ms;
}
golem15-seo-links .seo-links__copy:hover {
background: var(--c-subtle);
}
golem15-seo-links .seo-links__copy:focus-visible,
golem15-seo-links .seo-links__title:focus-visible {
outline: none;
box-shadow: 0 0 0 3px var(--c-ring);
}
golem15-seo-links .seo-links__error {
display: flex;
align-items: center;
gap: 8px;
margin: 0;
font-size: 13px;
line-height: 1.5;
color: var(--c-danger);
}
golem15-seo-links .seo-links__status {
margin: 0;
font-size: 13px;
color: var(--c-muted);
}
golem15-seo-links .seo-links__status:empty {
display: none;
}