forked from jd-opensource/nutui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prettier-plugin (jd-opensource#2618)
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import linguistLanguages from 'linguist-languages'; | ||
import { printers as MarkdownPrinter, parsers as MarkdownParsers } from 'prettier/plugins/markdown'; | ||
|
||
export const languages = [ | ||
{ | ||
...linguistLanguages.Markdown, | ||
parsers: ['markdown'] | ||
} | ||
]; | ||
|
||
export const parsers = { | ||
markdown: { | ||
...MarkdownParsers.markdown | ||
} | ||
}; | ||
|
||
export const printers = { | ||
mdast: { | ||
...MarkdownPrinter.mdast, | ||
print: pluginPrint | ||
} | ||
}; | ||
|
||
function pluginPrint(path, options, print) { | ||
const node = path.getValue(); | ||
|
||
if (node.type == 'table') { | ||
return printTable(path, options, print); | ||
} | ||
return MarkdownPrinter.mdast.print(path, options, print); | ||
} | ||
|
||
function printTable(path, options, print) { | ||
const contents = path.map( | ||
() => | ||
path.map(() => { | ||
const text = print().flat(Infinity).join(''); | ||
return { text }; | ||
}, 'children'), | ||
'children' | ||
); | ||
|
||
const alignedTable = printTableContents(); | ||
return [alignedTable]; | ||
|
||
function printTableContents() { | ||
/** @type{Doc[]} */ | ||
const parts = [printRow(contents[0]), printAlign()]; | ||
if (contents.length > 1) { | ||
for (let i = 1; i < contents.length - 1; i++) { | ||
parts.push(printRow(contents[i])); | ||
} | ||
parts.push(printRow(contents[contents.length - 1], true)); | ||
} | ||
return parts; | ||
} | ||
|
||
function printAlign() { | ||
const align = contents[0].map(() => { | ||
return `---`; | ||
}); | ||
|
||
return `| ${align.join(' | ')} |\n`; | ||
} | ||
|
||
function printRow(rowContents, end = false) { | ||
const columns = rowContents.map(({ text }) => { | ||
return ` ${text} `; | ||
}); | ||
return end ? `|${columns.join('|')}|` : `|${columns.join('|')}|\n`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@nutui/prettier-plugin", | ||
"version": "0.0.1", | ||
"private": "true", | ||
"description": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^3.0.3", | ||
"linguist-languages": "^7.27.0" | ||
}, | ||
"keywords": [], | ||
"author": "jdf2e", | ||
"license": "MIT" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.