-
Notifications
You must be signed in to change notification settings - Fork 59.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule for Markdown table Liquid syntax (#52092)
- Loading branch information
Showing
6 changed files
with
168 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
83 changes: 83 additions & 0 deletions
83
src/content-linter/lib/linting-rules/table-liquid-versioning.js
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,83 @@ | ||
import { addError, filterTokens } from 'markdownlint-rule-helpers' | ||
|
||
// Detects a Markdown table delimiter row | ||
const delimiterRegexPure = /(\s)*(:)?(-+)(:)?(\s)*(\|)/ | ||
// Detects a Markdown table delimiter row with a Liquid tag | ||
const delimiterRegex = /(\s)*(:)?(-+)(:)?(\s)*(\|).*({%.*(ifversion|else|endif).*%})/ | ||
// Detects a Liquid versioning tag | ||
const liquidRegex = /^{%-?\s*(ifversion|else|endif).*-?%}/ | ||
// Detects a Markdown table row with a Liquid versioning tag | ||
const liquidAfterRowRegex = /(\|{1}).*(\|{1}).*{%\s*(ifversion|else|endif).*%}$/ | ||
|
||
export const tableLiquidVersioning = { | ||
names: ['GHD040', 'table-liquid-versioning'], | ||
description: 'Tables must use the correct liquid versioning format', | ||
severity: 'error', | ||
tags: ['tables'], | ||
information: new URL('https://github.com/github/docs/blob/main/src/content-linter/README.md'), | ||
function: function GHD040(params, onError) { | ||
const lines = params.lines | ||
let inTable = false | ||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i] | ||
|
||
if (inTable && (!line || isPreviousLineIndented(lines[i], lines[i - 1]))) { | ||
inTable = false | ||
continue | ||
} | ||
|
||
if (delimiterRegexPure.test(line)) { | ||
// A table with rows is at least 3 lines | ||
if (lines[i - 1] && lines[i + 1]) { | ||
inTable = true | ||
if (liquidAfterRowRegex.test(lines[i - 1])) { | ||
addError( | ||
onError, | ||
i, | ||
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.', | ||
lines[i - 1], | ||
null, | ||
) | ||
} | ||
if (delimiterRegex.test(line)) { | ||
addError( | ||
onError, | ||
i + 1, | ||
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.', | ||
line, | ||
null, | ||
) | ||
} | ||
continue | ||
} | ||
} | ||
if (inTable) { | ||
if (liquidRegex.test(line)) { | ||
addError( | ||
onError, | ||
i + 1, | ||
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`. If the conditional is on its own line but is not related to the table, ensure there is one new line beween a Liquid version tag and the table.', | ||
line, | ||
null, | ||
) | ||
} | ||
if (liquidAfterRowRegex.test(line)) { | ||
addError( | ||
onError, | ||
i + 1, | ||
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.', | ||
line, | ||
null, | ||
) | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
|
||
function isPreviousLineIndented(line, previousLine) { | ||
if (!line || !previousLine) return false | ||
const numWhitespaceLine = line.length - line.trimLeft().length | ||
const numWhitespacePrevLine = previousLine.length - previousLine.trimLeft().length | ||
return numWhitespaceLine < numWhitespacePrevLine | ||
} |
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,59 @@ | ||
--- | ||
title: Examples of tables in Markdown | ||
descriptions: Examples of tables in Markdown | ||
versions: | ||
fpt: '*' | ||
ghes: '*' | ||
ghec: '*' | ||
--- | ||
|
||
## Good | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---| | ||
| {% ifversion volvo %} | | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` | | ||
| {% endif %} | | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---| | ||
| {%- ifversion volvo %} | | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` | | ||
| {%- endif %} | | ||
|
||
{% ifversion volvo %} | ||
|
||
1. This is a list with a table | ||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---| | ||
| {%- ifversion volvo %} | | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` | | ||
| {%- endif %} | | ||
{% endif %} | ||
|
||
## Bad | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---| | ||
{%- ifversion volvo %} | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` | | ||
{%- endif %} | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---|{% ifversion volvo %} | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` |{% endif %} | ||
|
||
{% ifversion volvo %} | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | | ||
| --- | --- | --- | ---| | ||
| Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` | | ||
{% endif %} | ||
|
||
Package manager | Languages | Recommended formats | All supported formats {% ifversion fpt %} | ||
:- | :- | :- | :-{% endif %}{% ifversion volvo %} | ||
Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock` {% endif %} | ||
|
||
| Package manager | Languages | Recommended formats | All supported formats | {% ifversion fpt %} | ||
| :- | :- | :- | :-|{% endif %}{% ifversion volvo %} | ||
|Cargo | Rust | `Cargo.lock` | `Cargo.toml`, `Cargo.lock`| {% endif %} |
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,17 @@ | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { runRule } from '../../lib/init-test.js' | ||
import { tableLiquidVersioning } from '../../lib/linting-rules/table-liquid-versioning.js' | ||
|
||
const FIXTURE_FILEPATH = 'src/content-linter/tests/fixtures/tables.md' | ||
|
||
describe(tableLiquidVersioning.names.join(' - '), () => { | ||
test('non-early access file with early access references fails', async () => { | ||
const result = await runRule(tableLiquidVersioning, { files: [FIXTURE_FILEPATH] }) | ||
const errors = result[FIXTURE_FILEPATH] | ||
expect(errors.length).toBe(11) | ||
const lineNumbers = errors.map((error) => error.lineNumber) | ||
const expectedErrorLines = [38, 40, 43, 44, 51, 53, 54, 55, 57, 58, 59] | ||
expect(JSON.stringify(lineNumbers)).toEqual(JSON.stringify(expectedErrorLines)) | ||
}) | ||
}) |