Skip to content

Commit

Permalink
feat: add defaultHighlightMessage option
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Nov 29, 2022
1 parent 41d85a8 commit eaaa9c4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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}\"",
Expand Down
11 changes: 9 additions & 2 deletions 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),
Expand Down
18 changes: 9 additions & 9 deletions src/parse.ts
Expand Up @@ -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<HTMLDivElement>("#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;
Expand All @@ -30,29 +30,29 @@ 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;
const noteValue = noteNode && noteNode.textContent ? noteNode.textContent : undefined;
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
Expand Down
6 changes: 4 additions & 2 deletions 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: ", "");
Expand All @@ -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));
})();

0 comments on commit eaaa9c4

Please sign in to comment.