encodeBookDocument function
({Map<String, ImageData> images, Map<String, dynamic> json})
encodeBookDocument(
- BookDocument document
Encodes document as JSON plus its image bytes.
Every ImageData in the document — inline blocks and the cover alike —
becomes a reference in the json carrying a stable id and its media type,
and the bytes arrive in the returned images map for the caller to store
as it likes. Ids are meaningful only within this one encoded document; do
not key long-lived storage on them across re-encodes.
Sentence spans are not encoded: they are re-segmented lazily on access.
Implementation
({Map<String, dynamic> json, Map<String, ImageData> images})
encodeBookDocument(BookDocument document) {
final images = <String, ImageData>{};
final metadata = document.metadata;
final cover = metadata.cover;
final json = <String, dynamic>{
'v': kBookDocumentSchemaVersion,
'metadata': {
'title': metadata.title,
'authors': metadata.authors,
'lang': metadata.sourceLanguageCode,
'cover': cover == null ? null : _imageRef(cover, images),
},
'chapters': [
for (final chapter in document.chapters)
{
'title': chapter.title,
'level': chapter.level,
'blocks': [
for (final block in chapter.blocks) _blockJson(block, images),
],
},
],
};
return (json: json, images: images);
}