Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/fence-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { SnippetDirective } from './types.js';
import {
findSnippetDirectiveToken,
formatSnippetDirective,
} from './snippet-directive.js';

const LANGUAGE = /^[ \t]*\S*/;
const WHITESPACE = /[ \t]/;

function splitLanguage(info: string): [language: string, meta: string] {
const language = LANGUAGE.exec(info)?.[0] ?? '';
return [language, info.slice(language.length)];
}

function withMetaDirective(
meta: string,
directive: SnippetDirective | null,
): string {
const token = findSnippetDirectiveToken(meta);

if (!token) {
return directive ? `${meta} ${formatSnippetDirective(directive)}` : meta;
}

if (directive) {
return (
meta.slice(0, token.start) +
formatSnippetDirective(directive) +
meta.slice(token.end)
);
}

const separated = WHITESPACE.test(meta.charAt(token.start - 1));
return (
meta.slice(0, token.start - (separated ? 1 : 0)) + meta.slice(token.end)
);
}

export function withDirective(
info: string,
directive: SnippetDirective | null,
): string {
const [language, meta] = splitLanguage(info);
return language + withMetaDirective(meta, directive);
}
183 changes: 183 additions & 0 deletions src/fenced-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import type { SnippetDirective } from './types.js';
import { withDirective } from './fence-info.js';
import { getLineEnding, splitLines, type LineEnding } from './line-endings.js';

export interface BlockEdit {
content?: string | undefined;
directive?: SnippetDirective | null | undefined;
}

export type BlockEnd =
| { readonly kind: 'closing-fence'; readonly line: string }
| { readonly kind: 'line-break' }
| { readonly kind: 'unterminated' };

export interface FencedBlock {
readonly fence: string;
readonly info: string;
readonly rawBody: string;
readonly end: BlockEnd;
readonly bodyPrefix: string;
readonly lineEnding: LineEnding;
}

