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
50 changes: 50 additions & 0 deletions third_party/muya/src/state/__tests__/codeFenceInfoString.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest';
import { MarkdownToState } from '../markdownToState';
import ExportMarkdown from '../stateToMarkdown';

function roundTrip(markdown: string): string {
const states = new MarkdownToState().generate(markdown);
return new ExportMarkdown({ listIndentation: 1 }).generate(states);
}

describe('#4770: fenced code block info string round-trip', () => {
it('preserves a Pandoc/RMarkdown-style attribute info string', () => {
const markdown = '```{example, listing1-name}\nlabel for code listing 1\n```\n';

expect(roundTrip(markdown)).toContain('```{example, listing1-name}');
});

it('preserves a language followed by attributes', () => {
const markdown = '```js title="app.js"\nconst a = 1\n```\n';

expect(roundTrip(markdown)).toContain('```js title="app.js"');
});

it('leaves a plain single-word language unchanged', () => {
const output = roundTrip('```js\nconst a = 1\n```\n');

expect(output).toContain('```js\n');
});

it('leaves a language-less fence unchanged', () => {
const output = roundTrip('```\nplain\n```\n');

expect(output).toContain('```\n');
expect(output).not.toContain('```undefined');
});

it('uses the edited language when a stored info string is now stale', () => {
const states = [
{
name: 'code-block' as const,
meta: { type: 'fenced', lang: 'python', info: '{example, listing1-name}' },
text: 'x',
},
];

const output = new ExportMarkdown({ listIndentation: 1 }).generate(states);

expect(output).toContain('```python\n');
expect(output).not.toContain('example');
});
});
2 changes: 2 additions & 0 deletions third_party/muya/src/state/markdownToState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,14 @@ export class MarkdownToState {
// but `'fenced'` reaches us at runtime via the
// walkTokens assignment — hence the cast.
const isFenced = (codeBlockStyle as 'indented' | 'fenced' | undefined) === 'fenced';
const info = infoString || '';
return {
name: 'code-block' as const,
meta: {
type: isFenced ? 'fenced' : 'indented',
lang,
...(isFenced && fenceLength && fenceLength > 3 ? { fenceLength } : {}),
...(isFenced && info !== lang ? { info } : {}),
},
text: value,
};
Expand Down
3 changes: 2 additions & 1 deletion third_party/muya/src/state/stateToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,11 @@ export default class ExportMarkdown {
const { text, meta } = state;
const textList = text.split('\n');
const { type, lang } = meta;
const info = meta.info && meta.info.match(/\S*/)?.[0] === lang ? meta.info : lang;

if (type === 'fenced') {
const fence = '`'.repeat(this._codeFenceLength(text, meta.fenceLength));
result.push(`${indent}${lang ? `${fence}${lang}\n` : `${fence}\n`}`);
result.push(`${indent}${info ? `${fence}${info}\n` : `${fence}\n`}`);
textList.forEach((text) => {
result.push(`${indent}${text}\n`);
});
Expand Down
1 change: 1 addition & 0 deletions third_party/muya/src/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ICodeBlockState {
type: string; // "indented" | "fenced";
lang: string;
fenceLength?: number;
info?: string;
};
text: string;
}
Expand Down
Loading