Removed duplicated code

This commit is contained in:
Simon Cambier
2022-04-21 19:42:54 +02:00
parent 3d730b4d7f
commit 4238bc510e
3 changed files with 21 additions and 32 deletions

View File

@@ -1,5 +1,7 @@
import type { CachedMetadata } from 'obsidian'
import {
excerptAfter,
excerptBefore,
highlightClass,
isSearchMatch,
regexLineSplit,
@@ -82,3 +84,16 @@ export function extractHeadingsFromCache(
export function loopIndex(index: number, nbItems: number): number {
return (index + nbItems) % nbItems
}
export function makeExcerpt(content: string, offset: number): string {
const pos = offset ?? -1
if (pos > -1) {
const from = Math.max(0, pos - excerptBefore)
const to = Math.min(content.length - 1, pos + excerptAfter)
content =
(from > 0 ? '…' : '') +
content.slice(from, to).trim() +
(to < content.length - 1 ? '…' : '')
}
return escapeHTML(content)
}