@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
181 lines • 6.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemType = exports.Item = void 0;
exports.itemTypeToString = itemTypeToString;
class Item {
constructor() {
this.Type = ItemType.tError;
this.Err = null;
this.low = 0;
this.high = 0;
this.segments = [];
this.firstByte = 0;
this.isString = false;
this.content = '';
this.bytes = [];
}
Pos() {
if (this.segments.length > 0) {
return this.segments[0].Low;
}
return this.low;
}
Val(source) {
if (this.segments.length === 0) {
return source.slice(this.low, this.high);
}
if (this.segments.length === 1) {
return source.slice(this.segments[0].Low, this.segments[0].High);
}
const chunks = [];
for (const s of this.segments) {
chunks.push(source.slice(s.Low, s.High));
}
// Concatenate all chunks
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
result.set(chunk, offset);
offset += chunk.length;
}
return result;
}
ValStr(source) {
return new TextDecoder().decode(this.Val(source));
}
ValTyped(source) {
const str = this.ValStr(source);
if (this.isString) {
// A quoted value that is a string even if it looks like a number etc.
return str;
}
if (boolRe.test(str)) {
return str === "true";
}
if (intRe.test(str)) {
const num = parseInt(str, 10);
if (!isNaN(num)) {
return num;
}
return str;
}
if (floatRe.test(str)) {
const num = parseFloat(str);
if (!isNaN(num)) {
return num;
}
return str;
}
return str;
}
IsText() {
return this.Type === ItemType.tText || this.Type === ItemType.tIndentation;
}
IsIndentation() {
return this.Type === ItemType.tIndentation;
}
IsNonWhitespace(source) {
return this.ValStr(source).trim().length > 0;
}
IsShortcodeName() {
return this.Type === ItemType.tScName;
}
IsInlineShortcodeName() {
return this.Type === ItemType.tScNameInline;
}
IsLeftShortcodeDelim() {
return this.Type === ItemType.tLeftDelimScWithMarkup || this.Type === ItemType.tLeftDelimScNoMarkup;
}
IsRightShortcodeDelim() {
return this.Type === ItemType.tRightDelimScWithMarkup || this.Type === ItemType.tRightDelimScNoMarkup;
}
IsShortcodeClose() {
return this.Type === ItemType.tScClose;
}
IsShortcodeParam() {
return this.Type === ItemType.tScParam;
}
IsShortcodeParamVal() {
return this.Type === ItemType.tScParamVal;
}
IsShortcodeMarkupDelimiter() {
return this.Type === ItemType.tLeftDelimScWithMarkup || this.Type === ItemType.tRightDelimScWithMarkup;
}
IsFrontMatter() {
return this.Type >= ItemType.TypeFrontMatterYAML && this.Type <= ItemType.TypeFrontMatterORG;
}
IsDone() {
return this.Type === ItemType.tError || this.Type === ItemType.tEOF;
}
IsEOF() {
return this.Type === ItemType.tEOF;
}
IsError() {
return this.Type === ItemType.tError;
}
ToString(source) {
const val = this.Val(source);
const valStr = this.ValStr(source);
const typeStr = itemTypeToString(this.Type);
switch (true) {
case this.Type === ItemType.tEOF:
return "EOF";
case this.Type === ItemType.tError:
return valStr;
case this.Type === ItemType.tIndentation:
return `${typeStr}:[${visualizeSpaces(valStr)}]`;
case this.Type > ItemType.tKeywordMarker:
return `<${valStr}>`;
case val.length > 50:
return `${typeStr}:${valStr.substring(0, 20)}...`;
default:
return `${typeStr}:[${valStr}]`;
}
}
}
exports.Item = Item;
var ItemType;
(function (ItemType) {
ItemType[ItemType["tError"] = 0] = "tError";
ItemType[ItemType["tEOF"] = 1] = "tEOF";
// page items
ItemType[ItemType["TypeLeadSummaryDivider"] = 2] = "TypeLeadSummaryDivider";
ItemType[ItemType["TypeFrontMatterYAML"] = 3] = "TypeFrontMatterYAML";
ItemType[ItemType["TypeFrontMatterTOML"] = 4] = "TypeFrontMatterTOML";
ItemType[ItemType["TypeFrontMatterJSON"] = 5] = "TypeFrontMatterJSON";
ItemType[ItemType["TypeFrontMatterORG"] = 6] = "TypeFrontMatterORG";
ItemType[ItemType["TypeIgnore"] = 7] = "TypeIgnore";
// shortcode items
ItemType[ItemType["tLeftDelimScNoMarkup"] = 8] = "tLeftDelimScNoMarkup";
ItemType[ItemType["tRightDelimScNoMarkup"] = 9] = "tRightDelimScNoMarkup";
ItemType[ItemType["tLeftDelimScWithMarkup"] = 10] = "tLeftDelimScWithMarkup";
ItemType[ItemType["tRightDelimScWithMarkup"] = 11] = "tRightDelimScWithMarkup";
ItemType[ItemType["tScClose"] = 12] = "tScClose";
ItemType[ItemType["tScName"] = 13] = "tScName";
ItemType[ItemType["tScNameInline"] = 14] = "tScNameInline";
ItemType[ItemType["tScParam"] = 15] = "tScParam";
ItemType[ItemType["tScParamVal"] = 16] = "tScParamVal";
ItemType[ItemType["tIndentation"] = 17] = "tIndentation";
ItemType[ItemType["tText"] = 18] = "tText";
// preserved for later - keywords come after this
ItemType[ItemType["tKeywordMarker"] = 19] = "tKeywordMarker";
})(ItemType || (exports.ItemType = ItemType = {}));
const boolRe = /^(true|false)$/;
const intRe = /^[-+]?\d+$/;
const floatRe = /^[-+]?\d*\.\d+$/;
// Helper function to visualize spaces (similar to goldmark's util.VisualizeSpaces)
function visualizeSpaces(str) {
return str.replace(/ /g, '␣').replace(/\t/g, '→');
}
// ItemType string conversion
const _ItemType_name = "tErrortEOFTypeLeadSummaryDividerTypeFrontMatterYAMLTypeFrontMatterTOMLTypeFrontMatterJSONTypeFrontMatterORGTypeIgnoretLeftDelimScNoMarkuptRightDelimScNoMarkuptLeftDelimScWithMarkuptRightDelimScWithMarkuptScClosetScNametScNameInlinetScParamtScParamValtIndentationtTexttKeywordMarker";
const _ItemType_index = [0, 6, 10, 32, 51, 70, 89, 107, 117, 137, 158, 180, 203, 211, 218, 231, 239, 250, 262, 267, 281];
function itemTypeToString(itemType) {
const i = itemType;
if (i < 0 || i >= _ItemType_index.length - 1) {
return `ItemType(${i})`;
}
return _ItemType_name.slice(_ItemType_index[i], _ItemType_index[i + 1]);
}
//# sourceMappingURL=item.js.map