const TAB_SIZE = 4;
const BLOCKQUOTE_MARKER = '>';
const OPENING_LINE = /^(`{3,}|~{3,})(.*)$/;
const BODY_FENCE_LINE = /^[ \t]*(`{3,}|~{3,})[ \t]*$/;
const FENCE_RUN = /`{3,}|~{3,}/;
const STARTS_WITH_WHITESPACE = /^[ \t]/;
const TRAILING_LINE_BREAKS = /(?:\r\n|\r|\n)+$/;

function columnAfter(text: string, startColumn: number): number {
return [...text].reduce(
(column, character) =>
character === '\t' ? column + TAB_SIZE - (column % TAB_SIZE) : column + 1,
startColumn,
);
}

function bodyPrefixFor(linePrefix: string): string {
const [outsideQuotes = '', ...quoted] = linePrefix.split(BLOCKQUOTE_MARKER);
const initial = columnAfter(outsideQuotes, 0);

return quoted.reduce(
({ column, prefix }, segment) => {
const start = column + BLOCKQUOTE_MARKER.length;
const end = columnAfter(segment, start);
const optionalSpace = STARTS_WITH_WHITESPACE.test(segment) ? 1 : 0;
const indent = ' '.repeat(end - start - optionalSpace);
return { column: end, prefix: `${prefix}${BLOCKQUOTE_MARKER} ${indent}` };
},
{ column: initial, prefix: ' '.repeat(initial) },
).prefix;
}

function closingFencePattern(fence: string, quoteDepth: number): RegExp {
const blockquoteMarkers = `(?:[ \\t]*${BLOCKQUOTE_MARKER}){${quoteDepth}}`;
const fenceRun = `${fence.charAt(0)}{${fence.length},}`;
return new RegExp(`^${blockquoteMarkers}[ \\t]*${fenceRun}[ \\t]*$`);
}

function blockEnd(
lines: ReadonlyArray<string>,
fence: string,
quoteDepth: number,
): BlockEnd {
const last = lines.length > 1 ? lines.at(-1) : undefined;
if (last === '') {
return { kind: 'line-break' };
}
if (last !== undefined && closingFencePattern(fence, quoteDepth).test(last)) {
return { kind: 'closing-fence', line: last };
}
return { kind: 'unterminated' };
}

function endText(end: BlockEnd): string {
return end.kind === 'closing-fence' ? end.line : '';
}

function linePrefixAt(source: string, offset: number): string {
const lineStart =
Math.max(
source.lastIndexOf('\n', offset - 1),
source.lastIndexOf('\r', offset - 1),
) + 1;
return source.slice(lineStart, offset);
}

export function parseFencedBlockAt(
source: string,
start: number,
end: number,
): FencedBlock {
const text = source.slice(start, end);
const lines = splitLines(text);
const [opening = ''] = lines;
const match = OPENING_LINE.exec(opening);
if (!match) {
throw new Error(`Not the start of a fenced code block: ${opening}`);
}

const [, fence = '', info = ''] = match;
const linePrefix = linePrefixAt(source, start);
const quoteDepth = linePrefix.split(BLOCKQUOTE_MARKER).length - 1;
const blockEnding = blockEnd(lines, fence, quoteDepth);

return {
fence,
info,
rawBody: text.slice(
opening.length,
text.length - endText(blockEnding).length,
),
end: blockEnding,
bodyPrefix: bodyPrefixFor(linePrefix),
lineEnding: getLineEnding(text),
};
}

function longestFenceRun(content: string, fenceChar: string): number {
return splitLines(content).reduce((longest, line) => {
const run = BODY_FENCE_LINE.exec(line)?.[1];
return run?.charAt(0) === fenceChar
? Math.max(longest, run.length)
: longest;
}, 0);
}

function fenceFor(content: string, fence: string): string {
const fenceChar = fence.charAt(0);
const longest = longestFenceRun(content, fenceChar);
return longest >= fence.length ? fenceChar.repeat(longest + 1) : fence;
}

function lengthenFence(line: string, fence: string): string {
return line.replace(FENCE_RUN, (run) =>
run.length >= fence.length ? run : fence,
);
}

function bodyLines(content: string, prefix: string): Array<string> {
if (content === '') {
return [];
}
const blankLinePrefix = prefix.trimEnd();
return splitLines(content).map((line) =>
line === '' ? blankLinePrefix : prefix + line,
);
}

function bodyFor(content: string, end: BlockEnd): string {
return end.kind === 'closing-fence'
? content
: content.replace(TRAILING_LINE_BREAKS, '');
}

function endLines(end: BlockEnd, fence: string): Array<string> {
if (end.kind === 'closing-fence') {
return [lengthenFence(end.line, fence)];
}
return end.kind === 'line-break' ? [''] : [];
}

export function renderFencedBlock(block: FencedBlock, edit: BlockEdit): string {
const info =
edit.directive === undefined
? block.info
: withDirective(block.info, edit.directive);

if (edit.content === undefined) {
return block.fence + info + block.rawBody + endText(block.end);
}

const content = bodyFor(edit.content, block.end);
const fence = fenceFor(content, block.fence);

return [
fence + info,
...bodyLines(content, block.bodyPrefix),
...endLines(block.end, fence),
].join(block.lineEnding);
}
8 changes: 7 additions & 1 deletion src/line-endings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export type LineEnding = '\n' | '\r\n';

const LINE_BREAK = /\r\n|\r|\n/;

export function getLineEnding(content: string): LineEnding {
return content.match(/\r\n|\n/)?.[0] === '\r\n' ? '\r\n' : '\n';
}

export function splitLines(content: string): Array<string> {
return content.split(LINE_BREAK);
}

export function normalizeLineEndings(
content: string,
lineEnding: LineEnding,
): string {
return content.replace(/\r\n|\r|\n/g, lineEnding);
return splitLines(content).join(lineEnding);
}
Loading
Loading