Skip to content

Commit

Permalink
feat: markdownlint rule to disallow opening angle brackets (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 13, 2024
1 parent 84c79b4 commit 33b2a5c
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 4 deletions.
3 changes: 2 additions & 1 deletion configs/markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"br_spaces": 0
},
"single-h1": false,
"no-inline-html": false
"no-inline-html": false,
"no-angle-brackets": false
}
25 changes: 25 additions & 0 deletions markdownlint-rules/emd002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { addError, filterTokens } = require('markdownlint/helpers');

module.exports = {
names: ['EMD002', 'no-angle-brackets'],
description: 'No unescaped opening angle brackets in text (does not play nice with MDX)',
tags: ['brackets'],
function: function EMD002(params, onError) {
filterTokens(params, 'inline', (token) => {
for (const childToken of token.children) {
// childToken.line has the raw content, but may also contain
// more content than just childToken.content. This may cause
// the same line to produce multiple errors, unfortunately.
if (
childToken.type === 'text' &&
childToken.markup !== '<' &&
childToken.markup !== '<' &&
childToken.content.includes('<') &&
childToken.line.match(/(?<!\\)</g) !== null
) {
addError(onError, token.lineNumber, 'Unescaped opening angle bracket');
}
}
});
},
};
6 changes: 6 additions & 0 deletions tests/__snapshots__/electron-markdownlint.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`electron-markdownlint should not allow opening angle brackets if EMD002 enabled 1`] = `
"<root>angle-brackets.md:114 EMD002/no-angle-brackets No unescaped opening angle brackets in text (does not play nice with MDX) [Unescaped opening angle bracket]
"
`;
4 changes: 3 additions & 1 deletion tests/electron-lint-markdown-links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('electron-lint-markdown-links', () => {
'--root',
FIXTURES_DIR,
'--ignore',
'**/{broken,valid}-*-link.md',
'**/{{broken,valid}-*-link.md,*angle-brackets.md}',
'*.md',
);

Expand All @@ -45,6 +45,8 @@ describe('electron-lint-markdown-links', () => {
'**/broken-{external,internal}-link.md',
'--ignore',
'**/{broken,valid}-cross-file-link.md',
'--ignore',
'**/*angle-brackets.md',
'*.md',
);

Expand Down
40 changes: 38 additions & 2 deletions tests/electron-markdownlint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe('electron-markdownlint', () => {
path.resolve(__dirname, '../dist/bin/markdownlint-cli-wrapper.js'),
path.resolve(FIXTURES_DIR, 'shortcut-links.md'),
],
{ stdio: 'pipe' },
{ stdio: 'pipe', encoding: 'utf-8' },
);

expect(stderr.toString('utf-8')).toContain('EMD001/no-shortcut-reference-links');
expect(stderr).toContain('EMD001/no-shortcut-reference-links');
expect(status).toEqual(1);
});

Expand All @@ -30,4 +30,40 @@ describe('electron-markdownlint', () => {

expect(status).toEqual(0);
});

it('should not allow opening angle brackets if EMD002 enabled', () => {
const { status, stderr, stdout } = cp.spawnSync(
process.execPath,
[
path.resolve(__dirname, '../dist/bin/markdownlint-cli-wrapper.js'),
'--enable',
'EMD002',
'--',
path.resolve(FIXTURES_DIR, 'angle-brackets.md'),
],
{ stdio: 'pipe', encoding: 'utf-8' },
);

expect(stderr.replace(`${FIXTURES_DIR}${path.sep}`, '<root>')).toMatchSnapshot();
expect(stdout).toBe('');
expect(status).toEqual(1);
});

it('should allow escaped opening angle brackets if EMD002 enabled', () => {
const { status, stderr, stdout } = cp.spawnSync(
process.execPath,
[
path.resolve(__dirname, '../dist/bin/markdownlint-cli-wrapper.js'),
'--enable',
'EMD002',
'--',
path.resolve(FIXTURES_DIR, 'escaped-angle-brackets.md'),
],
{ stdio: 'pipe', encoding: 'utf-8' },
);

expect(stderr).toBe('');
expect(stdout).toBe('');
expect(status).toEqual(0);
});
});
Loading

0 comments on commit 33b2a5c

Please sign in to comment.