Skip to content

Commit 0812502

Browse files
authored
fix: improve SARIF formatter (help text markdown) (#1665)
* fix: improve SARIF formatter (help text markdown) * Update sarif.ts
1 parent 3f3a048 commit 0812502

File tree

5 files changed

+98
-57
lines changed

5 files changed

+98
-57
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "htmlhint",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "The Static Code Analysis Tool for your HTML",
55
"keywords": [
66
"html",

src/cli/formatters/sarif.ts

Lines changed: 89 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,91 @@ const pkg = require('../../../package.json')
1818
* @returns Markdown content for the rule or undefined if not found
1919
*/
2020
function getRuleMarkdown(ruleId: string): string | undefined {
21-
const mdxFilePath = path.join(
22-
process.cwd(),
23-
'website',
24-
'src',
25-
'content',
26-
'docs',
27-
'rules',
28-
`${ruleId}.mdx`
29-
)
30-
31-
try {
32-
if (existsSync(mdxFilePath)) {
33-
const content = readFileSync(mdxFilePath, 'utf8')
34-
35-
// Extract content after frontmatter
36-
const frontmatterEnd = content.indexOf('---', 4) + 3
37-
if (frontmatterEnd > 3) {
38-
// Skip the frontmatter and extract the actual markdown content
39-
const markdown = content.substring(frontmatterEnd).trim()
40-
41-
// Process the content line by line for better control
42-
const lines = markdown.split(/\r?\n/)
43-
44-
// Remove the import line
45-
const filteredLines = lines.filter(
46-
(line) =>
47-
!line.includes(
48-
"import { Badge } from '@astrojs/starlight/components';"
49-
)
50-
)
51-
52-
// Join the lines back together
53-
let processedMarkdown = filteredLines.join('\n')
54-
55-
// Replace all Badge component instances with plain text
56-
// This matches the standard pattern used in the rule documentation
57-
processedMarkdown = processedMarkdown.replace(
58-
/<Badge\s+text="([^"]+)"[^>]*\/>/g,
59-
'$1'
60-
)
61-
62-
// Wrap HTML elements in backticks for proper markdown formatting
63-
// This matches HTML tags, DOCTYPE declarations, and other HTML elements
64-
processedMarkdown = processedMarkdown.replace(
65-
/(<\/?[a-zA-Z][^>\s]*[^>]*>|<!DOCTYPE[^>]*>)/g,
66-
'`$1`'
67-
)
68-
69-
// Replace any other Astro-specific components or syntax if needed
70-
71-
return processedMarkdown
21+
// Try multiple possible locations for the rule documentation
22+
const possiblePaths = [
23+
// Standard path from process.cwd()
24+
path.join(
25+
process.cwd(),
26+
'website',
27+
'src',
28+
'content',
29+
'docs',
30+
'rules',
31+
`${ruleId}.mdx`
32+
),
33+
// Absolute path based on module location
34+
path.join(
35+
path.dirname(require.resolve('../../../package.json')),
36+
'website',
37+
'src',
38+
'content',
39+
'docs',
40+
'rules',
41+
`${ruleId}.mdx`
42+
),
43+
// Handle case where we're in the website directory
44+
path.join(
45+
process.cwd(),
46+
'src',
47+
'content',
48+
'docs',
49+
'rules',
50+
`${ruleId}.mdx`
51+
),
52+
]
53+
54+
// Try each path until we find one that exists
55+
for (const mdxFilePath of possiblePaths) {
56+
try {
57+
if (existsSync(mdxFilePath)) {
58+
const content = readFileSync(mdxFilePath, 'utf8')
59+
60+
// Extract content after frontmatter
61+
const frontmatterEnd = content.indexOf('---', 4) + 3
62+
if (frontmatterEnd > 3) {
63+
// Skip the frontmatter and extract the actual markdown content
64+
const markdown = content.substring(frontmatterEnd).trim()
65+
66+
// Process the content line by line for better control
67+
const lines = markdown.split(/\r?\n/)
68+
69+
// Remove the import line
70+
const filteredLines = lines.filter(
71+
(line) =>
72+
!line.includes(
73+
"import { Badge } from '@astrojs/starlight/components';"
74+
)
75+
)
76+
77+
// Join the lines back together
78+
let processedMarkdown = filteredLines.join('\n')
79+
80+
// Replace all Badge component instances with plain text
81+
// This matches the standard pattern used in the rule documentation
82+
processedMarkdown = processedMarkdown.replace(
83+
/<Badge\s+text="([^"]+)"[^>]*\/>/g,
84+
'$1'
85+
)
86+
87+
// Wrap HTML elements in backticks for proper markdown formatting
88+
// This matches HTML tags, DOCTYPE declarations, and other HTML elements
89+
processedMarkdown = processedMarkdown.replace(
90+
/(<\/?[a-zA-Z][^>\s]*[^>]*>|<!DOCTYPE[^>]*>)/g,
91+
'`$1`'
92+
)
93+
94+
// Handle code blocks - ensure they're preserved as is
95+
processedMarkdown = processedMarkdown.replace(
96+
/```html\n([\s\S]*?)```/g,
97+
(match, code) => `\`\`\`html\n${code}\`\`\``
98+
)
99+
100+
return processedMarkdown
101+
}
72102
}
103+
} catch (error) {
104+
// Silent error handling for missing documentation files
73105
}
74-
} catch (error) {
75-
// Silently fail if file doesn't exist or can't be read
76106
}
77107

78108
return undefined
@@ -165,9 +195,15 @@ const sarifFormatter: FormatterCallback = function (formatter) {
165195

166196
const updatedSarifContent = JSON.stringify(sarifJson, null, 2)
167197
writeFileSync('htmlhint.sarif', updatedSarifContent)
198+
199+
// Output the SARIF content to stdout for CLI usage
200+
console.log(updatedSarifContent)
168201
} catch (error) {
169202
// If there's an error, fall back to the original content
170203
writeFileSync('htmlhint.sarif', sarifContent)
204+
205+
// Output the original SARIF content to stdout for CLI usage
206+
console.log(sarifContent)
171207
}
172208
})
173209
}

website/src/content/docs/changelog.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ description: The release notes for HTMLHint, see what's changed with each new ve
55

66
import { Badge } from '@astrojs/starlight/components'
77

8+
## 1.6.1 _(2025-06-17)_
9+
10+
- <Badge text="Fix" size="small" variant="danger" /> Improve SARIF report formatting
11+
- <Badge text="Fix" size="small" variant="danger" /> Improve [`attr-value-no-duplication`](/rules/attr-value-no-duplication/) logic
12+
813
## 1.6.0 _(2025-06-17)_
914

1015
- <Badge text="Feat" size="small" /> New rule: [`attr-value-no-duplication`](/rules/attr-value-no-duplication/)

website/src/content/docs/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ hero:
1414
variant: minimal
1515
banner:
1616
content: |
17-
v1.6.0 is now available! Check the <a href="/changelog/">changelog</a> to see what's new.
17+
v1.6.1 is now available! Check the <a href="/changelog/">changelog</a> to see what's new.
1818
tableOfContents: false
1919
lastUpdated: false
2020
editUrl: false

0 commit comments

Comments
 (0)