-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
104 lines (88 loc) · 3.64 KB
/
Copy pathmain.js
File metadata and controls
104 lines (88 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Alt Text Warning - Copyright (C) 2026 InPoint Automation Sp. z o.o.
// Licensed under the GNU General Public License v3 or later; see LICENSE.
//
// Flags images missing alt text in preview, fills fallback alt in production.
class AltTextWarning {
constructor(API, name, config) {
this.API = API;
this.name = name;
this.config = config;
}
addModifiers() {
this.API.addModifier('htmlOutput', this.processPage, 3, this);
}
addInsertions() {
this.API.addInsertion('customHeadCode', this.addStyles, 1, this);
}
processPage(rendererInstance, html, globalContext, context) {
const scope = this.config.applyTo || 'both';
const isPost = !!(context && context.post);
const isPage = !!(context && context.page);
if (scope === 'post' && !isPost) return html;
if (scope === 'page' && !isPage) return html;
if (!isPost && !isPage) return html;
const isPreview = rendererInstance.previewMode || false;
const titleMatch = html.match(/<title>([^<]*)<\/title>/i);
const title = titleMatch ? titleMatch[1].trim() : 'Untitled';
const fallbackPattern = this.config.fallbackPattern || '{title} image';
const fallbackAlt = fallbackPattern.replace('{title}', title);
const escapedAlt = fallbackAlt.replace(/"/g, '"');
// skip logo/nav/footer images
const mainStart = html.indexOf('<main');
const mainEnd = html.indexOf('</main>');
if (mainStart === -1 || mainEnd === -1) return html;
const mainTagEnd = html.indexOf('>', mainStart) + 1;
const beforeMain = html.slice(0, mainTagEnd);
const mainContent = html.slice(mainTagEnd, mainEnd);
const afterMain = html.slice(mainEnd);
const processed = mainContent.replace(/<img\s([^>]*)>/gi, function (match, attrs) {
const altMatch = attrs.match(/\balt="([^"]*)"/);
const hasAlt = altMatch && altMatch[1].trim().length > 0;
if (hasAlt) return match;
if (isPreview) {
return '<span class="alt-warning">'
+ '<span class="alt-warning__label">\u26A0 Missing alt text</span>'
+ match
+ '</span>';
} else {
if (altMatch) {
return match.replace(/\balt=""/, 'alt="' + escapedAlt + '"');
} else {
return match.replace(/<img\s/, '<img alt="' + escapedAlt + '" ');
}
}
});
return beforeMain + processed + afterMain;
}
// preview warning CSS
addStyles(rendererInstance, context) {
if (!rendererInstance.previewMode) return '';
if (!context.post && !context.page) return '';
return '<style>'
+ '.alt-warning {'
+ ' position: relative;'
+ ' display: inline-block;'
+ '}'
+ '.alt-warning img {'
+ ' outline: 3px solid #ef4444 !important;'
+ ' outline-offset: -3px;'
+ '}'
+ '.alt-warning__label {'
+ ' position: absolute;'
+ ' top: 8px;'
+ ' left: 8px;'
+ ' z-index: 10;'
+ ' background: #ef4444;'
+ ' color: #fff !important;'
+ ' font-size: 12px;'
+ ' font-weight: bold;'
+ ' padding: 3px 8px;'
+ ' border-radius: 3px;'
+ ' font-family: system-ui, sans-serif;'
+ ' pointer-events: none;'
+ ' line-height: 1.3;'
+ '}'
+ '</style>';
}
}
module.exports = AltTextWarning;