@@ -26,9 +26,41 @@ class MarkdownLintError extends Error {
26
26
}
27
27
}
28
28
29
+ function useBadgeLinterEffect ( ) {
30
+ const { frontMatter, metadata } = useDoc ( ) ;
31
+ const { hide_title } = frontMatter ;
32
+ const { id } = metadata ;
33
+
34
+ useEffect ( ( ) => {
35
+ if ( hide_title ) {
36
+ console . debug ( `Skipping badge linting for ${ id } because \`hide_title\` is set` ) ;
37
+ return ;
38
+ }
39
+
40
+ const invalidBadges = document . querySelectorAll ( `.theme-doc-markdown > header + .badge,
41
+ .theme-doc-markdown .markdown > .badge
42
+ ` ) ;
43
+
44
+ const badgeCount = invalidBadges . length ;
45
+
46
+ if ( ! badgeCount ) return ;
47
+
48
+ const badgeContent = Array . from ( invalidBadges , ( badge ) => `"${ badge . textContent } "` ) ;
49
+
50
+ const message = `${ id } : ${ badgeCount } Badge(s) defined in Markdown content instead of the frontmatter:\n ${ badgeContent . join ( "\n" ) } ` ;
51
+
52
+ console . error ( message ) ;
53
+
54
+ console . error ( `Found ${ badgeCount } invalid badges on ${ id } ` , invalidBadges ) ;
55
+
56
+ throw new MarkdownLintError ( message ) ;
57
+ } , [ hide_title , id ] ) ;
58
+ }
59
+
29
60
const DocItemContent : React . FC < Props > = ( { children } ) => {
30
61
const syntheticTitle = useSyntheticTitle ( ) ;
31
62
const { frontMatter, metadata, contentTitle } = useDoc ( ) ;
63
+ const { id } = metadata ;
32
64
const {
33
65
// ---
34
66
support_level,
@@ -37,6 +69,8 @@ const DocItemContent: React.FC<Props> = ({ children }) => {
37
69
authentik_preview,
38
70
} = frontMatter ;
39
71
72
+ useBadgeLinterEffect ( ) ;
73
+
40
74
const badges : JSX . Element [ ] = [ ] ;
41
75
42
76
if ( authentik_version ) {
@@ -57,46 +91,23 @@ const DocItemContent: React.FC<Props> = ({ children }) => {
57
91
58
92
if ( badges . length && ! syntheticTitle ) {
59
93
throw new MarkdownLintError (
60
- `${ metadata . id } : ${ badges . length } Badge(s) found with a missing synthetic title. Remove the page heading and set it via the frontmatter.` ,
94
+ `${ id } : ${ badges . length } Badge(s) found with a missing synthetic title. Remove the page heading and set it via the frontmatter.` ,
61
95
) ;
62
96
}
63
97
64
98
if ( frontMatter . title && contentTitle && frontMatter . title === contentTitle ) {
65
99
throw new MarkdownLintError (
66
- `${ metadata . id } : Synthetic title "${ frontMatter . title } " and content title "${ contentTitle } " are the same. Remove the first heading and let the frontmatter set the title.` ,
100
+ `${ id } : Synthetic title "${ frontMatter . title } " and content title "${ contentTitle } " are the same. Remove the first heading and let the frontmatter set the title.` ,
67
101
) ;
68
102
}
69
103
70
- useEffect ( ( ) => {
71
- const invalidBadges = document . querySelectorAll ( `.theme-doc-markdown > header + .badge,
72
- .theme-doc-markdown .markdown > .badge
73
- ` ) ;
74
-
75
- if ( ! invalidBadges . length ) return ;
76
-
77
- console . error (
78
- `Found ${ invalidBadges . length } invalid badges on ${ metadata . id } ` ,
79
- invalidBadges ,
80
- ) ;
81
-
82
- throw new MarkdownLintError (
83
- `${ metadata . id } : ${ invalidBadges . length } Badge(s) defined in markdown content instead of the frontmatter.` ,
84
- ) ;
85
- } , [ metadata . id ] ) ;
86
-
87
104
return (
88
105
< div className = { clsx ( ThemeClassNames . docs . docMarkdown , "markdown" ) } >
89
106
{ syntheticTitle ? (
90
107
< header >
91
108
< Heading as = "h1" > { syntheticTitle } </ Heading >
92
109
93
- { badges . length ? (
94
- < p className = "badge-group" >
95
- { badges . map ( ( badge , index ) => (
96
- < React . Fragment key = { index } > { badge } </ React . Fragment >
97
- ) ) }
98
- </ p >
99
- ) : null }
110
+ < BadgeGroup badges = { badges } />
100
111
</ header >
101
112
) : null }
102
113
@@ -105,4 +116,20 @@ const DocItemContent: React.FC<Props> = ({ children }) => {
105
116
) ;
106
117
} ;
107
118
119
+ interface BadgesProps {
120
+ badges : JSX . Element [ ] ;
121
+ }
122
+
123
+ const BadgeGroup : React . FC < BadgesProps > = ( { badges } ) => {
124
+ if ( ! badges . length ) return null ;
125
+
126
+ return (
127
+ < p className = "badge-group" >
128
+ { badges . map ( ( badge , index ) => (
129
+ < React . Fragment key = { index } > { badge } </ React . Fragment >
130
+ ) ) }
131
+ </ p >
132
+ ) ;
133
+ } ;
134
+
108
135
export default DocItemContent ;
0 commit comments