mirror of
https://github.com/laurent22/joplin.git
synced 2026-05-07 20:02:45 +00:00
36 lines
1006 B
TypeScript
36 lines
1006 B
TypeScript
import * as fs from 'fs-extra';
|
|
const Datauri = require('datauri/sync');
|
|
import { dirname } from 'path';
|
|
import packToWriter, { type FileApiChunkCallback } from './packToWriter';
|
|
|
|
const dataUriEncode = (filePath: string): string => {
|
|
const result = Datauri(filePath);
|
|
return result.content;
|
|
};
|
|
|
|
export default async function htmlpack(inputFile: string, outputFile: string): Promise<void> {
|
|
const inputHtml = await fs.readFile(inputFile, 'utf8');
|
|
const baseDir = dirname(inputFile);
|
|
|
|
const chunks: string[] = [];
|
|
await packToWriter(baseDir, inputHtml, {
|
|
exists(path: string) {
|
|
return fs.exists(path);
|
|
},
|
|
readFileText(path: string) {
|
|
return fs.readFile(path, 'utf8');
|
|
},
|
|
async readFileDataUri(path: string) {
|
|
return dataUriEncode(path);
|
|
},
|
|
async streamFileDataUri(path: string, onChunk: FileApiChunkCallback) {
|
|
await onChunk(dataUriEncode(path));
|
|
},
|
|
writeChunk(chunk: string) {
|
|
chunks.push(chunk);
|
|
},
|
|
});
|
|
|
|
await fs.writeFile(outputFile, chunks.join(''), 'utf8');
|
|
}
|