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,