@@ -18,61 +18,91 @@ const pkg = require('../../../package.json')
1818 * @returns Markdown content for the rule or undefined if not found
1919 */
2020function 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- / < B a d g e \s + t e x t = " ( [ ^ " ] + ) " [ ^ > ] * \/ > / 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 - z A - Z ] [ ^ > \s ] * [ ^ > ] * > | < ! D O C T Y P E [ ^ > ] * > ) / 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+ / < B a d g e \s + t e x t = " ( [ ^ " ] + ) " [ ^ > ] * \/ > / 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 - z A - Z ] [ ^ > \s ] * [ ^ > ] * > | < ! D O C T Y P E [ ^ > ] * > ) / g,
91+ '`$1`'
92+ )
93+
94+ // Handle code blocks - ensure they're preserved as is
95+ processedMarkdown = processedMarkdown . replace (
96+ / ` ` ` h t m l \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}
0 commit comments