Extend Readwise Action to support the JSON Notes object

Hi,

I’m looking to extend the Add to Readwise Reader-Action by support for what Readwise calls inline notes and tags. I had already modified it to pass only a selection:

const SOURCE_TYPE = "drafts";
const BASE_URL = "https://readwise.io/api/v2/";

let selectedText;
if (editor.getSelectedText().length > 0) {
  selectedText = editor.getSelectedText();
} else {
  selectedText = draft.content;
}

let selectedBook = selectBook(getEntriesFromReadwise);
if (selectedBook != "cancelled") {
  postToReadwise(selectedText, selectedBook);
}

function selectBook(data) {
  let actionPrompt = new Prompt();
  actionPrompt.isCancellable = true;

  entries = getEntriesFromReadwise();
  entries.forEach(function (bookTitle) {
    actionPrompt.addButton(bookTitle);
  });

  actionPrompt.addButton("New Book");

  let userSelectedAButton = actionPrompt.show();
  let selectedBook = "userCancelled";

  if (userSelectedAButton) {
    selectedBook = actionPrompt.buttonPressed;
  } else {
    app.displayInfoMessage("Prompt was cancelled.");
    context.cancel();
    return "cancelled";
  }

  if (selectedBook == "New Book") {
    let newBookPrompt = new Prompt();
    newBookPrompt.addTextField("bookName", "New Book", "Name");
    newBookPrompt.addButton("OK");
    let userSelectedAButtonNewBook = newBookPrompt.show();

    if (userSelectedAButtonNewBook) {
      selectedBook = newBookPrompt.fieldValues["bookName"];
    } else {
      app.displayInfoMessage("Prompt was cancelled.");
      context.cancel();
      return "cancelled";
    }
  }
  return selectedBook;
}

function getEntriesFromReadwise() {
  const GET_URL = BASE_URL + "books/?source=" + SOURCE_TYPE + "&page_size=5";

  let credReadwise = Credential.create(
    "Readwise",
    "Highlight surfacing service."
  );
  credReadwise.addPasswordField("token", "API Token");
  credReadwise.authorize();

  let httpMain = HTTP.create();
  let respMain = httpMain.request({
    url: GET_URL,
    method: "GET",
    headers: {
      Authorization: `Token ${credReadwise.getValue("token")}`,
    },
  });

  let entries = [];

  if (respMain.statusCode == 200) {
    let responseText = respMain.responseText;
    let responseData = JSON.parse(responseText);
    let bookResults = responseData.results;

    bookResults.forEach(function (book) {
      entries.push(book.title);
    });

    entries.push("Quotes");
  } else {
    console.log("Error in getting books");
  }

  return entries;
}

function postToReadwise(selectedText, selectedBook) {
  const POST_URL = BASE_URL + "highlights/";

  let credReadwise = Credential.create(
    "Readwise",
    "Highlight surfacing service."
  );
  credReadwise.addPasswordField("token", "API Token");
  credReadwise.authorize();

  if (selectedBook) {
    let httpMain = HTTP.create();
    let respMain = httpMain.request({
      url: POST_URL,
      method: "POST",
      data: {
        highlights: [
          {
            text: selectedText,
            title: selectedBook.toString(),
            source_type: SOURCE_TYPE,
          },
        ],
      },
      headers: {
        Authorization: `Token ${credReadwise.getValue("token")}`,
      },
    });

    if (respMain.success) {
      return true;
    } else {
      console.log(`[${respMain.statusCode}] ${respMain.error}`);
      return false;
    }
  } else {
    // Nothing to do
  }
}

In Readwise, notes and tags can be appended to notes, with the last line +1 featuring any tags with a leading dot and the last line +2 featuring any notes, .e.g

Actual Readwise Note
.tag1 .tag2
Note to the Readwise Note

While tags and notes can provided inline with most other Readwise integrations and interfaces, this was split up in the API into a separate parameter of the JSON object called note, which I could add to the existing script under line 112.

So I’m looking to do two specific things:

  1. Import inline notes & tags
  2. Append any Drafts tags

For 1), I will need to check
A) if the last line has a leading dot (which would mean these are the tags, and there are no following notes) then pass that line in the note parameter.
B) If the last line -1 has a leading dot (which would mean these are the tags, and there are following notes in the last line), then pass both lines in the note parameter.

For 2) I would need to to take any Drafts tag, without a leading hash, transfrom it to have a leading dot, and append it to the last line or the last line -1

Could someone help me come up with the Javascript code for that?

Thanks