From 73bf695cf31652793d1675fca8b5aa993288040b Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 2 Aug 2022 20:06:16 +0800 Subject: [PATCH] Add function to get highlight start location --- packages/api/package.json | 2 ++ packages/api/src/services/highlights.ts | 7 ++++++ packages/api/test/services/highlights.test.ts | 25 +++++++++++++++++++ .../db/migrations/0090.do.integrations.sql | 2 +- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 packages/api/src/services/highlights.ts create mode 100644 packages/api/test/services/highlights.test.ts diff --git a/packages/api/package.json b/packages/api/package.json index 258ae3310..a52f999aa 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -46,6 +46,7 @@ "cookie-parser": "^1.4.5", "cors": "^2.8.5", "dataloader": "^2.0.0", + "diff-match-patch": "^1.0.5", "dompurify": "^2.0.17", "dot-case": "^3.0.4", "dotenv": "^8.2.0", @@ -96,6 +97,7 @@ "@types/chai-string": "^1.4.2", "@types/cookie": "^0.4.0", "@types/cookie-parser": "^1.4.2", + "@types/diff-match-patch": "^1.0.32", "@types/dompurify": "^2.0.4", "@types/express": "^4.17.7", "@types/graphql-fields": "^1.3.4", diff --git a/packages/api/src/services/highlights.ts b/packages/api/src/services/highlights.ts new file mode 100644 index 000000000..62a514fc4 --- /dev/null +++ b/packages/api/src/services/highlights.ts @@ -0,0 +1,7 @@ +import { diff_match_patch } from 'diff-match-patch' + +export const getHighlightLocation = (patch: string): number | undefined => { + const dmp = new diff_match_patch() + const patches = dmp.patch_fromText(patch) + return patches[0].start1 || undefined +} diff --git a/packages/api/test/services/highlights.test.ts b/packages/api/test/services/highlights.test.ts new file mode 100644 index 000000000..1a6a7221e --- /dev/null +++ b/packages/api/test/services/highlights.test.ts @@ -0,0 +1,25 @@ +import 'mocha' +import { expect } from 'chai' +import { getHighlightLocation } from '../../src/services/highlights' + +describe('getHighlightLocation', () => { + let patch: string + let location: number + + before(async () => { + location = 109 + patch = `@@ -${location + 1},16 +${location + 1},36 @@ + . We're ++%3Comnivore_highlight%3E + humbled +@@ -254,16 +254,37 @@ + h in the ++%3C/omnivore_highlight%3E + coming` + }) + + it('returns highlight location from patch', async () => { + const result = getHighlightLocation(patch) + expect(result).to.eql(location) + }) +}) diff --git a/packages/db/migrations/0090.do.integrations.sql b/packages/db/migrations/0090.do.integrations.sql index c07568d25..33829ac77 100755 --- a/packages/db/migrations/0090.do.integrations.sql +++ b/packages/db/migrations/0090.do.integrations.sql @@ -9,7 +9,7 @@ CREATE TYPE omnivore.integration_type AS ENUM ('READWISE'); CREATE TABLE omnivore.integrations ( id uuid PRIMARY KEY DEFAULT uuid_generate_v1mc(), user_id uuid NOT NULL REFERENCES omnivore.user ON DELETE CASCADE, - "type" integration_type NOT NULL, + "type" omnivore.integration_type NOT NULL, token varchar(255) NOT NULL, "enabled" boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT current_timestamp,