@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
159 lines • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalHighlighter = exports.DefaultHighlighter = exports.HighlightResultImpl = void 0;
exports.createDefaultHighlighter = createDefaultHighlighter;
exports.createExternalHighlighter = createExternalHighlighter;
/**
* HighlightResultImpl implements HighlightResult
*/
class HighlightResultImpl {
constructor(wrappedContent, innerContent) {
this.wrappedContent = wrappedContent;
this.innerContent = innerContent;
}
wrapped() {
return this.wrappedContent;
}
inner() {
return this.innerContent;
}
}
exports.HighlightResultImpl = HighlightResultImpl;
/**
* Default highlighter using highlight.js or prism.js
*/
class DefaultHighlighter {
constructor(config) {
this.config = config;
}
async highlight(code, lang, opts) {
// Default implementation - could be extended to use highlight.js or prism.js
if (!lang || lang === 'text' || lang === 'plain') {
return this.escapeHTML(code);
}
// For now, return escaped HTML with basic highlighting
// In a real implementation, you would use a syntax highlighter library
const escapedCode = this.escapeHTML(code);
const className = `language-${lang}`;
if (this.config.lineNos) {
return this.addLineNumbers(escapedCode, className);
}
return `<code class="${className}">${escapedCode}</code>`;
}
async highlightCodeBlock(ctx, opts) {
const lang = ctx.type();
const code = ctx.inner();
const highlighted = await this.highlight(code, lang, opts);
const wrapped = `<pre>${highlighted}</pre>`;
return new HighlightResultImpl(wrapped, highlighted);
}
async renderCodeblock(cctx, w, ctx) {
const result = await this.highlightCodeBlock(ctx);
await w.writeString(result.wrapped());
}
isDefaultCodeBlockRenderer() {
return true;
}
escapeHTML(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
addLineNumbers(code, className) {
const lines = code.split('\n');
const numberedLines = lines.map((line, index) => {
const lineNumber = index + this.config.lineNoStart;
if (this.config.lineNumbersInTable) {
return `<tr><td class="line-number">${lineNumber}</td><td class="line-code"><code class="${className}">${line}</code></td></tr>`;
}
else {
return `<span class="line-number">${lineNumber}</span><code class="${className}">${line}</code>`;
}
});
if (this.config.lineNumbersInTable) {
return `<table class="code-table"><tbody>${numberedLines.join('')}</tbody></table>`;
}
else {
return numberedLines.join('\n');
}
}
}
exports.DefaultHighlighter = DefaultHighlighter;
/**
* External highlighter adapter - allows using external highlight libraries
*/
class ExternalHighlighter {
constructor(highlightFn, config = {
style: 'github',
lineNos: false,
lineNoStart: 1,
anchorLineNos: false,
lineAnchors: '',
lineNumbersInTable: true,
noClasses: true,
codeFences: true,
guessSyntax: false,
tabWidth: 4,
}) {
this.highlightFn = highlightFn;
this.config = config;
}
async highlight(code, lang, opts) {
return this.highlightFn(code, lang);
}
async highlightCodeBlock(ctx, opts) {
const lang = ctx.type();
const code = ctx.inner();
const highlighted = await this.highlight(code, lang, opts);
const wrapped = `<pre>${highlighted}</pre>`;
return new HighlightResultImpl(wrapped, highlighted);
}
async renderCodeblock(cctx, w, ctx) {
const result = await this.highlightCodeBlock(ctx);
await w.writeString(result.wrapped());
}
isDefaultCodeBlockRenderer() {
return false;
}
}
exports.ExternalHighlighter = ExternalHighlighter;
/**
* Factory function to create default highlighter
*/
function createDefaultHighlighter(config) {
const defaultConfig = {
style: 'github',
lineNos: false,
lineNoStart: 1,
anchorLineNos: false,
lineAnchors: '',
lineNumbersInTable: true,
noClasses: true,
codeFences: true,
guessSyntax: false,
tabWidth: 4,
};
return new DefaultHighlighter({ ...defaultConfig, ...config });
}
/**
* Factory function to create external highlighter
*/
function createExternalHighlighter(highlightFn, config) {
const defaultConfig = {
style: 'github',
lineNos: false,
lineNoStart: 1,
anchorLineNos: false,
lineAnchors: '',
lineNumbersInTable: true,
noClasses: true,
codeFences: true,
guessSyntax: false,
tabWidth: 4,
};
return new ExternalHighlighter(highlightFn, { ...defaultConfig, ...config });
}
//# sourceMappingURL=highlight.js.map