Skip to content

Commit

Permalink
fix: remove markdown workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 7, 2023
1 parent 3f53617 commit 822b571
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 51 deletions.
1 change: 0 additions & 1 deletion fixtures/output/all/markdown.md
Expand Up @@ -27,7 +27,6 @@ Jane Roe|JFK|314
+ with a [link] (/to/somewhere)
+ and [another one]


[another one]: http://example.com 'Example title'

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Expand Down
1 change: 0 additions & 1 deletion fixtures/output/js/markdown.md
Expand Up @@ -27,7 +27,6 @@ Jane Roe|JFK|314
+ with a [link] (/to/somewhere)
+ and [another one]


[another one]: http://example.com 'Example title'

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Expand Down
1 change: 0 additions & 1 deletion fixtures/output/tab-double-quotes/markdown.md
Expand Up @@ -27,7 +27,6 @@ Jane Roe|JFK|314
+ with a [link] (/to/somewhere)
+ and [another one]


[another one]: http://example.com 'Example title'

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Expand Down
1 change: 0 additions & 1 deletion fixtures/output/ts-override/markdown.md
Expand Up @@ -27,7 +27,6 @@ Jane Roe|JFK|314
+ with a [link] (/to/somewhere)
+ and [another one]


[another one]: http://example.com 'Example title'

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -71,6 +71,7 @@
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"eslint-config-flat-gitignore": "^0.1.2",
"eslint-parser-plain": "^0.1.0",
"eslint-plugin-antfu": "^2.0.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-i": "^2.29.0",
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions src/configs/formatters.ts
@@ -1,3 +1,4 @@
import * as parserPlain from 'eslint-parser-plain'
import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault } from '../utils'
Expand All @@ -7,7 +8,6 @@ import { StylisticConfigDefaults } from './stylistic'
export async function formatters(
options: OptionsFormatters | true = {},
stylistic: StylisticConfig = {},
markdownEnabled = true,
): Promise<FlatConfigItem[]> {
await ensurePackages([
'eslint-plugin-format',
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function formatters(
{
files: [GLOB_CSS, GLOB_POSTCSS],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:css',
rules: {
Expand All @@ -84,7 +84,7 @@ export async function formatters(
{
files: [GLOB_SCSS],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:scss',
rules: {
Expand All @@ -100,7 +100,7 @@ export async function formatters(
{
files: [GLOB_LESS],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:less',
rules: {
Expand All @@ -120,7 +120,7 @@ export async function formatters(
configs.push({
files: ['**/*.html'],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:html',
rules: {
Expand All @@ -139,7 +139,7 @@ export async function formatters(
configs.push({
files: ['**/*.toml'],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:toml',
rules: {
Expand All @@ -160,11 +160,9 @@ export async function formatters(
: options.markdown

configs.push({
files: markdownEnabled
? ['**/*.__markdown_content__']
: [GLOB_MARKDOWN],
files: [GLOB_MARKDOWN],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:markdown',
rules: {
Expand All @@ -189,7 +187,7 @@ export async function formatters(
configs.push({
files: ['**/*.graphql'],
languageOptions: {
parser: pluginFormat.parserPlain,
parser: parserPlain,
},
name: 'antfu:formatter:graphql',
rules: {
Expand Down
62 changes: 29 additions & 33 deletions src/configs/markdown.ts
@@ -1,11 +1,11 @@
import type { Linter } from 'eslint'
import * as parserPlain from 'eslint-parser-plain'
import type { FlatConfigItem, OptionsComponentExts, OptionsFiles, OptionsOverrides } from '../types'
import { GLOB_MARKDOWN, GLOB_MARKDOWN_CODE, GLOB_MARKDOWN_IN_MARKDOWN } from '../globs'
import { interopDefault } from '../utils'

export async function markdown(
options: OptionsFiles & OptionsComponentExts & OptionsOverrides = {},
formatMarkdown: boolean = false,
): Promise<FlatConfigItem[]> {
const {
componentExts = [],
Expand All @@ -20,38 +20,27 @@ export async function markdown(
// `eslint-plugin-markdown` only creates virtual files for code blocks,
// but not the markdown file itself. In order to format the whole markdown file,
// we need to create another virtual file for the markdown file itself.
const processor: Linter.Processor = !formatMarkdown
? {
meta: {
name: 'markdown-processor',
},
supportsAutofix: true,
...baseProcessor,
}
: {
meta: {
name: 'markdown-processor-with-content',
},
postprocess(messages, filename) {
const markdownContent = messages.pop()
const codeSnippets = baseProcessor.postprocess(messages, filename)
return [
...markdownContent || [],
...codeSnippets || [],
]
},
preprocess(text, filename) {
const result = baseProcessor.preprocess(text, filename)
return [
...result,
{
filename: '.__markdown_content__',
text,
},
]
},
supportsAutofix: true,
}
const processor: Linter.Processor = {
meta: {
name: 'markdown-processor-with-content',
},
postprocess(messages, filename) {
const markdownContent = messages.shift()
const codeSnippets = baseProcessor.postprocess(messages, filename)
return [
...markdownContent || [],
...codeSnippets || [],
]
},
preprocess(text, filename) {
const result = baseProcessor.preprocess(text, filename)
return [
text,
...result,
]
},
supportsAutofix: true,
}

return [
{
Expand All @@ -66,6 +55,13 @@ export async function markdown(
name: 'antfu:markdown:processor',
processor,
},
{
files,
languageOptions: {
parser: parserPlain,
},
name: 'antfu:markdown:parser',
},
{
files: [
GLOB_MARKDOWN_CODE,
Expand Down
2 changes: 0 additions & 2 deletions src/factory.ts
Expand Up @@ -172,7 +172,6 @@ export async function antfu(
componentExts,
overrides: overrides.markdown,
},
options.formatters === true || !!(options.formatters || {})?.markdown,
),
)
}
Expand All @@ -181,7 +180,6 @@ export async function antfu(
configs.push(formatters(
options.formatters,
typeof stylisticOptions === 'boolean' ? {} : stylisticOptions,
options.markdown !== false,
))
}

Expand Down

0 comments on commit 822b571

Please sign in to comment.