Skip to content

Commit

Permalink
feat: add typograph command (#8)
Browse files Browse the repository at this point in the history
* feat: add typogrpah command

* test: add test for typograph command, bump version to 0.1.9

* fix: remove lint-src before typograph

* fix: add stdout to createconfig commands, add create typograph config test

* update: supress rules in typograph config
  • Loading branch information
holamgadol authored Mar 14, 2023
1 parent fda236d commit 61f6962
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ Run _foliant-md-linter_ from the project root with following commands and option
- `-v`, `-s`, `-c`, `-p`, `-d`, `-f`, `-l`
- `fix` Fix formatting errors with markdownlint
- `-v`, `-s`, `-c`, `-p`, `-d`, `-f`, `-l`
- `typograph` Fix typograph errors with markdownlint
- `-v`, `-s`, `-c`, `-p`, `-d`, `-f`, `-l`
- `print` Print linting results
- `-v`
- `create-full-config` Create markdownlint config for styleguide adherence
- `-s`, `-p`, `-d`
- `create-slim-config` Create markdownlint config for critical errors check
- `-s`, `-p`, `-d`

- `create-typograph-config` Create typograph config for typograph errors check
- `-s`, `-p`, `-d`

### Examples

Expand Down
63 changes: 62 additions & 1 deletion generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ function createConfig (mode = 'full', source = '', project = '') {
MD049: { style: 'underscore' },
MD050: { style: 'asterisk' },
MD051: false,
MD052: true,
MD053: false,
'indented-fence': true,
'non-literal-fence-label': true,
'fenced-code-in-quote': true,
Expand Down Expand Up @@ -111,6 +113,8 @@ function createConfig (mode = 'full', source = '', project = '') {
MD049: false,
MD050: false,
MD051: false,
MD052: true,
MD053: false,
'indented-fence': true,
'non-literal-fence-label': true,
'fenced-code-in-quote': true,
Expand All @@ -121,11 +125,67 @@ function createConfig (mode = 'full', source = '', project = '') {
}
}

const configTypograph = {
MD001: false,
MD003: false,
MD004: false,
MD005: false,
MD007: false,
MD009: false,
MD010: false,
MD011: false,
MD012: false,
MD013: false,
MD014: false,
MD018: false,
MD019: false,
MD020: false,
MD021: false,
MD022: false,
MD023: false,
MD024: false,
MD025: false,
MD026: false,
MD027: false,
MD028: false,
MD029: false,
MD030: false,
MD031: false,
MD032: false,
MD033: false,
MD034: false,
MD035: false,
MD036: false,
MD037: false,
MD038: false,
MD040: false,
MD041: false,
MD042: false,
MD043: false,
MD044: false,
MD045: false,
MD046: false,
MD047: false,
MD048: false,
MD049: false,
MD050: false,
MD051: false,
MD052: false,
MD053: false,
'indented-fence': false,
'non-literal-fence-label': false,
'fenced-code-in-quote': false,
typograph: true,
'validate-internal-links': false
}

let config
if (mode === 'slim') {
config = configSlim
} else if (mode === 'default') {
return null
} else if (mode === 'typograph') {
config = configTypograph
} else {
config = configFull
}
Expand All @@ -135,13 +195,14 @@ function createConfig (mode = 'full', source = '', project = '') {
}
const json = JSON.stringify(obj, null, 4)
fs.writeFileSync(path.resolve(cwd, '.markdownlint-cli2.jsonc'), json, 'utf8')
console.log(`${mode} markdownlint config created succesfully!`)
}

program
.name('create-markdownlint-config')
.description('script for generating .markdownlint-cli2.jsonc in foliant-project root')
.version('0.0.1')
.option('-m, --mode <mode>', 'full, slim or default config', 'full')
.option('-m, --mode <mode>', 'full, slim, typograph or default config', 'full')
.option('-s, --source <source>', 'relative path to source directory', '')
.option('-p, --project <project>', 'project name', '')

Expand Down
24 changes: 24 additions & 0 deletions linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ const commandsGen = function (src = defaultSrc, customConfig = false, project =
const and = (isWin === true) ? '&' : ';'
commands.createFullMarkdownlintConfig = (customConfig === false) ? `node ${path.join(__dirname, '/generate.js')} -m full -s ${src} -p "${project}"` : 'echo "using custom config"'
commands.createSlimMarkdownlintConfig = (customConfig === false) ? `node ${path.join(__dirname, '/generate.js')} -m slim -s ${src} -p "${project}"` : 'echo "using custom config"'
commands.createTypographMarkdownlintConfig = (customConfig === false) ? `node ${path.join(__dirname, '/generate.js')} -m typograph -s ${src} -p "${project}"` : 'echo "using custom config"'
commands.markdownlintSrcSlim = `${commands.createSlimMarkdownlintConfig} && ${execPath}/markdownlint-cli2 "${src}/**/*.md" ${writeLog(markdownLintSlimLog)}`
commands.markdownlintSrcFull = `${commands.markdownlintSrcSlim} ${and} ${commands.createFullMarkdownlintConfig} && ${execPath}/markdownlint-cli2 "${src}/**/*.md" ${writeLog(markdownLintFullLog)}`
commands.markdownlintSrcFix = `${commands.markdownlintSrcSlim} ${and} ${commands.createFullMarkdownlintConfig} && ${execPath}/markdownlint-cli2-fix "${src}/**/*.md" ${writeLog(markdownLintFullLog)}`
commands.markdownlintSrcTypograph = `${commands.createTypographMarkdownlintConfig} && ${execPath}/markdownlint-cli2-fix "${src}/**/*.md" ${writeLog(markdownLintFullLog)}`
commands.markdownlinkcheckSrcUnix = `find ${src}/ -type f -name '*.md' -print0 | xargs -0 -n1 ${execPath}/markdown-link-check -p -c ${path.join(__dirname, '/configs/mdLinkCheckConfig.json')} ${writeLog(path.join(cwd, markdownLinkCheckLog))}`
commands.markdownlinkcheckSrcWin = `del ${path.join(cwd, markdownLinkCheckLog)} & forfiles /P ${src} /S /M *.md /C "cmd /c npx markdown-link-check @file -p -c ${path.join(__dirname, '/configs/mdLinkCheckConfig.json')} ${writeLog(path.join(cwd, markdownLinkCheckLog))}"`
commands.markdownlinkcheckSrc = (isWin === true) ? commands.markdownlinkcheckSrcWin : commands.markdownlinkcheckSrcUnix
Expand Down Expand Up @@ -294,6 +296,19 @@ program.command('fix')
execute(commandsGen(options.source, options.config, options.project).commands.markdownlintSrcFix, options.verbose, options.debug, options.allowfailure, options.clearconfig)
})

