import { readFileSync , existsSync } from 'fs' ; const deglob = require ( 'deglob' ) ; const gutil = require ( 'gulp-util' ) ; const moduleName = 'md-lint' ; interface Config { patterns : string [ ] ; options : { useGitIgnore ? : boolean ; usePackageJson ? : boolean ; configKey ? : string ; gitIgnoreFile ? : string ; ignore ? : string [ ] ; cwd ? : string ; } ; } function loadConfig ( ) { const defaultConfig = { patterns : [ '**/*.md' ] , options : { useGitIgnore : true , ignore : [ 'node_modules/**/*' ] , } , } ; if ( ! existsSync ( './package.json' ) ) { gutil . log ( moduleName , gutil . colors . yellow ( 'Can not found package.json, use default config' ) ) ; return defaultConfig ; } const packageConfig = readFileSync ( './package.json' ) . toString ( ) ; if ( packageConfig ) { const config = JSON . parse ( packageConfig ) ; if ( config [ moduleName ] ) return config [ moduleName ] ; } gutil . log ( moduleName , gutil . colors . yellow ( 'Load config from package.json error, use default config' ) ) ; return defaultConfig ; } function getMarkdownLint ( ) { return [ require ( 'remark-lint' ) , [ require ( 'remark-lint-file-extension' ) , 'md' ] , require ( 'remark-lint-no-file-name-mixed-case' ) , require ( 'remark-lint-no-file-name-articles' ) , require ( 'remark-lint-no-file-name-irregular-characters' ) , require ( 'remark-lint-no-file-name-consecutive-dashes' ) , require ( 'remark-lint-no-file-name-outer-dashes' ) , require ( 'remark-lint-no-consecutive-blank-lines' ) , require ( 'remark-lint-no-shell-dollars' ) , require ( 'remark-lint-hard-break-spaces' ) , [ require ( 'remark-lint-heading-style' ) , 'atx' ] , require ( 'remark-lint-heading-increment' ) , require ( 'remark-lint-no-duplicate-headings' ) , require ( 'remark-lint-no-multiple-toplevel-headings' ) , require ( 'remark-lint-maximum-heading-length' ) , [ require ( 'remark-lint-no-heading-punctuation' ) , ':.' ] , [ require ( 'remark-lint-blockquote-indentation' ) , 2 ] , require ( 'remark-lint-no-blockquote-without-marker' ) , [ require ( 'remark-lint-unordered-list-marker-style' ) ] , [ require ( 'remark-lint-ordered-list-marker-style' ) , '.' ] , [ require ( 'remark-lint-ordered-list-marker-value' ) , 'ordered' ] , [ require ( 'remark-lint-list-item-indent' ) , 'mixed' ] , require ( 'remark-lint-list-item-content-indent' ) , require ( 'remark-lint-list-item-spacing' ) , [ require ( 'remark-lint-code-block-style' ) , 'fenced' ] , [ require ( 'remark-lint-fenced-code-flag' ) , { allowEmpty : false } ] , [ require ( 'remark-lint-fenced-code-marker' ) , '`' ] , [ require ( 'remark-lint-rule-style' ) , '---' ] , require ( 'remark-lint-no-table-indentation' ) , require ( 'remark-lint-table-pipes' ) , require ( 'remark-lint-table-pipe-alignment' ) , [ require ( 'remark-lint-table-cell-padding' ) , 'padded' ] , require ( 'remark-lint-no-inline-padding' ) , require ( 'remark-lint-no-shortcut-reference-image' ) , require ( 'remark-lint-no-shortcut-reference-link' ) , require ( 'remark-lint-final-definition' ) , require ( 'remark-lint-definition-case' ) , require ( 'remark-lint-definition-spacing' ) , [ require ( 'remark-lint-link-title-style' ) , '"' ] , [ require ( 'remark-lint-strong-marker' ) , '*' ] , [ require ( 'remark-lint-emphasis-marker' ) , '*' ] , require ( 'remark-lint-no-emphasis-as-heading' ) , require ( 'remark-lint-no-literal-urls' ) , require ( 'remark-lint-no-auto-link-without-protocol' ) , require ( 'remark-lint-final-newline' ) , require ( 'remark-lint-list-item-bullet-indent' ) , require ( 'remark-lint-no-duplicate-definitions' ) , require ( 'remark-lint-no-heading-content-indent' ) , require ( 'remark-lint-no-undefined-references' ) , require ( 'remark-lint-no-unused-definitions' ) , ] ; } function doCheck ( files ) { const remark = require ( 'remark' ) ; const report = require ( 'vfile-reporter' ) ; const remarkInstance = remark ( ) . use ( getMarkdownLint ( ) ) ; files . forEach ( filePath => { const mdContent = readFileSync ( filePath , 'utf8' ) ; if ( mdContent ) { remarkInstance . process ( mdContent , ( err , file ) => { const res = report ( err || file ) ; console . log ( filePath ) ; console . log ( res ) ; console . log ( ) ; if ( res !== 'no issues found' ) process . exitCode = - 1 ; } ) ; } } ) ; } const config = loadConfig ( ) ; deglob ( config . patterns , config . options , ( err , files ) => { if ( err ) { gutil . log ( moduleName , gutil . colors . red ( err ) ) ; process . exit ( - 1 ) ; } doCheck ( files ) ; } ) ;