Skip to content

Commit 4f8f91e

Browse files
docs: fix TypeScript errors in README.md examples
1 parent fbf8fa2 commit 4f8f91e

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@ UNOFFICIAL standard module for Scrapbox UserScript
77

88
## Getting Started
99

10-
This library serves as an unofficial standard library for developing Scrapbox userscripts. It provides a comprehensive set of utilities for interacting with Scrapbox's features, including REST API operations, browser interactions, and common utilities.
10+
This library serves as an unofficial standard library for developing Scrapbox
11+
userscripts. It provides a comprehensive set of utilities for interacting with
12+
Scrapbox's features, including REST API operations, browser interactions, and
13+
common utilities.
1114

1215
### Installation
1316

14-
1. Bundler Configuration
15-
This library is distributed through JSR (JavaScript Registry) and requires a bundler configuration. Follow these steps:
17+
1. Bundler Configuration This library is distributed through JSR (JavaScript
18+
Registry) and requires a bundler configuration. Follow these steps:
1619

1720
a. Configure your bundler to use JSR:
21+
1822
- For esbuild: Add JSR to your import map
1923
- For other bundlers: Refer to your bundler's JSR integration documentation
2024

2125
b. Import the library:
26+
2227
```typescript
23-
// Import the entire library
24-
import { ... } from "jsr:@cosense/std";
28+
// Import commonly used functions
29+
import { getPage } from "jsr:@cosense/std/rest";
30+
import { parseAbsoluteLink } from "jsr:@cosense/std";
2531

26-
// Or import specific modules (recommended)
27-
import { getPage, getLines } from "jsr:@cosense/std/rest";
28-
import { getLines, press } from "jsr:@cosense/std/browser/dom";
32+
// Import specific modules (recommended)
33+
import { getLinks } from "jsr:@cosense/std/rest";
34+
import { press } from "jsr:@cosense/std/browser/dom";
35+
import { getLines } from "jsr:@cosense/std/browser/dom";
2936
```
3037

31-
2. Module Organization
32-
The library is organized into the following main modules:
38+
2. Module Organization The library is organized into the following main modules:
3339

3440
- `rest/`: API operations for Scrapbox REST endpoints
3541
- Page operations
@@ -49,60 +55,81 @@ The library is organized into the following main modules:
4955
### Basic Usage
5056

5157
1. Retrieving Page Information
58+
5259
```typescript
5360
// Get page content and metadata
5461
import { getPage } from "jsr:@cosense/std/rest";
5562

56-
const page = await getPage("projectName", "pageName");
57-
console.log(page.title); // Access page title
58-
console.log(page.lines); // Access page content as lines
59-
console.log(page.descriptions); // Access page descriptions
63+
const result = await getPage("projectName", "pageName");
64+
if (result.ok) {
65+
const page = result.val;
66+
console.log("Page title:", page.title);
67+
console.log("Page content:", page.lines.map((line) => line.text));
68+
console.log("Page descriptions:", page.descriptions.join("\n"));
69+
}
6070
```
6171

6272
2. DOM Operations
73+
6374
```typescript
6475
// Interact with the current page's content
6576
import { getLines, press } from "jsr:@cosense/std/browser/dom";
6677

6778
// Get all lines from the current page
6879
const lines = getLines();
69-
console.log(lines.map(line => line.text));
80+
console.log(lines.map((line) => line.text));
7081

7182
// Simulate keyboard input
7283
await press("Enter"); // Add a new line
73-
await press("Tab"); // Indent the line
84+
await press("Tab"); // Indent the line
7485
```
7586

7687
3. External Link Analysis
88+
7789
```typescript
7890
// Parse external links (YouTube, Spotify, etc.)
7991
import { parseAbsoluteLink } from "jsr:@cosense/std";
8092
import type { LinkNode } from "@progfay/scrapbox-parser";
8193

82-
const link: LinkNode = {
83-
type: "link",
84-
pathType: "absolute",
94+
// Create a link node with absolute path type
95+
const link = {
96+
type: "link" as const,
97+
pathType: "absolute" as const,
8598
href: "https://www.youtube.com/watch?v=xxxxx",
86-
content: ""
87-
};
99+
content: "",
100+
raw: "[https://www.youtube.com/watch?v=xxxxx]",
101+
} satisfies LinkNode & { pathType: "absolute" };
88102

89103
// Parse and handle different link types
90104
const parsed = parseAbsoluteLink(link);
91-
if (parsed.type === "youtube") {
92-
console.log(parsed.videoId); // YouTube video ID
93-
console.log(parsed.timestamp); // Video timestamp (if present)
94-
} else if (parsed.type === "spotify") {
95-
console.log(parsed.trackId); // Spotify track ID
105+
if (parsed?.type === "youtube") {
106+
// Handle YouTube links
107+
console.log("YouTube video ID:", parsed.href.split("v=")[1]);
108+
const params = new URLSearchParams(parsed.href.split("?")[1]);
109+
const start = params.get("t");
110+
if (start) {
111+
console.log("Video timestamp:", start);
112+
}
113+
} else if (parsed?.type === "spotify") {
114+
// Handle Spotify links
115+
const match = parsed.href.match(/spotify\.com\/track\/([^?]+)/);
116+
if (match) {
117+
console.log("Spotify track ID:", match[1]);
118+
}
96119
}
97120
```
98121

99122
### Important Notes
123+
100124
- This library requires a bundler for use in userscripts
101125
- Full TypeScript support with type definitions included
102126
- Comprehensive error handling with type-safe responses
103-
- For more examples and use cases, see the [Examples](https://github.com/takker99/scrapbox-userscript-std/tree/main/examples) directory
127+
- For more examples and use cases, see the
128+
[Examples](https://github.com/takker99/scrapbox-userscript-std/tree/main/examples)
129+
directory
104130

105131
### Additional Resources
132+
106133
- [JSR Package Page](https://jsr.io/@cosense/std)
107134
- [API Documentation](https://jsr.io/@cosense/std/doc)
108135
- [GitHub Repository](https://github.com/takker99/scrapbox-userscript-std)

0 commit comments

Comments
 (0)