program.command('typograph')
.description('Fix typographic errors with markdownlint')
.addOption(verboseOption)
.addOption(sourceOption)
.addOption(configOption)
.addOption(projectOption)
.addOption(debugOption)
.addOption(allowfailureOption)
.addOption(clearconfigOption)
.action((options) => {
execute(commandsGen(options.source, options.config, options.project).commands.markdownlintSrcTypograph, options.verbose, options.debug, options.allowfailure, options.clearconfig)
})

program.command('print')
.description('Print linting results')
.addOption(verboseOption)
Expand All @@ -319,4 +334,13 @@ program.command('create-slim-config')
execute(commandsGen(options.source, options.config, options.project).commands.createSlimMarkdownlintConfig, options.verbose, options.debug)
})

program.command('create-typograph-config')
.description('Create typograph markdownlint config')
.addOption(sourceOption)
.addOption(projectOption)
.addOption(debugOption)
.action((options) => {
execute(commandsGen(options.source, options.config, options.project).commands.createTypographMarkdownlintConfig, options.verbose, options.debug)
})

program.parse()
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foliant-md-linter",
"version": "0.1.8",
"version": "0.1.9",
"description": "CLI tool for linting Foliant markdown sources",
"license": "MIT",
"homepage": "https://github.com/holamgadol/foliant-md-linter",
Expand All @@ -22,7 +22,7 @@
"commander": "^10.0.0",
"markdown-link-check": "^3.10.3",
"markdownlint-cli2": "^0.6.0",
"markdownlint-rules-foliant": "^0.1.8"
"markdownlint-rules-foliant": "^0.1.9"
},
"devDependencies": {
"eslint": "^7.32.0",
Expand Down
119 changes: 117 additions & 2 deletions test/linter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test('First print', async () => {
})

test('create-full-config', async () => {
const expectedStdout = ['Command completed with no errors!\n']
const expectedStdout = ['']
const result = await cli(['create-full-config'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)
Expand All @@ -54,7 +54,7 @@ test('create-full-config', async () => {
})

test('create-slim-config', async () => {
const expectedStdout = ['Command completed with no errors!\n']
const expectedStdout = ['']
const result = await cli(['create-slim-config'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)
Expand All @@ -63,6 +63,16 @@ test('create-slim-config', async () => {
expect(result.code).toEqual(0)
})

test('create-typograph-config', async () => {
const expectedStdout = ['']
const result = await cli(['create-typograph-config'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('slim', async () => {
const expectedStdout = [
'Checked 2 files\n',
Expand Down Expand Up @@ -634,6 +644,111 @@ test('fix -v -c -s no-errors-src -f', async () => {
expect(result.code).toEqual(0)
})

test('typograph', async () => {
const expectedStdout = [
'Checked 2 files\n',
'Found 0 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -l', async () => {
const expectedStdout = [
'Checked 2 files\n',
'Found 0 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`,
`removing ${path.join(cwd, '.markdownlint-cli2.jsonc ...')}`]
const result = await cli(['typograph', '-l'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(false)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -v', async () => {
const expectedStdout = [
'Checked 2 files\n',
'Found 0 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -v -p another-project', async () => {
const expectedStdout = [
'Checked 2 files\n',
'Found 0 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v', '-p another-project'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(true)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -v -c', async () => {
const expectedStdout = [
'Checked 2 files\n',
'Found 3 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v', '-c'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(false)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -v -c -s alt-src', async () => {
const expectedStdout = [
'Checked 1 files\n',
'Found 2 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v', '-c', '-s alt-src'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(false)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('typograph -v -c -s alt-src -f', async () => {
const expectedStdout = [
'Checked 1 files\n',
'Found 2 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v', '-c', '-s alt-src', '-f'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(false)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(1)
})

test('typograph -v -c -s no-errors-src -f', async () => {
const expectedStdout = [
'Checked 1 files\n',
'Found 0 styleguide and formatting errors\n',
`Full markdownlint log see in ${path.join(cwd, '.markdownlint_full.log')}\n`]
const result = await cli(['typograph', '-v', '-c', '-s no-errors-src', '-f'], '.')
expect(fs.existsSync(`${cwd}/.markdownlint-cli2.jsonc`)).toBe(false)
console.log(result)

expectedStdout.forEach(element => expect(result.stdout).toContain(element))
expect(result.code).toEqual(0)
})

test('urls', async () => {
const expectedStdout = [
'Found 2 broken external links\n',
Expand Down

0 comments on commit 61f6962

Please sign in to comment.