"use strict"; /** * LNReader-plugin voor de zelfgehoste HFY-Novels-index. * * De catalogus (titels, auteurs, hoofdstuklijsten) komt uit een statische * JSON-index. De verhaaltekst zelf staat niet in die index: parseChapter * haalt een hoofdstuk live op via de Arctic Shift-API (optioneel te * overschrijven in de plugin-instellingen). * * Dit bestand is CommonJS, zoals de officiƫle gecompileerde plugins: * require(...) uit de LNReader-allowlist, en `exports.default = instance`. */ Object.defineProperty(exports, "__esModule", { value: true }); const { fetchApi } = require("@libs/fetch"); const { FilterTypes } = require("@libs/filterInputs"); const { defaultCover } = require("@libs/defaultCover"); const { NovelStatus } = require("@libs/novelStatus"); const { storage } = require("@libs/storage"); const DEFAULT_INDEX_BASE = "https://git.timfalken.com/timfa/hfy-novels/-/raw/public"; const DEFAULT_INDEX_REV = "master"; const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com"; const PAGE_FALLBACK_SIZE = 50; function setting(key, fallback) { try { const value = storage.get(key); if (value === undefined || value === null || value === "") { return fallback; } return value; } catch (err) { return fallback; } } function joinUrl(base, path, query) { const trimmedBase = String(base || "").replace(/\/+$/, ""); const trimmedPath = String(path || "").replace(/^\/+/, ""); let url = trimmedBase + "/" + trimmedPath; const params = []; if (query && typeof query === "object") { for (const [k, v] of Object.entries(query)) { if (v !== undefined && v !== null && v !== "") { params.push(encodeURIComponent(k) + "=" + encodeURIComponent(String(v))); } } } if (params.length) { url += (url.includes("?") ? "&" : "?") + params.join("&"); } return url; } function statusFrom(text) { switch (text) { case "Completed": return NovelStatus.Completed; case "Ongoing": return NovelStatus.Ongoing; case "On Hiatus": return NovelStatus.OnHiatus; default: return NovelStatus.Unknown; } } async function readJson(url, label) { const res = await fetchApi(url); if (!res || res.ok === false) { const status = res && res.status != null ? res.status : "?"; throw new Error("HFY Novels: kon " + label + " niet laden (HTTP " + status + ")"); } if (typeof res.json === "function") { return await res.json(); } const text = await res.text(); return JSON.parse(text); } function toNovelItem(entry, plugin) { return { name: entry.name, path: entry.path, cover: plugin.coverUrl(entry.cover), }; } class HFYNovelsPlugin { constructor() { this.id = "hfyNovels"; this.name = "HFY Novels"; this.version = "0.1.0"; this.site = "https://www.reddit.com/r/HFY/"; this.icon = "icon.png"; this.filters = { kind: { value: "all", label: "Type", options: [ { label: "Alles", value: "all" }, { label: "Reeksen", value: "series" }, { label: "Oneshots", value: "oneshot" }, ], type: FilterTypes.Picker, }, nsfw: { value: "hide", label: "NSFW", options: [ { label: "Verbergen", value: "hide" }, { label: "Tonen", value: "show" }, ], type: FilterTypes.Picker, }, }; this.pluginSettings = { indexBaseUrl: { value: DEFAULT_INDEX_BASE, label: "Basis-URL van de index", type: "Text", }, indexRev: { value: DEFAULT_INDEX_REV, label: "Git-revisie van de index (rev=)", type: "Text", }, chapterSourceUrl: { value: DEFAULT_CHAPTER_SOURCE, label: "Bron voor hoofdstuktekst (Arctic Shift)", type: "Text", }, }; } indexQuery() { const rev = setting("indexRev", DEFAULT_INDEX_REV); return { rev: rev }; } indexUrl(path) { const base = setting("indexBaseUrl", DEFAULT_INDEX_BASE); return joinUrl(base, path, this.indexQuery()); } coverUrl(cover) { if (!cover) { return defaultCover; } if (/^https?:\/\//i.test(cover)) { return cover; } let url = this.indexUrl(cover); // BlueGit serveert SVG pas als afbeelding met ?as=img. if (/\.svg$/i.test(cover) && url.indexOf("as=img") === -1) { url += (url.indexOf("?") === -1 ? "?" : "&") + "as=img"; } return url; } chapterSource() { return String(setting("chapterSourceUrl", DEFAULT_CHAPTER_SOURCE)).replace(/\/+$/, ""); } matchesFilters(entry, filters) { const kind = filters && filters.kind ? filters.kind.value : "all"; const nsfw = filters && filters.nsfw ? filters.nsfw.value : "hide"; if (kind === "oneshot" && !entry.oneshot) { return false; } if (kind === "series" && entry.oneshot) { return false; } if (nsfw === "hide" && entry.nsfw) { return false; } return true; } async loadMeta() { return await readJson(this.indexUrl("catalog-meta.json"), "catalogus-meta"); } async popularNovels(pageNo, options) { const opts = options || {}; const filters = opts.filters || {}; const showLatest = !!opts.showLatestNovels; const page = Math.max(1, parseInt(pageNo, 10) || 1); // Zonder extra filters is de catalogus al op update-datum gesorteerd. const usePagedCatalog = !showLatest && (!filters.kind || filters.kind.value === "all") && (!filters.nsfw || filters.nsfw.value === "hide"); if (usePagedCatalog) { const meta = await this.loadMeta(); const pages = meta.pages || []; const spec = pages[page - 1]; if (!spec) { return []; } const payload = await readJson(this.indexUrl(spec.path), "cataloguspagina " + page); const novels = (payload.novels || []).filter((n) => !n.nsfw); return novels.map((n) => toNovelItem(n, this)); } const search = await readJson(this.indexUrl("search-index.json"), "zoekindex"); let items = (search.novels || []).filter((n) => this.matchesFilters(n, filters)); const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE; const start = (page - 1) * size; return items.slice(start, start + size).map((n) => toNovelItem(n, this)); } async searchNovels(searchTerm, pageNo) { const term = String(searchTerm || "") .trim() .toLowerCase(); if (!term) { return []; } const page = Math.max(1, parseInt(pageNo, 10) || 1); const search = await readJson(this.indexUrl("search-index.json"), "zoekindex"); const hits = (search.novels || []).filter((n) => { const name = String(n.name || "").toLowerCase(); const author = String(n.author || "").toLowerCase(); return name.includes(term) || author.includes(term); }); const size = PAGE_FALLBACK_SIZE; const start = (page - 1) * size; return hits.slice(start, start + size).map((n) => toNovelItem(n, this)); } seriesFileFor(novelPath) { const id = String(novelPath || "") .replace(/^series\//, "") .replace(/\/+$/, ""); if (!id) { throw new Error("HFY Novels: leeg reeks-pad"); } const prefix = id.charAt(0).toLowerCase() || "x"; return "series/" + prefix + "/" + id + ".json"; } async parseNovel(novelPath) { const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath); return { path: data.path || novelPath, name: data.name || "Untitled", author: data.author || "Anonymous", artist: undefined, cover: this.coverUrl(data.cover), genres: data.genres || "HFY", status: statusFrom(data.status), summary: data.summary || "", chapters: (data.chapterList || []).map((ch) => ({ name: ch.name, path: ch.path, releaseTime: ch.releaseTime || null, chapterNumber: ch.chapterNumber, })), }; } async parseChapter(chapterPath) { const id = String(chapterPath || "") .replace(/^chapter\//, "") .trim() .toLowerCase(); if (!/^[0-9a-z]{4,10}$/.test(id)) { throw new Error("HFY Novels: ongeldig hoofdstuk-id"); } const url = this.chapterSource() + "/api/posts/ids?ids=" + encodeURIComponent(id) + "&md2html=true"; const payload = await readJson(url, "hoofdstuk " + id); const posts = payload.data || payload; const post = Array.isArray(posts) ? posts[0] : posts; if (!post) { return "
Dit hoofdstuk is niet (meer) beschikbaar.
"; } const title = post.title ? "by u/" + escapeHtml(post.author) + "
" : ""; const html = post.selftext_html ? sanitizeHtml(post.selftext_html) : markdownToHtml(post.selftext || ""); if (!html) { return title + by + "Deze post heeft geen tekst (verwijderd of een link-post).
"; } return title + by + html; } } function escapeHtml(text) { return String(text) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } function sanitizeHtml(html) { // Arctic Shift levert al HTML; we strippen scripts en on-event-attributen. return String(html) .replace(/