UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

41 lines (40 loc) 1.49 kB
/** * Replaces a source tag with the actual source web UI. * * To give sources a number, the source replacer holds the current source count. */ export class SourceReplacer { constructor(renderer, factory, counter) { this.renderer = renderer; this.factory = factory; this.counter = counter; } async replacement(context, original) { const outputDoc = context.file.document; const replacement = outputDoc.createElement("span"); replacement.className = "source-id"; replacement.ariaLabel = "Source"; replacement.ariaHidden = "true"; const sourceId = this.counter.next(context); replacement.textContent = sourceId; const sourceContentsEl = outputDoc.createElement("span"); sourceContentsEl.className = "source-contents"; await this.content(context, original, sourceContentsEl); const anchor = outputDoc.createElement("span"); anchor.id = sourceId; anchor.ariaHidden = "true"; anchor.className = "anchor"; replacement.append(anchor, sourceContentsEl); return replacement; } async content(context, original, container) { const href = original.href; if (href) { const source = await this.factory.create(context, href); await this.renderer.renderContent(context, source, container); } else { container.innerHTML = original.innerHTML; } } }