// THE REDACTION A BUILD PUBLISHES WITH (post-redact.mjs): the corpus's terms, // each post's channel markup and the project's own list, read together, and // the cut's slur filter. Read // only -- the corpus's two files are the editor's to write. // // /redactions.json [{ term, wholeWord?, note? }] (or { terms: […] }) // /channels//posts-markup.json { "": { redact: […] } } // // A file that is there and does not parse, or is refused by its validator, // FAILS the read: a public render that silently ignored its redaction would // publish what it was told to blank. A file that is not there is nothing. // // And the SLUR FILTER (post-slurs.mjs): the default list shipped beside this // module (slurs.json) under the project's `render.posts.slurs` -- off for an // uncensored cut (`{ uncensored: true }`: build-video's --uncensored, Play's // Uncensored), the redaction itself untouched by it. import { readFileSync } from "node:fs"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { normalizeTerms, projectRedactOf, validatePostsMarkup, validateRedactTerms } from "./post-redact.mjs"; import { resolveSlurs, slurListErrors } from "./post-slurs.mjs"; const HERE = path.dirname(/* turbopackIgnore: true */ fileURLToPath(import.meta.url)); /** The slur filter's default list. */ export const SLURS_FILE = path.join(/* turbopackIgnore: true */ HERE, "slurs.json"); let SLUR_LIST = null; /** The default slur list (slurs.json), read once; a list that does not parse or is refused FAILS (a public render does not run without its filter). */ export function defaultSlurList() { if (SLUR_LIST) return SLUR_LIST; let doc; try { doc = JSON.parse(readFileSync(/* turbopackIgnore: true */ SLURS_FILE, "utf8")); } catch (e) { throw new Error(`${SLURS_FILE} does not read (${e instanceof Error ? e.message : e}) -- a public render does not run without its slur filter`); } const errors = slurListErrors(doc); if (errors.length) throw new Error(`${SLURS_FILE} is refused: ${errors.join("; ")}`); SLUR_LIST = doc; return doc; } /** The cut's slur filter: its `render.posts.slurs` over the default list. */ export const cutSlurs = (manifest, { uncensored = false } = {}) => resolveSlurs(manifest?.render?.posts?.slurs, defaultSlurList(), { uncensored }); export const CORPUS_REDACTIONS_FILE = "redactions.json"; export const POSTS_MARKUP_FILE = "posts-markup.json"; /** The corpus's terms file beside a channels dir. */ export const corpusRedactionsPath = (channelsDir) => path.join(path.dirname(path.resolve(channelsDir)), CORPUS_REDACTIONS_FILE); /** A channel's markup file. */ export const postsMarkupPath = (channelsDir, slug) => path.join(path.resolve(channelsDir), slug, POSTS_MARKUP_FILE); async function readJson(file) { let text; try { text = await readFile(file, "utf8"); } catch (e) { if (e?.code === "ENOENT" || e?.code === "ENOTDIR") return undefined; throw e; } try { return JSON.parse(text); } catch (e) { throw new Error(`${file} does not parse (${e instanceof Error ? e.message : e}) -- a public render does not run without its redaction`); } } /** * The redaction for `manifest`'s post items: `{ terms, markup }`. `channelsDir` * null reads only the project's list (no corpus at hand). * * @param {string|null} channelsDir * @param {Record} manifest * @param {{ uncensored?: boolean }} [o] the slur filter off (the redaction kept) */ export async function readPostRedaction(channelsDir, manifest, { uncensored = false } = {}) { const project = projectRedactOf(manifest?.render); const terms = []; /** @type {Record} */ const markup = {}; if (channelsDir) { const file = corpusRedactionsPath(channelsDir); const doc = await readJson(file); if (doc !== undefined) { const list = Array.isArray(doc) ? doc : doc?.terms; const errors = validateRedactTerms(list, CORPUS_REDACTIONS_FILE); if (errors.length) throw new Error(`${file} is refused: ${errors.join("; ")}`); terms.push(...normalizeTerms(list)); } const posts = (Array.isArray(manifest?.timeline) ? manifest.timeline : []).filter((e) => e?.type === "post" && typeof e.post === "string"); const slugs = [...new Set(posts.map((e) => e.post.split("/")[0]).filter((s) => /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(s)))]; for (const slug of slugs) { const mf = postsMarkupPath(channelsDir, slug); const doc2 = await readJson(mf); if (doc2 === undefined) continue; const errors = validatePostsMarkup(doc2, `${slug}/${POSTS_MARKUP_FILE}`); if (errors.length) throw new Error(`${mf} is refused: ${errors.join("; ")}`); for (const [id, m] of Object.entries(doc2)) markup[`${slug}/${id}`] = m; } } terms.push(...normalizeTerms(project)); return { terms, markup, slurs: cutSlurs(manifest, { uncensored }) }; }