Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CacheDb, IMention, IOccurence } from './CacheDb';
import { getCodeblockPositions, isPositionInCodeblock } from './CodeblockHelper';
import { cyrb53Hash } from './cyrb53Hash';
import { isFilePathInIgnoredDirectories } from './IgnoreHelper';
import { getLinkPositions, isPositionInLink } from './LinkHelper';
import { getMentionRegExp, TASK_COMPLETE_REG_EXP } from './RegExp';
import { MentionSettings } from './Settings';

Expand Down Expand Up @@ -212,6 +213,11 @@ export class Cache {
(match) => !isPositionInCodeblock(codeblockPositions, match.matchArray.index + doc.line(match.lineNumber).from)
);

const linkPositions: [from: number, to: number][] = getLinkPositions(indexableFile.fileContent);
matches = matches.filter(
(match) => !isPositionInLink(linkPositions, match.matchArray.index + doc.line(match.lineNumber).from)
);

const occurences: IOccurence[] = [];

await Promise.all(
Expand Down
4 changes: 4 additions & 0 deletions src/CmDecorationExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { App } from 'obsidian';
import { getCodeblockPositions, isPositionInCodeblock } from './CodeblockHelper';
import { CLASS_ME_MENTION, CLASS_MENTION } from './Constants';
import { isFilePathInIgnoredDirectories } from './IgnoreHelper';
import { getLinkPositions, isPositionInLink } from './LinkHelper';
import { MentionSettings } from './Settings';

export interface CmDecorationExtensionConfig {
Expand Down Expand Up @@ -42,6 +43,9 @@ export function getCmDecorationExtension(app: App, cfg: CmDecorationExtensionCon
const codeblockPositions: [from: number, to: number][] = getCodeblockPositions(range, from);
mentions = mentions.filter((mention) => !isPositionInCodeblock(codeblockPositions, from + mention.index));

const linkPositions: [from: number, to: number][] = getLinkPositions(range, from);
mentions = mentions.filter((mention) => !isPositionInLink(linkPositions, from + mention.index));

mentions.forEach((m) => {
rangeSetBuilder.add(
from + m.index,
Expand Down
11 changes: 6 additions & 5 deletions src/CodeblockHelper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CODEBLOCK_REG_EXP } from './RegExp';
import { CODEBLOCK_REG_EXP, CODE_INLINE_REG_EXP } from './RegExp';

export function getCodeblockPositions(text: string, startIndex: number = 0): [from: number, to: number][] {
const codeblocks = [...text.matchAll(CODEBLOCK_REG_EXP)];
let codeblockPositions: [from: number, to: number][] = [];
type CodeblockRange = [from: number, to: number];

export function getCodeblockPositions(text: string, startIndex: number = 0): CodeblockRange[] {
const codeblocks = [...text.matchAll(CODEBLOCK_REG_EXP), ...text.matchAll(CODE_INLINE_REG_EXP)];
return codeblocks.map((c) => [startIndex + c.index, startIndex + c.index + c[0].length]);
}

export function isPositionInCodeblock(codeblocks: [from: number, to: number][], position: number) {
export function isPositionInCodeblock(codeblocks: CodeblockRange[], position: number) {
return codeblocks.find((c) => c[0] < position && c[1] > position) != null;
}
13 changes: 13 additions & 0 deletions src/LinkHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LINK_REG_EXP } from './RegExp';

type LinkRange = [from: number, to: number];

export function getLinkPositions(text: string, startIndex: number = 0): LinkRange[] {
const codeblocks = [...text.matchAll(LINK_REG_EXP)];
return codeblocks.map((c) => [startIndex + c.index, startIndex + c.index + c[0].length]);
}

export function isPositionInLink(links: LinkRange[], position: number) {
return links.find((c) => c[0] < position && c[1] > position) != null;
}

3 changes: 3 additions & 0 deletions src/MentionSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Cache } from './Cache';
import { getCodeblockPositions, isPositionInCodeblock } from './CodeblockHelper';
import { isFilePathInIgnoredDirectories } from './IgnoreHelper';
import { getLinkPositions, isPositionInLink } from './LinkHelper';
import { MENTION_SUGGEST_REG_EXP } from './RegExp';
import { MentionSettings } from './Settings';

Expand All @@ -29,9 +30,11 @@ export class MentionSuggest extends EditorSuggest<Completition> {
if (isFilePathInIgnoredDirectories(file.path, this.settings)) return;

const codeblockPositions: [from: number, to: number][] = getCodeblockPositions(editor.getValue());
const linkPositions: [from: number, to: number][] = getLinkPositions(editor.getValue());
const cursorPos = editor.posToOffset(cursor);

if (isPositionInCodeblock(codeblockPositions, cursorPos)) return;
if (isPositionInLink(linkPositions, cursorPos)) return;

const line = editor.getLine(cursor.line).substring(0, cursor.ch);

Expand Down
2 changes: 2 additions & 0 deletions src/RegExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export const MENTION_SUGGEST_REG_EXP = new RegExp('(^"[\\p{L}0-9-_, ]*$)|(^[\\p{
export const TASK_REG_EXP = new RegExp(/\[[ x]{1}\]/);
export const TASK_COMPLETE_REG_EXP = new RegExp(/^\- \[x{1}\]/);
export const CODEBLOCK_REG_EXP = new RegExp('```[a-zA-Z0-9\\w\\d\\s!@#$%^&*()_+-=\\[\\]{};\':"\\|,.<>/?]*```', 'gm');
export const CODE_INLINE_REG_EXP = new RegExp('`[a-zA-Z0-9\\w\\d\\s!@#$%^&*()_+-=\\[\\]{};\':"\\|,.<>/?]*`', 'g');
export const LINK_REG_EXP = new RegExp(/(\[[^\]]+\]\([^)]+\)|<[^>]+>)/g);

export function getMeMentionOrMentionRegex(trigger: string, meMentionName: string): RegExp {
return new RegExp(getMeMentionRegExp(trigger, meMentionName).source + '|' + getMentionRegExp(trigger).source, 'gmu');
Expand Down