import { Part as PartType } from "../types.d.ts"; import { newSession } from "../utils/http.ts"; export default class Chapter { #session: ReturnType; id: string; workID: string; /** * internal part JSON; includes more detailed info */ partJSON: PartType; /** * name of the part */ name = ""; /** * exact html of the part */ html = ""; /** * the html of the part converted to markdown */ text = ""; constructor( workId: string, part: PartType, session: ReturnType, ) { this.#session = session; this.workID = workId; this.partJSON = part; this.id = part.id.toString(); this.name = part.title; } async init() { const html = await (await this.#session.get("/apiv2/storytext", false, { params: new URLSearchParams({ id: this.id, }), })).text(); this.html = html; // pseudo-parse html to markdown with regex this.text = html .replaceAll( //g, "", ) //strip starting paragraph tags (they're useless) .replaceAll(/<\/?(em|i)>/g, "*") // i/emphasis tags -> md italics .replaceAll(/<\/?strong>/g, "**") // strong tag -> md bold .replaceAll(/<\/?u>/g, "__") // u tag -> discord md underline (the normal spec doesn't support underlines) .replaceAll(/<\/p>/g, "\n") // make ending p tags into newlines, to be consistent with wattpad rendering .replaceAll(/
/g, "\n") // make line break tags into real newlines .trim(); } }