Skip to content

Commit 808548a

Browse files
committed
refactor: ♻️ improve code typings
add typings for Readwise data
1 parent b86e005 commit 808548a

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

Diff for: src/main.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import spacetime from 'spacetime';
66
import * as YAML from 'yaml';
77

88
import { DEFAULT_SETTINGS, FRONTMATTER_TO_ESCAPE, YAML_TOSTRING_OPTIONS } from 'constants/index';
9-
import { Export, Highlight, Library, Tag } from 'models/readwise';
9+
import { Export, Highlight, Library, Tag, ReadwiseMetadata } from 'models/readwise';
1010
import { PluginSettings } from 'models/settings';
1111
import { YamlStringState } from 'models/yaml';
1212
import ReadwiseApi from 'services/readwise-api';
@@ -32,12 +32,19 @@ export default class ReadwiseMirror extends Plugin {
3232
}
3333

3434
// Before metadata is used
35-
public escapeFrontmatter(metadata: any, fieldsToProcess: Array<string>): any {
35+
public escapeFrontmatter(metadata: ReadwiseMetadata, fieldsToProcess: Array<string>): ReadwiseMetadata {
3636
// Copy the metadata object to avoid modifying the original
37-
const processedMetadata = { ...metadata };
37+
const processedMetadata = { ...metadata } as ReadwiseMetadata;
3838
fieldsToProcess.forEach((field) => {
39-
if (field in processedMetadata && processedMetadata[field] && typeof processedMetadata[field] === 'string') {
40-
processedMetadata[field] = this.escapeYamlValue(processedMetadata[field]);
39+
if (
40+
field in processedMetadata &&
41+
processedMetadata[field as keyof ReadwiseMetadata] &&
42+
typeof processedMetadata[field as keyof ReadwiseMetadata] === 'string'
43+
) {
44+
const key = field as keyof ReadwiseMetadata;
45+
if (typeof processedMetadata[key] === 'string') {
46+
(processedMetadata[key] as unknown) = this.escapeYamlValue(processedMetadata[key] as string);
47+
}
4148
}
4249
});
4350

@@ -236,14 +243,14 @@ export default class ReadwiseMirror extends Plugin {
236243
* - Protection is configured in plugin settings
237244
* - Example protected fields: status, tags, categories
238245
*/
239-
private async writeUpdatedFrontmatter(file: TFile, updates: Record<string, any>): Promise<void> {
246+
private async writeUpdatedFrontmatter(file: TFile, updates: Record<string, unknown>): Promise<void> {
240247
const { frontmatter, body } = await this.updateFrontmatter(file, updates);
241248

242249
// Combine and write back
243250
await this.app.vault.modify(file, `${frontmatter}\n${body}`);
244251
}
245252

246-
private async updateFrontmatter(file: TFile, updates: Record<string, any>) {
253+
private async updateFrontmatter(file: TFile, updates: Record<string, unknown>) {
247254
const content = await this.app.vault.read(file);
248255
const frontmatterRegex = /^(---\n[\s\S]*?\n---)/;
249256
const match = content.match(frontmatterRegex);
@@ -417,7 +424,7 @@ export default class ReadwiseMirror extends Plugin {
417424
? `[[${author}]]`
418425
: ``;
419426

420-
const metadata = {
427+
const metadata: ReadwiseMetadata = {
421428
id: user_book_id,
422429
title: title,
423430
sanitized_title: sanitizedTitle,

Diff for: src/models/readwise.ts

+24
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,27 @@ export interface Library {
4545
books: Exports;
4646
highlightCount: number;
4747
}
48+
49+
export interface ReadwiseMetadata {
50+
id: number; // book id from Readwise API
51+
title: string;
52+
sanitized_title: string;
53+
author: string;
54+
authorStr: string;
55+
document_note: string;
56+
summary: string;
57+
category: string;
58+
num_highlights: number;
59+
created: string;
60+
updated: string;
61+
cover_image_url: string;
62+
highlights_url: string;
63+
highlights: Highlight[];
64+
last_highlight_at: string;
65+
source_url: string;
66+
unique_url: string;
67+
tags: string;
68+
highlight_tags: string;
69+
tags_nohash: string;
70+
hl_tags_nohash: string;
71+
}

Diff for: src/test/sample-data.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// TODO: Base it on Readwise API and not internal metadata
77
//
88

9-
import { Tag } from 'models/readwise';
9+
import { ReadwiseMetadata, Tag } from 'models/readwise';
1010

1111
export const testTags: Tag[] = [
1212
{ id: 1, name: 'important' },
@@ -21,8 +21,8 @@ export const testTags: Tag[] = [
2121
{ id: 10, name: 'nested/path/tag' },
2222
];
2323

24-
export const sampleMetadata = {
25-
id: '12345',
24+
export const sampleMetadata: ReadwiseMetadata = {
25+
id: 12345,
2626
title: "My Book:\nA Subtitle's Journey",
2727
sanitized_title: "My Book - A Subtitle's Journey",
2828
author: 'O\'Reilly, Tim & "Doc" Smith',

0 commit comments

Comments
 (0)