diff --git a/README.md b/README.md index c132f03..4010bd4 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,12 @@ See [Releases page](https://github.com/azu/kindle-highlight-to-markdown/releases ## Running tests -Install devDependencies and Run `npm test`: +Add Cookie to `.cookie` file + +and Edit test/node.ts and Run tests + + npm run debug - npm test ## Contributing diff --git a/package.json b/package.json index 3f2ce04..8d467d1 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "src/" ], "scripts": { - "main": "ts-node test/node.ts", + "debug": "ts-node test/node.ts", "build": "tsc -p . && tsc -p ./tsconfig.module.json", "clean": "rimraf lib/ module/", "format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"", diff --git a/src/markdown.ts b/src/markdown.ts index ae66dab..da4e911 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -1,12 +1,19 @@ import { ParseResult } from "./parse"; import { mdImg, mdLink } from "markdown-function"; -export const toMarkdown = (parseResult: ParseResult): string => { +export type ToMarkdownOptions = { + defaultHighlightMessage?: string; +}; +export const toMarkdown = (parseResult: ParseResult, options?: ToMarkdownOptions): string => { const { title, url, coverImageUrl, annotations } = parseResult; + const defaultHighlightMessage = options?.defaultHighlightMessage ?? "**CAN NOT SHOW THE HIGHLIGHT**"; const annotationsBody = annotations .map((annotation) => { const note = annotation.note ? `\n\n${annotation.note}` : ""; - return `> ${annotation.highlight.split("\n").join("\n> ")} + // Some highlight is empty string because can not get the highlight. + return `> ${ + annotation.highlight === "" ? defaultHighlightMessage : annotation.highlight.split("\n").join("\n> ") + } > Location: ${mdLink({ url: annotation.kindleUrl, title: String(annotation.locationNumber), diff --git a/src/parse.ts b/src/parse.ts index 377c28b..62e38d5 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -16,7 +16,7 @@ export type ParseResult = { url: string; annotations: Annotation[]; }; -export const parsePage = (window: Window) => { +export const parsePage = (window: Window): ParseResult => { const pages = window.document.querySelectorAll("#a-page"); const page = pages[pages.length - 1]; // select child #a-page if nested #a-page const title = page.querySelector("h3.kp-notebook-metadata") as HTMLHeadingElement; @@ -30,11 +30,7 @@ export const parsePage = (window: Window) => { assertOk(annotationNodes.length > 0, "annotations not found"); const annotations: Annotation[] = Array.from(annotationNodes) .filter((annotation) => { - return ( - annotation.getAttribute("id") !== "empty-annotations-pane" && - // Sorry, we’re unable to display this type of content. - annotation.querySelector(`[id="highlight"]`) !== null - ); + return annotation.getAttribute("id") !== "empty-annotations-pane"; }) .map((annotation) => { const noteNode = annotation.querySelector(`[id="note"]`) as HTMLSpanElement; @@ -42,17 +38,21 @@ export const parsePage = (window: Window) => { const locationNode = annotation.querySelector(`[id="kp-annotation-location"]`) as HTMLInputElement; assertOk(locationNode, "locationNode is not found"); const highlightNode = annotation.querySelector(`[id="highlight"]`) as HTMLSpanElement; - assertOk(highlightNode, "highlightNode is not found"); + // Kindle disappear the highlight, see https://github.com/azu/kindle-highlight-to-markdown/issues/3 + // Sorry, we’re unable to display this type of content. + const isEmptyHighlight = highlightNode === null; + const highlightText = isEmptyHighlight ? "" : highlightNode.textContent; + assertOk(highlightText !== undefined, "highlightText is not found"); const locationNumber = Number(locationNode.value); return { note: noteValue, locationNumber: locationNumber, - highlight: highlightNode.textContent as string, + highlight: highlightText as string, kindleUrl: `kindle://book?action=open&asin=${asinValue}&location=${locationNumber}` }; }); return { - title: title.textContent?.trim(), + title: title.textContent?.trim() ?? "", coverImageUrl: coverImage.src, asin: asinValue, url: `https://www.amazon.co.jp/dp/${asinValue}`, // TODO: hardcode diff --git a/test/node.ts b/test/node.ts index 0d67a5a..d8d83a1 100644 --- a/test/node.ts +++ b/test/node.ts @@ -1,7 +1,7 @@ import { JSDOM, CookieJar } from "jsdom"; import * as fs from "fs/promises"; import path from "path"; -import { parsePage } from "../src"; +import { parsePage, toMarkdown } from "../src"; (async function () { const cookieString = (await fs.readFile(path.join(__dirname, "../.cookie"), "utf-8")).replace("^Cookie: ", ""); @@ -13,5 +13,7 @@ import { parsePage } from "../src"; const { window } = await JSDOM.fromURL("https://read.amazon.co.jp/notebook?asin=B09RZG8KR1&contentLimitState=&", { cookieJar }); - console.log(parsePage(window as any as Window)); + const message = parsePage(window as any as Window); + console.log(message); + console.log(toMarkdown(message)); })();