Skip to content

Commit e8ba3cc

Browse files
committed
docs: add Node.js usage guide and normalize section header capitalization.
1 parent 064aa91 commit e8ba3cc

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

docs/src/content/docs/developer/custom-adapters.mdx

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Tabs, TabItem } from '@astrojs/starlight/components';
77

88
This guide explains how to create custom adapters for capo.js and validate them using the built-in test utilities.
99

10-
## Why Custom Adapters?
10+
## Why custom adapters?
1111

1212
Custom adapters allow you to use capo.js with different HTML parsers or AST formats, such as:
1313
- JSX/React elements
@@ -16,7 +16,7 @@ Custom adapters allow you to use capo.js with different HTML parsers or AST form
1616
- Custom XML parsers
1717
- Server-side rendering frameworks
1818

19-
## Creating a Custom Adapter
19+
## Creating a custom adapter
2020

2121
### Step 1: Extend the AdapterInterface
2222

@@ -44,7 +44,7 @@ export class MyCustomAdapter extends AdapterInterface {
4444
}
4545
```
4646
47-
### Step 2: Use Your Adapter
47+
### Step 2: Use your adapter
4848
4949
```javascript
5050
import { MyCustomAdapter } from './my-custom-adapter.js';
@@ -54,7 +54,41 @@ const adapter = new MyCustomAdapter();
5454
const results = analyzeHead(headNode, adapter);
5555
```
5656
57-
## Validating Your Adapter
57+
## Node.js usage
58+
59+
While capo.js is designed for the browser, you can use it in Node.js by pairing it with a DOM simulation library like [jsdom](https://github.com/jsdom/jsdom).
60+
61+
Since jsdom provides a standard DOM API, you can reuse the built-in `BrowserAdapter` instead of writing a custom one:
62+
63+
```javascript
64+
import { JSDOM } from 'jsdom';
65+
import { analyzeHead, BrowserAdapter } from '@rviscomi/capo.js';
66+
67+
const html = `
68+
<!DOCTYPE html>
69+
<html>
70+
<head>
71+
<title>My Page</title>
72+
<meta charset="utf-8">
73+
</head>
74+
<body></body>
75+
</html>
76+
`;
77+
78+
// 1. Create a JSDOM instance
79+
const dom = new JSDOM(html);
80+
const head = dom.window.document.querySelector('head');
81+
82+
// 2. Use the built-in BrowserAdapter
83+
const adapter = new BrowserAdapter();
84+
85+
// 3. Analyze
86+
const results = analyzeHead(head, adapter);
87+
88+
console.log(results.weights);
89+
```
90+
91+
## Validating your adapter
5892
5993
capo.js provides **three levels of validation** for custom adapters:
6094
@@ -75,7 +109,7 @@ validateAdapter(adapter); // Throws if invalid
75109
76110
**When to use:** Quick validation during development.
77111
78-
### Level 2: Programmatic Validation
112+
### Level 2: Programmatic validation
79113
80114
Use the `validateAdapter()` function for explicit validation:
81115
@@ -99,7 +133,7 @@ try {
99133
100134
**When to use:** Integration tests, CI/CD pipelines.
101135
102-
### Level 3: Full Test Suite
136+
### Level 3: Full test suite
103137
104138
Run the comprehensive test suite to validate behavior:
105139
@@ -124,7 +158,7 @@ describe('MyCustomAdapter', () => {
124158
});
125159
```
126160
127-
### Note on Parent Pointers
161+
### Note on parent pointers
128162
129163
Some adapters, like those for ESLint, require parent pointers on nodes to support `getParent()` and `getSiblings()`. If your parser doesn't provide these pointers by default (like `@html-eslint/parser`), you must shim them in your test setup.
130164
@@ -163,15 +197,15 @@ runAdapterTestSuite(MyAdapter, {
163197
**When to use:** Comprehensive validation before release.
164198
165199
166-
## Best Practices
200+
## Best practices
167201
168202
1. **Extend `AdapterInterface`** to get base implementation.
169203
2. **Handle null/undefined** inputs in all methods.
170204
3. **Expect lowercase attribute names** as inputs.
171205
4. **Shim parent pointers** if needed for `getParent`/`getSiblings`.
172206
5. **Run the test suite** to verify compliance.
173207
174-
## API Reference
208+
## API reference
175209
176210
### runAdapterTestSuite(AdapterClass, options)
177211

0 commit comments

Comments
 (0)