paper-manager 0.10.2 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,18 +26,13 @@ export async function convertPdfToMarkdown(pdfPath) {
26
26
  format: "markdown",
27
27
  quiet: true,
28
28
  });
29
- const files = readdirSync(outDir);
30
- const mdFile = files.find((f) => f.endsWith(".md"));
29
+ const mdFile = readdirSync(outDir).find((f) => f.endsWith(".md"));
31
30
  if (!mdFile)
32
31
  return null;
33
32
  const markdown = readFileSync(path.join(outDir, mdFile), "utf-8");
34
33
  const imageExtensions = new Set([".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".bmp"]);
35
34
  const images = new Map();
36
- for (const file of files) {
37
- if (imageExtensions.has(path.extname(file).toLowerCase())) {
38
- images.set(file, readFileSync(path.join(outDir, file)));
39
- }
40
- }
35
+ collectImages(outDir, outDir, imageExtensions, images);
41
36
  return { markdown, images };
42
37
  }
43
38
  catch {
@@ -57,10 +52,11 @@ export function saveConvertResult(filesDir, id, result) {
57
52
  if (result.images.size > 0) {
58
53
  const imageSubDir = path.join(filesDir, id);
59
54
  mkdirSync(imageSubDir, { recursive: true });
60
- for (const [filename, data] of result.images) {
61
- writeFileSync(path.join(imageSubDir, filename), data);
62
- // Rewrite image/link references: ](filename) → ](<id>/filename)
63
- markdown = markdown.replaceAll(`](${filename})`, `](${id}/${filename})`);
55
+ for (const [relPath, data] of result.images) {
56
+ const basename = path.basename(relPath);
57
+ writeFileSync(path.join(imageSubDir, basename), data);
58
+ // Rewrite image/link references: ](old/path.png) ](<id>/basename.png)
59
+ markdown = markdown.replaceAll(`](${relPath})`, `](${id}/${basename})`);
64
60
  }
65
61
  }
66
62
  writeFileSync(path.join(filesDir, `${id}.md`), markdown, "utf-8");
@@ -75,6 +71,18 @@ export function removeImageDir(filesDir, id) {
75
71
  }
76
72
  }
77
73
  // ─── Internal ────────────────────────────────────────────
74
+ /** Recursively collect image files under `dir`, keyed by path relative to `root`. */
75
+ function collectImages(dir, root, extensions, out) {
76
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
77
+ const full = path.join(dir, entry.name);
78
+ if (entry.isDirectory()) {
79
+ collectImages(full, root, extensions, out);
80
+ }
81
+ else if (extensions.has(path.extname(entry.name).toLowerCase())) {
82
+ out.set(path.relative(root, full), readFileSync(full));
83
+ }
84
+ }
85
+ }
78
86
  let cachedAvailability;
79
87
  async function detectAvailability() {
80
88
  const [hasPackage, hasJava] = await Promise.all([checkPackage(), checkJava()]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "paper-manager",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "A paper management system.",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/EurFelux/paper-manager",