@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
112 lines • 3.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = exports.RenderContextDataHolder = exports.BufWriter = void 0;
/**
* BufWriter implements FlexiWriter using a buffer
*/
class BufWriter {
constructor(initialCapacity = 1024) {
this.capacity = initialCapacity;
this.buffer = new Uint8Array(this.capacity);
this.position = 0;
}
async write(data) {
this.ensureCapacity(data.length);
this.buffer.set(data, this.position);
this.position += data.length;
}
async writeString(str) {
const encoder = new TextEncoder();
const data = encoder.encode(str);
await this.write(data);
}
async writeByte(b) {
this.ensureCapacity(1);
this.buffer[this.position] = b;
this.position++;
}
bytes() {
return this.buffer.slice(0, this.position);
}
length() {
return this.position;
}
truncate(size) {
if (size < 0 || size > this.position) {
throw new Error('Invalid truncate size');
}
this.position = size;
}
ensureCapacity(additional) {
const needed = this.position + additional;
if (needed <= this.capacity) {
return;
}
// Double the capacity or use needed size, whichever is larger
const newCapacity = Math.max(this.capacity * 2, needed);
const newBuffer = new Uint8Array(newCapacity);
newBuffer.set(this.buffer);
this.buffer = newBuffer;
this.capacity = newCapacity;
}
}
exports.BufWriter = BufWriter;
/**
* RenderContextDataHolder holds render and document context
*/
class RenderContextDataHolder {
constructor(rctx, dctx) {
this.rctx = rctx;
this.dctx = dctx;
}
renderContext() {
return this.rctx;
}
documentContext() {
return this.dctx;
}
}
exports.RenderContextDataHolder = RenderContextDataHolder;
/**
* Context combines BufWriter and ContextData
*/
class Context {
constructor(bufWriter, contextData) {
this.bufWriter = bufWriter;
this.contextData = contextData;
this.posStack = [];
}
// FlexiWriter methods
async write(data) {
return this.bufWriter.write(data);
}
async writeString(str) {
return this.bufWriter.writeString(str);
}
async writeByte(b) {
return this.bufWriter.writeByte(b);
}
// ContextData methods
renderContext() {
return this.contextData.renderContext();
}
documentContext() {
return this.contextData.documentContext();
}
// Buffer management
get buffer() {
return this.bufWriter;
}
pushPos(pos) {
this.posStack.push(pos);
}
popPos() {
const pos = this.posStack.pop();
if (pos === undefined) {
throw new Error('Position stack is empty');
}
return pos;
}
}
exports.Context = Context;
//# sourceMappingURL=context.js.map