Skip to content

Commit

Permalink
Add check for Windows-forbidden characters in the filename (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanspeck authored Oct 4, 2024
1 parent ee5eda7 commit 207c862
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com).

## Unreleased

- Add rule `TLDR111` ([#353](https://github.com/tldr-pages/tldr-lint/pull/353))

## [v0.0.15 - 2024-04-03](https://github.com/tldr-pages/tldr-lint/compare/v0.0.14...v0.0.15)

- Add rule `TLDR020` ([#308](https://github.com/tldr-pages/tldr-lint/pull/308))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ TLDR107 | File name should end with `.md` extension
TLDR108 | File name should not contain whitespace
TLDR109 | File name should be lowercase
TLDR110 | Command example should not be empty
TLDR111 | File name should not contain any Windows-forbidden character

[npm-url]: https://www.npmjs.com/package/tldr-lint
[npm-image]: https://img.shields.io/npm/v/tldr-lint.svg
Expand Down
5 changes: 5 additions & 0 deletions lib/tldr-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports.ERRORS = parser.ERRORS = {
'TLDR108': 'File name should not contain whitespace',
'TLDR109': 'File name should be lowercase',
'TLDR110': 'Command example should not be empty',
'TLDR111': 'File name should not contain any Windows-forbidden character'
};

(function(parser) {
Expand Down Expand Up @@ -212,6 +213,10 @@ linter.processFile = function(file, verbose, alsoFormat, ignoreErrors) {
result.errors.push({ locinfo: { first_line: '0' }, code: 'TLDR109', description: this.ERRORS.TLDR109 });
}

if (/[<>:"/\\|?*]/.test(path.basename(file))) {
result.errors.push({ locinfo: { first_line: '0' }, code: 'TLDR111', description: this.ERRORS.TLDR111 });
}

if (ignoreErrors) {
ignoreErrors = ignoreErrors.split(',').map(function(val) {
return val.trim();
Expand Down
7 changes: 7 additions & 0 deletions specs/pages/failing/111.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# jar

> JAR (Java Archive) is a package file format.
- Unzip file to the current directory:

`jar -xvf *.jar`
16 changes: 16 additions & 0 deletions specs/tldr-lint.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const linter = require('../lib/tldr-lint.js');
const { lintFile, containsErrors, containsOnlyErrors } = require('./tldr-lint-helper');

Expand Down Expand Up @@ -183,6 +184,21 @@ describe('Common TLDR formatting errors', function() {
expect(containsOnlyErrors(errors, 'TLDR110')).toBeTruthy();
expect(errors.length).toBe(1);
});

const invalidCharacters = ['<', '>', ':', '"', '/', '\\', '|', '?', '*'];
invalidCharacters.forEach((char) => {
it('TLDR111\t' + linter.ERRORS.TLDR111 + '\t - ${char}', function() {
const basenameSpy = jest.spyOn(path, 'basename').mockImplementation((filePath) => {
return `111${char}`;
});

let errors = lintFile(`pages/failing/111.md`).errors;
expect(containsOnlyErrors(errors, 'TLDR111')).toBeTruthy();
expect(errors.length).toBe(1);

basenameSpy.mockRestore();
});
});
});

describe('TLDR pages that are simply correct', function() {
Expand Down

0 comments on commit 207c862

Please sign in to comment.