@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
129 lines • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ref = void 0;
const log_1 = require("../../../../pkg/log");
// Create domain-specific logger for ref operations
const log = (0, log_1.getDomainLogger)('site', { component: 'ref' });
/**
* Ref - TypeScript equivalent of Go's Ref entity
* Handles page references and link generation
*/
class Ref {
constructor(site, contentSvc, notFoundURL = '#ZgotmplZ') {
this.site = site;
this.contentSvc = contentSvc;
this.notFoundURL = notFoundURL;
}
/**
* Generate relative reference from arguments
* TypeScript equivalent of RelRefFrom method from Go
*/
async relRefFrom(argsm, source) {
return this.relRef(argsm, source);
}
/**
* Generate relative reference
* TypeScript equivalent of relRef method from Go
*/
async relRef(argsm, source) {
try {
const args = this.decodeRefArgs(argsm);
if (!args.path) {
return '';
}
// TODO: not support search by output format yet
return this.refLink(args.path, source, true, args.outputFormat);
}
catch (error) {
throw new Error(`Invalid arguments to Ref: ${error}`);
}
}
/**
* Decode reference arguments
* TypeScript equivalent of decodeRefArgs method from Go
*/
decodeRefArgs(args) {
return {
path: args.path || '',
outputFormat: args.outputFormat || ''
};
}
/**
* Generate reference link
* TypeScript equivalent of refLink method from Go
*/
async refLink(ref, source, relative, outputFormat) {
const pw = source;
if (!pw || typeof pw.unwrapPage !== 'function') {
throw new Error('source is not a PageWrapper');
}
const page = pw.unwrapPage();
// Parse URL
let refURL;
const normalizedRef = ref.replace(/\\/g, '/');
try {
refURL = new globalThis.URL(normalizedRef, 'http://example.com');
}
catch (error) {
log.error(`Failed to parse ref URL: ${error}`);
return this.notFoundURL;
}
let target = null;
let link = '';
if (refURL.pathname !== '') {
try {
target = await this.contentSvc.getPageRef(page, refURL.pathname, this.site.home.page);
let position = null;
if (!target) {
if (this.isPositioner(source)) {
position = source.position();
}
}
if (!target) {
this.logNotFound(refURL.pathname, 'page not found', page, position);
return this.notFoundURL;
}
const sitePage = await this.site.sitePage(target);
if (relative) {
link = sitePage.relPermalink();
}
else {
link = sitePage.permalink();
}
}
catch (error) {
log.error(`[${page.pageIdentity().pageLanguage()}] REF_NOT_FOUND: Ref "${ref}": ${error}`);
return this.notFoundURL;
}
}
if (refURL.hash) {
link = link + '#' + refURL.hash;
// TODO: support with more detail from pageContext
}
return link;
}
/**
* Check if source is a Positioner
*/
isPositioner(source) {
return source && typeof source.position === 'function';
}
/**
* Log not found error
* TypeScript equivalent of logNotFound method from Go
*/
logNotFound(ref, what, page, position) {
const lang = page.pageIdentity().pageLanguage();
if (position && position.isValid()) {
log.error(`[${lang}] REF_NOT_FOUND: Ref "${ref}": ${position.toString()}: ${what}`);
}
else if (!page) {
log.error(`[${lang}] REF_NOT_FOUND: Ref "${ref}": ${what}`);
}
else {
log.error(`[${lang}] REF_NOT_FOUND: Ref "${ref}" from page "${page.path()}": ${what}`);
}
}
}
exports.Ref = Ref;
//# sourceMappingURL=ref.js.map