Skip to content

Commit

Permalink
feat: underlines to SideNotes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Jun 27, 2023
1 parent fb4762b commit 5196a88
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ __Annotation Types extracted__
- Free Comment ➡️ blockquote of the comment text
- Strikethrough ➡️ Markdown strikethrough
- Rectangle ➡️ image
- Underlines ➡️ sent to [SideNotes](https://www.apptorium.com/sidenotes) (if not
installed, they are ignored)

### Automatic Page Number Identification
Instead of the PDF page numbers, this workflow retrieves information about the *real* page numbers from the BibTeX library and inserts them. If there is no page data in the BibTeX entry (for example, monographies), you are prompted to enter the page number manually.
Expand All @@ -65,6 +67,7 @@ Insert these special codes at the __beginning__ of an annotation to invoke speci
- `? foo` __(free comments)__: Turns "foo" into a [Question Callout](https://help.obsidian.md/How+to/Use+callouts) (`> ![QUESTION]`) and move up. (Callouts are Obsidian-specific Syntax.)
- `##`: Turns highlighted text into a __heading__ that is added at that location. The number of `#` determines the heading level. If the annotation is a free comment, the text following the `#` is used as heading instead. (The space after the is `#` required).
- `=`: Adds highlighted text as __tags__ to the YAML frontmatter (mostly used for Obsidian as output). If the annotation is a free comment, uses the text after the `=`. In both cases, the annotation is removed afterward.
- `_`: A copy of the annotation is sent to [SideNotes](https://www.apptorium.com/sidenotes). If SideNotes is not installed, these annotations are extracted as normal.

### Extracting Images
- The respective images is saved in the `attachments` sub-folder of the output folder, and named `{citekey}_image{n}.png`.
Expand Down
3 changes: 3 additions & 0 deletions cheatsheet-pdf-annotation-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ __Annotation Types extracted__
- Free Comment ➡️ blockquote of the comment text
- Strikethrough ➡️ Markdown strikethrough
- Rectangle ➡️ image
- Underlines ➡️ sent to [SideNotes](https://www.apptorium.com/sidenotes) (if not
installed, they are ignored)

### Automatic Page Number Identification
Instead of the PDF page numbers, this workflow retrieves information about the *real* page numbers from the BibTeX library and inserts them. If there is no page data in the BibTeX entry (for example, monographies), you are prompted to enter the page number manually.
Expand All @@ -19,6 +21,7 @@ Insert these special codes at the __beginning__ of an annotation to invoke speci
- `? foo` __(free comments)__: Turns "foo" into a [Question Callout](https://help.obsidian.md/How+to/Use+callouts) (`> ![QUESTION]`) and move up. (Callouts are Obsidian-specific Syntax.)
- `##`: Turns highlighted text into a __heading__ that is added at that location. The number of `#` determines the heading level. If the annotation is a free comment, the text following the `#` is used as heading instead. (The space after the is `#` required).
- `=`: Adds highlighted text as __tags__ to the YAML frontmatter (mostly used for Obsidian as output). If the annotation is a free comment, uses the text after the `=`. In both cases, the annotation is removed afterward.
- `_`: A copy of the annotation is sent to [SideNotes](https://www.apptorium.com/sidenotes). If SideNotes is not installed, these annotations are extracted as normal.

### Extracting Images
- The respective images is saved in the `attachments` sub-folder of the output folder, and named `{citekey}_image{n}.png`.
Expand Down
45 changes: 31 additions & 14 deletions scripts/process_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,38 @@ function insertPageNumber(annotations, pageNo) {
});
}

/** code: "_" or type "Underline" -> split off and send to SideNotes.app
/** code: "_" or annotation type "Underline" -> split off and send to SideNotes.app
* when SideNotes is not installed, Underlines are ignored and annotations with
* leading "_" are still extracted (though the "_" is removed)
* @param {Annotation[]} annotations
* @param {string} citekey
*/
function splitOffUnderlines(annotations, citekey) {
const underlineAnnos = annotations.filter((a) => a.type === "Underline");
function underlinesToSidenotes(annotations, citekey) {
// sidenotes is installed?
let sidenotesIsInstalled = false;
try {
Application("SideNotes");
sidenotesIsInstalled = true;
} catch (_error) {
sidenotesIsInstalled = false;
}

// Annotations with leading "_"
const underscoreAnnos = [];
annotations.forEach((anno) => {
if (!anno.comment?.startsWith("_")) return;
anno.comment = anno.comment.slice(1).trim(); // remove "_" prefix
underscoreAnnos.push(anno);
});

const annosToSplitOff = [...underlineAnnos, ...underscoreAnnos];
if (annosToSplitOff.length > 0) {
const text = jsonToMd(annosToSplitOff, citekey);
Application("SideNotes").createNote({ text: text });
if (sidenotesIsInstalled) {
const underlineAnnos = annotations.filter((a) => a.type === "Underline");

const annosToSplitOff = [...underlineAnnos, ...underscoreAnnos];
if (annosToSplitOff.length > 0) {
const text = jsonToMd(annosToSplitOff, citekey);
Application("SideNotes").createNote({ text: text });
}
}
return annotations.filter((/** @type {{ type: string; }} */ anno) => anno.type !== "Underline");
}
Expand Down Expand Up @@ -196,7 +210,7 @@ function jsonToMd(annotations, citekey) {
break;
case "Heading":
// ensure no leading line break when heading is first item
if (firstItem) output = "## ";
if (firstItem) output = comment;
else output = "\n" + comment;
break;
case "Question Callout": // blockquoted comment
Expand Down Expand Up @@ -395,11 +409,14 @@ function extractMetadata(citekey, rawEntry) {
if (data.firstPage === -999) {
let response, validInput;
do {
response = app.displayDialog("BibTeX Entry has no page numbers.\n\nEnter true page number of FIRST pdf page:", {
defaultAnswer: "",
buttons: ["OK", "Cancel"],
defaultButton: "OK",
});
response = app.displayDialog(
"BibTeX Entry has no page numbers.\n\nEnter true page number of FIRST pdf page:",
{
defaultAnswer: "",
buttons: ["OK", "Cancel"],
defaultButton: "OK",
},
);
if (response.buttonReturned === "Cancel") return;
validInput = response.textReturned.match(/^-?\d+$/);
} while (!validInput);
Expand Down Expand Up @@ -501,7 +518,7 @@ function run(argv) {

// finish up
if (!usePdfannots) annos = insertImage4pdfannots2json(annos, citekey);
annos = splitOffUnderlines(annos, citekey);
annos = underlinesToSidenotes(annos, citekey);
annos = jsonToMd(annos, citekey);

writeNote(annos, metadata, outPath, tagsForYaml);
Expand Down

0 comments on commit 5196a88

Please sign in to comment.