You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
60 lines
1.8 KiB
import { Part as PartType } from "../types.d.ts";
|
|
import { newSession } from "../utils/http.ts";
|
|
|
|
export default class Chapter {
|
|
#session: ReturnType<typeof newSession>;
|
|
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<typeof newSession>,
|
|
) {
|
|
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(
|
|
/<p( data-p-id=".{32}")?( style="(text-align):(left|right|center);")?>/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(/<br>/g, "\n") // make line break tags into real newlines
|
|
.trim();
|
|
}
|
|
}
|