Skip to content

Commit

Permalink
Merge pull request #39 from jaanonim/fix-callout-newline
Browse files Browse the repository at this point in the history
fix missing callout newline
  • Loading branch information
jaanonim authored Jan 6, 2025
2 parents 94e7741 + ba5723c commit 4278d29
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/EditorSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,28 @@ export class EditorSuggester extends EditorSuggest<VerseLink> {
(cursor.ch - link_pos < cursor.ch - embed_pos || embed_pos < 0);
const pos = isLink ? link_pos : embed_pos;
const currentContent = currentLine.substring(pos + 1, cursor.ch).trim();
const prefix = currentLine.substring(0, pos);

const matches = currentContent.match(linkRegex);
if (!matches) return null;
return matches?.reduce((prev, match) => {
if (match && prev === null) {
const end = currentContent.lastIndexOf(match);
if (end === 0 || currentContent.charAt(end - 1) !== "[")
if (end === 0 || currentContent.charAt(end - 1) !== "[") {
const t = isLink
? "@"
: prefix.match(/^ {1,3}$/gm)
? ">"
: "<";
return {
end: cursor,
start: {
line: cursor.line,
ch: pos,
},
query: (isLink ? "@" : ">") + match,
query: t + match,
};
}
}
return null;
}, null);
Expand All @@ -70,15 +77,17 @@ export class EditorSuggester extends EditorSuggest<VerseLink> {
context: EditorSuggestContext
): VerseLink[] | Promise<VerseLink[]> {
const query = context.query;
const isLink = query[0] !== ">";
const isLink = query[0] == "@";
const insertNewLine = query[0] == "<";

if (query[0] !== "@" && query[0] !== ">") {
if (query[0] !== "@" && query[0] !== ">" && query[0] !== "<") {
console.error(`INTERNAL: query should start with @ or >`);
}

return getSuggestionsFromQuery(
query.substring(1),
isLink,
insertNewLine,
this.settings
);
}
Expand Down Expand Up @@ -117,6 +126,7 @@ export function processVerses(verses_str: Array<string>): Array<VerseElement> {
export function getSuggestionsFromQuery(
query: string,
isLink: boolean,
insertNewLine: boolean,
settings: ObsidianYouversionLinkerSettings
): Verse[] {
console.debug("get suggestion for query ", query.toLowerCase());
Expand Down Expand Up @@ -158,7 +168,8 @@ export function getSuggestionsFromQuery(
bookUrl,
bookName,
chapterNumber,
verses
verses,
insertNewLine
);
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/GenerateLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function GenerateLinks(
const match = [...line.matchAll(linkRegex)];
match.forEach((match) => {
const suggestions = removeDuplicatedSuggestionsHandler(
getSuggestionsFromQuery(match[0], true, settings)
getSuggestionsFromQuery(match[0], true, false, settings)
);
suggestions.forEach(async (s) => {
if (match.index === undefined) return;
Expand Down
19 changes: 16 additions & 3 deletions src/VerseEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import LinkPreviewManager from "./LinkPreview";
import Verse from "./Verse";
import { BibleVersion } from "./SettingsData";
import Verse, { VerseElement } from "./Verse";

export default class VerseEmbed extends Verse {
constructor(
version: BibleVersion,
bookUrl: string,
book: string,
chapter: number,
verses: Array<VerseElement>,
private insertNewLine: boolean
) {
super(version, bookUrl, book, chapter, verses);
}

async toReplace(): Promise<string> {
const content = await LinkPreviewManager.processUrl(this.getUrl());
const p = this.insertNewLine ? "\n" : "";
if (content.err) {
return `>[!Error] Cannot get content of ${this.toSimpleText()}.\n`;
return `${p}>[!Error] Cannot get content of ${this.toSimpleText()}.\n`;
} else {
// prettier-ignore
return `>[!Quote] [${this.toSimpleText()} ${content.info.version}](${this.getUrl()})\n>${content.verses.replace(/\n/g,'\n>')}\n`;
return `${p}>[!Quote] [${this.toSimpleText()} ${content.info.version}](${this.getUrl()})\n>${content.verses.replace(/\n/g,'\n>')}\n`;
}
}
}

0 comments on commit 4278d29

Please sign in to comment.