/
manifest.ts
110 lines (95 loc) · 2.7 KB
/
manifest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Manifest shared module
* @module
*
* The manifest is what enables user agents to understand the bounds of digital
* publication and the connection between its resources. It includes metadata that
* describes the digital publication, as a publication has an identity and nature beyond
* its constituent resources (https://www.w3.org/TR/pub-manifest/).
*/
import { previewSchema } from '../src/config';
import { z } from 'zod';
export type Identifier = string;
const revisionSchema = z.string().min(1).max(7);
export type Revision = z.infer<typeof revisionSchema>;
const orderSchema = z.number().nullable();
export type Order = z.infer<typeof orderSchema>;
export type OrderLike = string;
export type HeadingLevel = number;
const rootSchema = z.string();
export type Root = z.infer<typeof rootSchema>;
/** i18n ISO string */
export type LanguageCode = string;
export interface Heading {
index: number;
level: HeadingLevel;
name: string | null;
id: string | null;
children: Heading[];
}
const Heading: z.ZodType<Heading> = z.lazy(() =>
z.object({
index: z.number(),
level: z.number().int().min(1).max(6),
name: z.string().nullable(),
id: z.string().nullable(),
children: z.array(Heading),
})
);
export enum DocRole {
Break = 'break',
Chapter = 'chapter',
Cover = 'cover',
About = 'about',
Other = 'other',
}
const publicationSumSchema = z.object({
all: z.object({
words: z.number(),
chars: z.number(),
ideas: z.number(),
}),
chapters: z.object({
words: z.number(),
chars: z.number(),
ideas: z.number(),
}),
});
export type PublicationSum = z.infer<typeof publicationSumSchema>;
const documentMetadataSchema = z.object({
title: z.string(),
file: z.string(),
words: z.number(),
chars: z.number(),
ideas: z.number(),
role: z.nativeEnum(DocRole),
order: orderSchema,
prev: z.string().nullable(),
next: z.string().nullable(),
toc: Heading.array(),
});
export type DocumentMetadata = z.infer<typeof documentMetadataSchema>;
// https://github.com/orgs/next-book/teams/nb-core/discussions/1
export const manifestSchema = z.object({
title: z.string(),
author: z.string(),
identifier: z.string(),
languageCode: z.string(),
revision: revisionSchema,
generatedAt: z.object({
date: z.string(),
unix: z.number(),
}),
readingOrder: z.string().array(),
documents: documentMetadataSchema.array(),
totals: publicationSumSchema,
subtitle: z.string().optional(),
published: z.number().optional(),
keywords: z.string().array().optional(),
publisher: z.string().optional(),
edition: z.string().optional(),
root: rootSchema,
preview: previewSchema,
});
type Manifest = z.infer<typeof manifestSchema>;
export default Manifest;