Skip to content

Commit

Permalink
fix: leave prettier's output as is for code blocks embedded in markdo…
Browse files Browse the repository at this point in the history
…wn and mdx
  • Loading branch information
ony3000 committed Dec 8, 2024
1 parent 384c7ae commit 8c127a0
Show file tree
Hide file tree
Showing 2 changed files with 63 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

0 comments on commit 8c127a0

Please sign in to comment.