-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-node.mjs
More file actions
144 lines (120 loc) · 4.98 KB
/
index-node.mjs
File metadata and controls
144 lines (120 loc) · 4.98 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
141
142
143
144
/**
* Node.js version of schema-elements with JSDOM support
*/
import { JSDOM } from 'jsdom';
import { readFileSync, writeFileSync, unlinkSync } from 'fs';
import { fileURLToPath, pathToFileURL } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Global DOM for library initialization
const globalDom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
url: 'http://localhost',
pretendToBeVisual: true,
resources: 'usable'
});
// Set up globals
const { window } = globalDom;
global.window = window;
global.document = window.document;
global.Document = window.Document;
global.Element = window.Element;
global.HTMLElement = window.HTMLElement;
global.HTMLTemplateElement = window.HTMLTemplateElement;
global.HTMLFormElement = window.HTMLFormElement;
global.HTMLBodyElement = window.HTMLBodyElement;
global.Node = window.Node;
global.NodeList = window.NodeList;
global.HTMLCollection = window.HTMLCollection;
global.Event = window.Event;
global.CustomEvent = window.CustomEvent;
global.MutationObserver = window.MutationObserver;
global.DOMParser = window.DOMParser;
global.NodeFilter = window.NodeFilter;
// Read and modify the main library file
const libPath = join(__dirname, 'index.mjs');
let libContent = readFileSync(libPath, 'utf8');
// Replace the HTTP import with local import
libContent = libContent.replace(
`import { HTMLTemplate } from 'https://jamesaduncan.github.io/html-template/index.mjs';`,
`import { HTMLTemplate } from './lib/html-template.mjs';`
);
// Write the modified content and import
const tempPath = join(__dirname, `index-node-temp-${Date.now()}.mjs`);
writeFileSync(tempPath, libContent);
const moduleUrl = pathToFileURL(tempPath).href;
const { Microdata, Schema, Template, MicrodataItem, MicrodataCollection } = await import(moduleUrl);
// Clean up temp file
unlinkSync(tempPath);
// Store reference to the JSDOM constructors that have microdata functionality
const MicrodataHTMLElement = global.HTMLElement;
const MicrodataDocument = global.Document;
// Helper for parsing HTML in Node.js
export function parseHTML(html) {
// Create new JSDOM but copy the microdata-enhanced prototypes
const newDom = new JSDOM(html, {
url: 'http://localhost',
pretendToBeVisual: true,
resources: 'usable'
});
const newDoc = newDom.window.document;
// Copy microdata property from the initialized prototype
const microdataDescriptor = Object.getOwnPropertyDescriptor(MicrodataHTMLElement.prototype, 'microdata');
if (microdataDescriptor) {
Object.defineProperty(newDom.window.HTMLElement.prototype, 'microdata', microdataDescriptor);
}
// Also copy to Element prototype if it exists there
const elementMicrodataDescriptor = Object.getOwnPropertyDescriptor(global.Element.prototype, 'microdata');
if (elementMicrodataDescriptor) {
Object.defineProperty(newDom.window.Element.prototype, 'microdata', elementMicrodataDescriptor);
}
// Copy document microdata property
const docMicrodataDescriptor = Object.getOwnPropertyDescriptor(MicrodataDocument.prototype, 'microdata');
if (docMicrodataDescriptor) {
Object.defineProperty(newDom.window.Document.prototype, 'microdata', docMicrodataDescriptor);
}
// Also copy body microdata if it exists
const bodyMicrodataDescriptor = Object.getOwnPropertyDescriptor(global.HTMLBodyElement?.prototype || {}, 'microdata');
if (bodyMicrodataDescriptor && newDom.window.HTMLBodyElement) {
Object.defineProperty(newDom.window.HTMLBodyElement.prototype, 'microdata', bodyMicrodataDescriptor);
}
// Create a custom microdata collection for this document
// Cache the collection to avoid recreating it
let cachedCollection = null;
Object.defineProperty(newDoc, 'microdata', {
get() {
if (cachedCollection) return cachedCollection;
// Create a simple array-like object with named access
const items = Array.from(this.querySelectorAll('[itemscope]:not([itemprop])'))
.map(el => el.microdata)
.filter(item => item != null);
// Create collection object
cachedCollection = Object.assign(items, {
// Add named access for elements with IDs
...items.reduce((acc, item, index) => {
const element = this.querySelectorAll('[itemscope]:not([itemprop])')[index];
if (element.id) {
acc[element.id] = item;
}
return acc;
}, {})
});
return cachedCollection;
},
configurable: true
});
return newDoc;
}
// Export main APIs
const dom = globalDom;
const document = global.document;
export {
Microdata,
Schema,
Template,
MicrodataItem,
MicrodataCollection,
dom,
window,
document
};