Skip to content

Commit

Permalink
fix: Fix code block processing
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Apr 9, 2024
1 parent b1da850 commit 4349c49
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/consumer/index.ts
Expand Up @@ -9,6 +9,8 @@ const replace = (from: number, to: number, source: string, past: string) => {
return start + past + end;
};

const last = <T>(array: T[], fallback: T): T => array.length ? array[array.length - 1] : fallback;

Check failure on line 12 in src/consumer/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Replace `array.length·?·array[array.length·-·1]·:·fallback` with `(array.length·?·array[array.length·-·1]·:·fallback)`

function countStartIndexes(acc: number[], line: string) {
acc.push(acc[acc.length - 1] + line.length + 1);

Expand All @@ -30,8 +32,6 @@ export enum CodeProcessing {
export class Consumer {
gap = 0;

limit = Infinity;

lines: number[];

compact: boolean;
Expand All @@ -42,8 +42,14 @@ export class Consumer {

hash: (tokens: Token[]) => string;

get limit() {
return last(this.limits, Infinity) + this.gap;
}

private cursor = 0;

private limits: number[] = [];

constructor(content: string, options: ConsumerOptions, hash: (tokens: Token[]) => string) {
this.lines = content.split('\n').reduce(countStartIndexes, [0]);
this.compact = Boolean(options.compact);
Expand All @@ -61,10 +67,16 @@ export class Consumer {

const parts = split(tokens);

return parts.map((part) => this.consume(part)).filter(Boolean) as {
const result = parts.map((part) => this.consume(part)).filter(Boolean) as {
part: Token[];
past: string;
}[];

if (window) {
this.unsetWindow();
}

return result;
};

consume = (part: Token[], past?: string) => {
Expand Down Expand Up @@ -94,7 +106,6 @@ export class Consumer {

this.content = replace(start, end, this.content, past);
this.cursor = start + past.length;
this.limit -= end - start - past.length;
this.gap -= end - start - past.length;
}

Expand Down Expand Up @@ -142,14 +153,16 @@ export class Consumer {
map = map || [0, this.lines.length - 1];
gap = gap || this.gap;

const [start, end] = [this.lines[map[0]] + gap, this.lines[map[1]] + gap];
const [start, end] = [this.lines[map[0]] + gap, this.lines[map[1]]];

this.limits.push(end);

if (start >= this.cursor) {
this.cursor = start;
}
}

if (end >= this.limit) {
this.limit = end;
}
private unsetWindow() {
this.limits.pop();
}
}
3 changes: 3 additions & 0 deletions src/skeleton/hooks/before-inline.ts
Expand Up @@ -18,6 +18,9 @@ export function beforeInline(parameters: CustomRendererHookParameters) {
return '';
}

/**
* Find next or prev (depends on step value) valuable token.
*/
function find(tokens: Token[], idx: number, step: number) {
while (tokens.length > idx && idx > 0) {
idx = idx + step;
Expand Down
4 changes: 3 additions & 1 deletion src/skeleton/rules/code.ts
Expand Up @@ -86,7 +86,9 @@ export const code: Renderer.RenderRuleRecord = {
return '';
}

this.state.process(token('text', {content: code.content}));
this.state.consume([token('skip', {skip: code.markup})]);
this.state.consume([token('text', {content: code.content})]);
this.state.consume([token('skip', {skip: code.markup})]);

return '';
},
Expand Down

0 comments on commit 4349c49

Please sign in to comment.