+52
-1
@@ -19,6 +19,15 @@ function firstDirect(parent: Element, name: string): Element | undefined {
|
||||
return directChildrenByLocalName(parent, name)[0];
|
||||
}
|
||||
|
||||
function enumeration<T extends string>(
|
||||
value: string | null | undefined,
|
||||
allowed: readonly T[],
|
||||
fallback: T,
|
||||
): T {
|
||||
const normalized = value?.trim().toLowerCase() as T | undefined;
|
||||
return normalized && allowed.includes(normalized) ? normalized : fallback;
|
||||
}
|
||||
|
||||
export function parsePackageDocument(
|
||||
packageXml: string,
|
||||
packagePath: string,
|
||||
@@ -49,6 +58,12 @@ export function parsePackageDocument(
|
||||
.map((item) => text(item))
|
||||
.filter(Boolean),
|
||||
};
|
||||
const uniqueIdentifier = root.getAttribute("unique-identifier") ?? undefined;
|
||||
const identifierElements = elementsByLocalName(metadataElement, "identifier");
|
||||
const uniqueIdentifierValue =
|
||||
identifierElements
|
||||
.find((element) => element.getAttribute("id") === uniqueIdentifier)
|
||||
?.textContent?.trim() ?? metadata.identifier;
|
||||
const packageDirectory = directoryOf(packagePath);
|
||||
const manifest: ManifestItem[] = directChildrenByLocalName(
|
||||
manifestElement,
|
||||
@@ -93,16 +108,52 @@ export function parsePackageDocument(
|
||||
const ncx =
|
||||
(ncxId ? byId.get(ncxId) : undefined) ??
|
||||
manifest.find((item) => item.mediaType === "application/x-dtbncx+xml");
|
||||
const rendition = new Map(
|
||||
elementsByLocalName(metadataElement, "meta")
|
||||
.map(
|
||||
(element) =>
|
||||
[element.getAttribute("property") ?? "", text(element)] as const,
|
||||
)
|
||||
.filter(([property]) => property.startsWith("rendition:")),
|
||||
);
|
||||
return {
|
||||
package: {
|
||||
version: root.getAttribute("version") ?? "",
|
||||
uniqueIdentifier: root.getAttribute("unique-identifier") ?? undefined,
|
||||
uniqueIdentifier,
|
||||
uniqueIdentifierValue,
|
||||
packagePath,
|
||||
packageDirectory,
|
||||
metadata,
|
||||
manifest,
|
||||
spine,
|
||||
navigation: [],
|
||||
direction: enumeration(
|
||||
root.getAttribute("dir"),
|
||||
["ltr", "rtl", "auto"],
|
||||
"auto",
|
||||
),
|
||||
pageProgressionDirection: enumeration(
|
||||
spineElement.getAttribute("page-progression-direction"),
|
||||
["ltr", "rtl", "default"],
|
||||
"default",
|
||||
),
|
||||
rendition: {
|
||||
layout: enumeration(
|
||||
rendition.get("rendition:layout"),
|
||||
["reflowable", "pre-paginated"],
|
||||
"reflowable",
|
||||
),
|
||||
orientation: enumeration(
|
||||
rendition.get("rendition:orientation"),
|
||||
["auto", "landscape", "portrait"],
|
||||
"auto",
|
||||
),
|
||||
spread: enumeration(
|
||||
rendition.get("rendition:spread"),
|
||||
["auto", "none", "landscape", "portrait", "both"],
|
||||
"auto",
|
||||
),
|
||||
},
|
||||
cover,
|
||||
nav,
|
||||
ncx,
|
||||
|
||||
Reference in New Issue
Block a user