Repository navigation
Expand file tree
/
Copy pathwcag.js
More file actions
140 lines (125 loc) · 4.37 KB
/
Copy pathwcag.js
File metadata and controls
140 lines (125 loc) · 4.37 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Plugin } from './plugin.js';
import * as cheerio from 'cheerio';
import * as fs from 'fs';
import * as path from 'path';
/**
* The WCAG plugin adds a new markdown tag which can render sections of WCAG.
* For example, the `[wcag:status-messages]` markdown will render the `status-messages` section.
*/
export class WcagPlugin extends Plugin {
constructor(tag, url, html) {
super(tag);
this.url = url;
this.html = html;
this.$ = cheerio.load(html);
}
/**
* Initializes the WcagPlugin by fetching HTML from a local file derived from the provided URL.
* @param {string} tag - The tag used in markdown (e.g., `wcag` or `wcag2ict`).
* @param {string} url - The URL to derive filename from (e.g., `https://www.w3.org/TR/2024/REC-WCAG22-20241212/`).
* @returns {Promise<function>} - An initialized `WcagPlugin` function.
*/
static async init(tag, url) {
const html = await WcagPlugin.fetchHTML(url);
return new WcagPlugin(tag, url, html).plugin();
}
/**
* Loads HTML from a local file derived from the URL.
* @param {string} url - The URL to derive filename from.
* @returns {Promise<string>} - The HTML content.
*/
static async fetchHTML(url) {
const filename = new URL(url).pathname.split('/').filter(Boolean).pop();
const directory = path.dirname(new URL(import.meta.url).pathname);
const filePath = path.join(directory, `${filename}.html`);
console.log(`Loading HTML from: ${filePath}`);
try {
const resolvedPath = path.resolve(filePath);
if (!fs.existsSync(resolvedPath)) {
throw new Error(`File not found: ${resolvedPath}`);
}
const html = await fs.promises.readFile(resolvedPath, 'utf-8');
return html;
} catch (error) {
console.error(`Error loading ${filePath}:`, error);
throw new Error(`Error loading content from ${filePath}: ${error.message}`);
}
}
/**
* Defines the regex for matching `[<tag>:<identifier>]`.
* @returns {RegExp} - The regex for matching the note syntax.
*/
regex() {
return new RegExp(`\\[${this.tag}:([a-zA-Z0-9-_\\.]+)\\]`);
}
/**
* Processes the matched content and populates the token with id, link, header and content.
* @param {Array} match - The regex match result.
* @param {object} token - The token to populate.
*/
process(match, token) {
const id = match[1];
const section = this.extract(id);
token.id = id;
token.link = `${this.url}#${id}`;
token.header = section.header;
token.content = section.content;
}
/**
* Renders the token into HTML with an expand/collapse component.
* @param {object} token - The token to render.
* @returns {string} - The rendered HTML string.
*/
render(token) {
// Add the tag as a prefix to avoid id conflicts
const $content = cheerio.load(token.content, null, false);
$content('[id]').each((_, element) => {
const currentId = $content(element).attr('id');
$content(element).attr('id', `${this.tag}-${currentId}`);
});
token.content = $content.html();
return `
<details class="wcag">
<summary>
<strong>${this.tag.toUpperCase()}:</strong> ${token.header}
</summary>
<blockquote cite="${token.link}">
<div>${token.content}</div>
</blockquote>
<footer>
<cite>
<a href="${token.link}" target="_blank">${token.link}</a>
</cite>
</footer>
</details>
`;
}
/**
* Extracts a section by ID from the loaded HTML.
* @param {string} id - The identifier for the section.
* @returns {object} - An object containing the `header` and `content` of the section.
*/
extract(id) {
const $section = this.$(`#${id}`);
if (!$section.length) {
throw new Error(`Section not found: ${id}`);
}
const header = $section.find('h1, h2, h3, h4, h5, h6').first().text();
$section.find('.header-wrapper, .doclinks, .conformance-level').remove();
$section.find('a').each((_, anchor) => {
const href = this.$(anchor).attr('href');
if (href) {
this.$(anchor).attr('target', '_blank');
// Skip if already absolute URL
if (!/^[\w]+:\/\//.test(href)) {
const fullUrl = new URL(href, this.url).href;
this.$(anchor).attr('href', fullUrl);
}
}
});
return {
header,
content: $section.html(),
};
}
}