Skip to content

Commit

Permalink
fix: improve caption handling
Browse files Browse the repository at this point in the history
(but still just goes to alt, which isn't useful)
  • Loading branch information
hatton committed Jul 26, 2022
1 parent 295c02a commit cb19125
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/NotionImage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ test("primary caption content after image links are removed", async () => {
const img = parseImageBlock(
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText
);
expect(img.caption).toBe("Caption before images.\nCaption after images.");
// carriage returns seem to mess up the markdown, so should be removed
expect(img.caption).toBe("Caption before images. Caption after images.");
});

test("gets localized image links", async () => {
const img = parseImageBlock(
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText
);
expect(img.localizedUrls.length).toBe(2);
expect(img.localizedUrls[0].iso632Code).toBe("FR");
expect(img.localizedUrls[1].iso632Code).toBe("ES");
expect(img.localizedUrls[0].iso632Code).toBe("fr");
expect(img.localizedUrls[1].iso632Code).toBe("es");
expect(img.localizedUrls[0].url).toBe("https://i.imgur.com/pYmE7OJ.png");
expect(img.localizedUrls[1].url).toBe("https://i.imgur.com/8paSZ0i.png");
});
Expand Down
24 changes: 20 additions & 4 deletions src/NotionImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ export function parseImageBlock(b: any): ImageSet {
const match = /\s*(..)\s*(https:\/\/.*)/.exec(l);
if (match) {
imageSet.localizedUrls.push({
iso632Code: match[1].toUpperCase(),
iso632Code: match[1].toLowerCase(),
url: match[2],
});
} else {
imageSet.caption += l + "\n";
// NB: carriage returns seem to mess up the markdown, so should be removed
imageSet.caption += l + " ";
}
});
// NB: currently notion-md puts the caption in Alt, which noone sees (unless the image isn't found)
// We could inject a custom element handler to emit a <figure> in order to show the caption.
imageSet.caption = imageSet.caption?.trim();
//console.log(JSON.stringify(imageSet, null, 2));

Expand All @@ -141,16 +144,29 @@ export function parseImageBlock(b: any): ImageSet {
// change the src to point to our copy of the image.
export async function processImageBlock(b: any): Promise<void> {
//console.log(JSON.stringify(b));
const img = parseImageBlock(b);
const imageSet = parseImageBlock(b);

const newPath = imagePrefix + "/" + (await saveImage(img, imageOutputPath));
const newPath =
imagePrefix + "/" + (await saveImage(imageSet, imageOutputPath));

// change the src to point to our copy of the image
if ("file" in b.image) {
b.image.file.url = newPath;
} else {
b.image.external.url = newPath;
}
// put back the simplified caption, stripped of the meta information
if (imageSet.caption) {
b.image.caption = [
{
type: "text",
text: { content: imageSet.caption, link: null },
plain_text: imageSet.caption,
},
];
} else {
b.image.caption = [];
}
}

function imageWasSeen(path: string) {
Expand Down

0 comments on commit cb19125

Please sign in to comment.