UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

165 lines 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Source = void 0; exports.posFromInput = posFromInput; exports.newPageSource = newPageSource; const log_1 = require("../../../../pkg/log"); const identity_1 = require("../vo/identity"); let pageIDCounter = 0; // Create a domain-specific logger for content operations const log = (0, log_1.getDomainLogger)('content', { component: 'pagesource' }); function posFromInput(filename, input, offset) { if (offset < 0) { return { filename: filename, lineNumber: 0, columnNumber: 0, offset: 0, }; } // Get the input up to the offset const inputSlice = input.slice(0, offset); // Count newlines to determine line number const newlineChar = 10; // '\n' character code let lineNumber = 1; for (let i = 0; i < inputSlice.length; i++) { if (inputSlice[i] === newlineChar) { lineNumber++; } } // Find the last newline to calculate column number let endOfLastLine = -1; for (let i = inputSlice.length - 1; i >= 0; i--) { if (inputSlice[i] === newlineChar) { endOfLastLine = i; break; } } return { filename: filename, lineNumber: lineNumber, columnNumber: offset - endOfLastLine, offset: offset, }; } class Source { constructor(file) { this.id = (++pageIDCounter).toString(); this.identity = new identity_1.Identity(pageIDCounter); this.file = file; } sourceKey() { return `${this.identity.lang}/${this.file.filename()}`.replace(/\\/g, '/'); } async contentSource() { return await this.readSourceAll(); } /** * Read source all - exact replica of Go's readSourceAll method */ async readSourceAll() { try { const file = await this.file.open(); const fileInfo = await file.stat(); const buffer = new Uint8Array(fileInfo.size()); const result = await file.read(buffer); await file.close(); return result.buffer; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new Error(`Failed to read file content: ${message}`); } } /** * Position offset - exact replica of Go's PosOffset method */ async posOffset(offset) { try { const source = await this.contentSource(); return posFromInput(this.file.filename(), source, offset); } catch (error) { throw new Error(`failed to read content source for "${this.file.filename()}": ${error}`); } } /** * Get identity - for PageIdentity interface */ pageIdentity() { return this.identity; } /** * Get file - for FileProvider interface */ pageFile() { return this.file; } /** * Get stale versions - for Staler interface */ staleVersions() { // Return empty array as default implementation // In a real implementation, this would track version numbers return []; } /** * Get section - for PageSource interface */ section() { return this.file.section(); } /** * Get paths - for PageSource interface */ paths() { return this.file.paths(); } /** * Get path - for PageSource interface */ path() { return this.file.paths().path(); } /** * Get opener - for PageSource interface */ opener() { return () => this.file.open(); } /** * Open reader - simple implementation */ openReader() { // Simple mock reader implementation return { read: async () => { // Return empty buffer to indicate end of stream return new Uint8Array(0); } }; } /** * Content - simple implementation */ async content() { try { log.error(`Reading content not implemented from: ${this.file.filename()}`); return new Uint8Array(0); } catch (error) { const errorMsg = `failed to read content source for "${this.file.filename()}": ${error}`; log.error(errorMsg); throw new Error(errorMsg); } } } exports.Source = Source; /** * Factory function to create new page source * Exact replica of Go's newPageSource function */ function newPageSource(file) { return new Source(file); } //# sourceMappingURL=pagesource.js.map