UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

67 lines (66 loc) 2.42 kB
export class DataAnchorHandler { constructor(dataService) { this.dataService = dataService; /** * Source counter in the page. */ this.number = 0; } async handle(context, linkEl, pathToSearch) { const dataList = await this.dataService.getFromDir(pathToSearch, ["api", "product", "org"], ["index.json"]); for (const data of dataList) { const type = data === null || data === void 0 ? void 0 : data.type; switch (type) { case "api": this.handleApi(context, data, linkEl); break; case "product": this.handleProduct(context, data, linkEl); break; case "org": this.handleOrg(context, data, linkEl); break; } } } handleNote(context, linkEl, note) { this.number++; const noteStr = this.number.toString(); const noteId = `deprecated-${noteStr}`; const outputDoc = context.file.document; const replacement = outputDoc.createElement("span"); replacement.className = "note-id"; replacement.ariaLabel = "Note"; replacement.textContent = "d" + noteStr; const contents = outputDoc.createElement("span"); contents.className = "note-contents"; contents.innerHTML = note; const anchor = outputDoc.createElement("span"); anchor.id = noteId; anchor.className = "anchor"; replacement.append(anchor, contents); linkEl.parentNode.insertBefore(replacement, linkEl.nextSibling); } handleApi(context, data, linkEl) { const next = data.next; if (linkEl.classList.contains("deprecated")) { return; } if (next) { linkEl.classList.add("deprecated"); let notes = data.notes; if ((notes === null || notes === void 0 ? void 0 : notes.length) <= 0) { notes = [`Remplacé par ${next}`]; } for (const note of notes) { this.handleNote(context, linkEl, note); } } } handleProduct(context, data, linkEl) { return this.handleApi(context, data, linkEl); } handleOrg(context, data, linkEl) { return this.handleApi(context, data, linkEl); } }