Myriad-Dreamin/typst.ts

Data upload via external file like the json,csv?

Closed this issue · 1 comments

Hi again,

Greetings.

I would like to know if it is possible to upload the data via an external file like json, csv etc as it is supported by the typst? If it does not already but there is a way to achieve this, please let me know.

Thanks for such an amazing library and your support, friend.

Reference(Data Loading)

Hi, you could add files to your access model, which can then get read by typst compiler. The access model refers to

export function withAccessModel(accessModel: FsAccessModel): BeforeBuildFn {

You can also use the {map,unmap,reset}Shadow function to manipulate any text or binary file data for typst compiler. They will shadow the file access from provided access model directly in memory.

/**
* Add a source file to the compiler.
* @param {string} path - The path of the source file.
* @param {string} source - The source code of the source file.
*
*/
addSource(path: string, source: string): void;
/**
* Add a shadow file to the compiler.
* @param {string} path - The path to the shadow file.
* @param {Uint8Array} content - The content of the shadow file.
*
*/
mapShadow(path: string, content: Uint8Array): void;
/**
* Remove a shadow file from the compiler.
* @param {string} path - The path to the shadow file.
*/
unmapShadow(path: string): void;
/**
* Reset the shadow files.
* Note: this function is independent to the {@link reset} function.
*/
resetShadow(): void;

The mapShadow(path: string, content: Uint8Array): void; resembles addSource(path: string, source: string): void;, but retrieves some binary data without guessing the underlying encoding.

Example usage:

const encoder = new TextEncoder();
// add a json file (utf8)
compiler.mapShadow('/assets/data.json', encoder.encode(jsonData));
// remove a json file
compiler.unmapShadow('/assets/data.json');
// clean up all shadow files (Note: this function will also clean all files added by `addSource`)
compiler.resetShadow();

// add an image file
const pngData = await fetch(...).arrayBuffer();
compiler.mapShadow('/assets/tiger.png', new Uint8Array(pngData));