Skip to content

Commit

Permalink
Merge pull request #82 from ony3000/fix-markdown
Browse files Browse the repository at this point in the history
Leave Prettier's output as is for code blocks embedded in markdown and mdx
  • Loading branch information
ony3000 authored Dec 8, 2024
2 parents e190f16 + 8c127a0 commit 4300b56
Show file tree
Hide file tree
Showing 30 changed files with 1,239 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/packages/v2-plugin/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { parsers as babelParsers } from 'prettier/parser-babel';
import { parsers as htmlParsers } from 'prettier/parser-html';
import { parsers as typescriptParsers } from 'prettier/parser-typescript';

const EOL = '\n';

const addon = {
parseBabel: (text: string, options: ParserOptions) =>
babelParsers.babel.parse(text, { babel: babelParsers.babel }, options),
Expand All @@ -25,6 +27,34 @@ function transformParser(
parsers: { [parserName: string]: Parser },
options: ParserOptions & ThisPluginOptions,
): FormattedTextAST => {
if (options.parentParser === 'markdown' || options.parentParser === 'mdx') {
let codeblockStart = '```';
const codeblockEnd = '```';

if (options.parser === 'babel') {
codeblockStart = '```jsx';
} else if (options.parser === 'typescript') {
codeblockStart = '```tsx';
}

const formattedCodeblock = format(`${codeblockStart}${EOL}${text}${EOL}${codeblockEnd}`, {
...options,
plugins: [],
rangeEnd: Infinity,
endOfLine: 'lf',
parser: options.parentParser,
parentParser: undefined,
});
const formattedText = formattedCodeblock
.trim()
.slice(`${codeblockStart}${EOL}`.length, -`${EOL}${codeblockEnd}`.length);

return {
type: 'FormattedText',
body: formattedText,
};
}

const plugins = options.plugins.filter((plugin) => typeof plugin !== 'string') as Plugin[];

let languageImplementedPlugin: Plugin | undefined;
Expand Down
33 changes: 33 additions & 0 deletions src/packages/v3-plugin/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { parsers as babelParsers } from 'prettier/plugins/babel';
import { parsers as htmlParsers } from 'prettier/plugins/html';
import { parsers as typescriptParsers } from 'prettier/plugins/typescript';

const EOL = '\n';

const addon = {
parseBabel: (text: string, options: ParserOptions) => babelParsers.babel.parse(text, options),
parseTypescript: (text: string, options: ParserOptions) =>
Expand All @@ -23,6 +25,37 @@ function transformParser(
text: string,
options: ParserOptions & ThisPluginOptions,
): Promise<FormattedTextAST> => {
if (options.parentParser === 'markdown' || options.parentParser === 'mdx') {
let codeblockStart = '```';
const codeblockEnd = '```';

if (options.parser === 'babel') {
codeblockStart = '```jsx';
} else if (options.parser === 'typescript') {
codeblockStart = '```tsx';
}

const formattedCodeblock = await format(
`${codeblockStart}${EOL}${text}${EOL}${codeblockEnd}`,
{
...options,
plugins: [],
rangeEnd: Infinity,
endOfLine: 'lf',
parser: options.parentParser,
parentParser: undefined,
},
);
const formattedText = formattedCodeblock
.trim()
.slice(`${codeblockStart}${EOL}`.length, -`${EOL}${codeblockEnd}`.length);

return {
type: 'FormattedText',
body: formattedText,
};
}

const plugins = options.plugins.filter((plugin) => typeof plugin !== 'string') as Plugin[];

let languageImplementedPlugin: Plugin | undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`'(1) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
<div />
\`\`\`
"
`;

exports[`'(2) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(3) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;

exports[`'(4) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
<div />
\`\`\`
"
`;

exports[`'(5) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(6) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;
59 changes: 59 additions & 0 deletions tests/v2-test/markdown/issue-80/__snapshots__/ideal.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`'(1) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
<div />
\`\`\`
"
`;

exports[`'(2) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(3) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;

exports[`'(4) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
<div />
\`\`\`
"
`;

exports[`'(5) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(6) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`'(1) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
<div />
\`\`\`
"
`;

exports[`'(2) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(3) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`jsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;

exports[`'(4) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
<div />
\`\`\`
"
`;

exports[`'(5) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
import foo from "foo";
<div />;
\`\`\`
"
`;

exports[`'(6) This plugin doesn\\'t support markdown parser, so it leaves Prettier\\'s output as is.' > expectation 1`] = `
"\`\`\`tsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
"
`;
13 changes: 13 additions & 0 deletions tests/v2-test/markdown/issue-80/absolute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { baseOptions } from 'test-settings';

import { thisPlugin, testSnapshotEach } from '../../adaptor';
import { fixtures } from './fixtures';

const options = {
...baseOptions,
plugins: [thisPlugin],
parser: 'markdown',
endingPosition: 'absolute',
};

testSnapshotEach(fixtures, options);
78 changes: 78 additions & 0 deletions tests/v2-test/markdown/issue-80/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { Fixture } from 'test-settings';

export const fixtures: Omit<Fixture, 'output'>[] = [
{
name: "(1) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`jsx
<div />;
\`\`\`
`,
options: {},
},
{
name: "(2) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`jsx
import foo from 'foo'
<div />
\`\`\`
`,
options: {},
},
{
name: "(3) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`jsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
`,
options: {
printWidth: 60,
},
},
{
name: "(4) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`tsx
<div />;
\`\`\`
`,
options: {},
},
{
name: "(5) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`tsx
import foo from 'foo'
<div />
\`\`\`
`,
options: {},
},
{
name: "(6) This plugin doesn't support markdown parser, so it leaves Prettier's output as is.",
input: `
\`\`\`tsx
export function Foo({ children }) {
return (
<div className="lorem ipsum dolor sit amet consectetur adipiscing elit proin ex massa hendrerit eu posuere eu volutpat id neque pellentesque">
{children}
</div>
);
}
\`\`\`
`,
options: {
printWidth: 60,
},
},
];
13 changes: 13 additions & 0 deletions tests/v2-test/markdown/issue-80/ideal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { baseOptions } from 'test-settings';

import { thisPlugin, testSnapshotEach } from '../../adaptor';
import { fixtures } from './fixtures';

const options = {
...baseOptions,
plugins: [thisPlugin],
parser: 'markdown',
endingPosition: 'absolute-with-indent',
};

testSnapshotEach(fixtures, options);
13 changes: 13 additions & 0 deletions tests/v2-test/markdown/issue-80/relative.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { baseOptions } from 'test-settings';

import { thisPlugin, testSnapshotEach } from '../../adaptor';
import { fixtures } from './fixtures';

const options = {
...baseOptions,
plugins: [thisPlugin],
parser: 'markdown',
endingPosition: 'relative',
};

testSnapshotEach(fixtures, options);
Loading

0 comments on commit 4300b56

Please sign in to comment.