@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
132 lines • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SourceParseInfo = void 0;
exports.createSourceParseInfo = createSourceParseInfo;
const pageparser_1 = require("./pageparser");
const item_1 = require("../../../../pkg/md/parser/item");
const pagelexer_1 = require("../../../../pkg/md/parser/pagelexer");
const log_1 = require("../../../../pkg/log");
// Create a domain-specific logger for markdown operations
const log = (0, log_1.getDomainLogger)('markdown', { component: 'parseinfo' });
/**
* Source parse info - contains parsing state and handlers
*/
class SourceParseInfo {
constructor(source, handlers) {
this.posMainContent = -1;
this.itemsStep1 = [];
this.source = source;
this.handlers = handlers;
this.validateHandlers();
}
/**
* Validate that all required handlers are provided
*/
validateHandlers() {
if (!this.handlers.frontMatterHandler()) {
throw new Error('no front matter handler');
}
if (!this.handlers.summaryHandler()) {
throw new Error('no summary handler');
}
if (!this.handlers.shortcodeHandler()) {
throw new Error('no shortcode handler');
}
if (!this.handlers.bytesHandler()) {
throw new Error('no bytes handler');
}
}
/**
* Check if parsing result is empty
*/
isEmpty() {
return this.itemsStep1.length === 0;
}
/**
* Parse the source into items
*/
async parse() {
const items = (0, pageparser_1.parseBytes)(this.source, {});
this.itemsStep1 = items;
}
/**
* Handle the parsed items using the provided handlers
*/
async handle() {
if (this.isEmpty()) {
return;
}
const iter = new pagelexer_1.Iterator(this.itemsStep1);
while (true) {
const it = iter.Next();
try {
if (it.Type === item_1.ItemType.TypeIgnore) {
log.info(`Ignoring item at position ${it.Pos()}: ${it.ValStr(this.source)}`);
}
else if (it.IsFrontMatter()) {
await this.handlers.frontMatterHandler()(it);
const next = iter.Peek();
if (!next.IsDone()) {
this.posMainContent = next.Pos();
}
}
else if (it.Type === item_1.ItemType.TypeLeadSummaryDivider) {
await this.handlers.summaryHandler()(it, iter);
}
else if (it.IsLeftShortcodeDelim()) {
iter.Backup();
const currentItem = iter.Current();
await this.handlers.shortcodeHandler()(currentItem, iter);
}
else if (it.IsEOF()) {
break;
}
else if (it.IsError()) {
log.error(`Error parsing item at position ${it.Pos()}: ${it.Err}`);
throw this.createError(it.Err || new Error('Unknown parsing error'), it);
}
else {
await this.handlers.bytesHandler()(it);
}
}
catch (error) {
throw this.createError(error, it);
}
}
}
/**
* Get the main content position
*/
getMainContentPosition() {
return this.posMainContent;
}
/**
* Get the parsed items
*/
getItems() {
return this.itemsStep1;
}
/**
* Get the source
*/
getSource() {
return this.source;
}
/**
* Create error with position information
*/
createError(err, item) {
const pos = (0, pageparser_1.posFromInput)('', this.source, item.Pos());
const positionedError = new Error(`${err.message} at line ${pos.line}, column ${pos.column}`);
positionedError.position = pos;
return positionedError;
}
}
exports.SourceParseInfo = SourceParseInfo;
/**
* Factory function to create a new SourceParseInfo
*/
function createSourceParseInfo(source, handlers) {
return new SourceParseInfo(source, handlers);
}
//# sourceMappingURL=parseinfo.js.map