Skip to content

Commit

Permalink
Merge pull request #111 from omnivore-app/fix/index-out-of-bound
Browse files Browse the repository at this point in the history
fix/index out of bound
  • Loading branch information
sywhb authored May 19, 2023
2 parents e8f1a26 + ee3185d commit d347fdd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@logseq/libs": "^0.0.14",
"date-fns": "^2.29.3",
"diff-match-patch": "^1.0.5",
"lodash": "^4.17.21",
"luxon": "^3.2.1",
"markdown-escape": "^1.1.0",
"mustache": "^4.2.0"
Expand All @@ -39,6 +40,7 @@
"@types/chai": "^4.3.4",
"@types/chai-string": "^1.4.2",
"@types/diff-match-patch": "^1.0.32",
"@types/lodash": "^4.14.194",
"@types/luxon": "^2.3.2",
"@types/markdown-escape": "^1.1.0",
"@types/mocha": "^10.0.1",
Expand Down
8 changes: 5 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface Article {
readingProgressPercent: number
isArchived: boolean
wordsCount?: number
archivedAt?: string
}

export interface Label {
Expand Down Expand Up @@ -120,6 +121,10 @@ export const getOmnivoreArticles = async (
content
publishedAt
readAt
isArchived
readingProgressPercent
wordsCount
archivedAt
highlights {
id
quote
Expand All @@ -135,9 +140,6 @@ export const getOmnivoreArticles = async (
labels {
name
}
isArchived
readingProgressPercent
wordsCount
}
}
pageInfo {
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import {
DATE_FORMAT,
compareHighlightsInFile,
escapeQuotes,
getHighlightLocation,
isBlockPropertiesChanged,
parseBlockProperties,
Expand Down Expand Up @@ -86,13 +87,13 @@ const getBlockByContent = async (
`[:find (pull ?b [*])
:where
[?b :block/page ?p]
[?p :block/original-name "${pageName}"]
[?p :block/original-name "${escapeQuotes(pageName)}"]
[?b :block/parent ?parent]
[?parent :block/uuid ?u]
[(str ?u) ?s]
[(= ?s "${parentBlockId}")]
[?b :block/content ?c]
[(clojure.string/includes? ?c "${content}")]]`
[(clojure.string/includes? ?c "${escapeQuotes(content)}")]]`
)
).flat()

Expand All @@ -108,9 +109,9 @@ const getBlockByTitle = async (
`[:find (pull ?b [*])
:where
[?b :block/page ?p]
[?p :block/original-name "${pageName}"]
[?p :block/original-name "${escapeQuotes(pageName)}"]
[?b :block/content ?c]
[(= ?c "${title}")]]`
[(= ?c "${escapeQuotes(title)}")]]`
)
).flat()

Expand Down Expand Up @@ -242,7 +243,7 @@ const fetchOmnivore = async (inBackground = false) => {
parseDateTime(syncAt).toISO(),
getQueryFromFilter(filter, customQuery),
true,
'markdown',
'highlightedMarkdown',
endpoint
)
const articleBatchMap: Map<string, IBatchBlock[]> = new Map()
Expand Down
19 changes: 17 additions & 2 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { truncate } from 'lodash'
import Mustache from 'mustache'
import { Article, Highlight, HighlightType, Label, PageType } from '../api'
import {
Expand Down Expand Up @@ -33,6 +34,7 @@ export type ArticleView =
state: string
wordsCount?: number
readLength?: number
dateArchived?: string
}
| FunctionMap

Expand Down Expand Up @@ -137,6 +139,9 @@ const createArticleView = (
const readLength = wordsCount
? Math.round(Math.max(1, wordsCount / 235))
: undefined
const dateArchived = article.archivedAt
? formatDate(new Date(article.archivedAt), preferredDateFormat)
: undefined
return {
title: article.title,
omnivoreUrl: `https://omnivore.app/me/${article.slug}`,
Expand All @@ -155,6 +160,7 @@ const createArticleView = (
state: getArticleState(article),
wordsCount,
readLength,
dateArchived,
...functionMap,
}
}
Expand Down Expand Up @@ -195,10 +201,19 @@ export const renderPageName = (
preferredDateFormat: string
) => {
const date = formatDate(new Date(article.savedAt), preferredDateFormat)
return Mustache.render(pageName, {
title: article.title,
// replace slash with dash in the title to prevent creating subpages
// since there is no way to escape slashes in logseq
const title = article.title.replace(/\//g, '-')

const renderedPageName = Mustache.render(pageName, {
title,
date,
})

// truncate the page name to 100 characters
return truncate(renderedPageName, {
length: 100,
})
}

export const preParseTemplate = (template: string) => {
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ export const isBlockPropertiesChanged = (
return newBlockProperty != existingBlockProperty
})
}

export const escapeQuotes = (str: string): string => {
return str.replace(/"/g, '\\"')
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,11 @@
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/lodash@^4.14.194":
version "4.14.194"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.194.tgz#b71eb6f7a0ff11bff59fc987134a093029258a76"
integrity sha512-r22s9tAS7imvBt2lyHC9B8AGwWnXaYb1tY09oyLkXDs4vArpYJzw09nj8MLx5VfciBPGIb+ZwG0ssYnEPJxn/g==

"@types/luxon@^2.3.2":
version "2.3.2"
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.3.2.tgz#8a3f2cdd4858ce698b56cd8597d9243b8e9d3c65"
Expand Down Expand Up @@ -3369,7 +3374,7 @@ lodash.uniqby@^4.7.0:

lodash@^4.17.15, lodash@^4.17.21, lodash@^4.17.4:
version "4.17.21"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

[email protected]:
Expand Down

0 comments on commit d347fdd

Please sign in to comment.