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
9 changes: 9 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface Settings {
enableRenderingLinksInMarkdown: boolean;
enableRenderingHeadersInMarkdown: boolean;
enableRenderingEmbedsInMarkdown: boolean;
// enableRenderingXXXInLivePreview are used for both live preview and source mode
// they should probably be renamed to reflect that
enableRenderingBlockIdInLivePreview: boolean;
enableRenderingLinksInLivePreview: boolean;
enableRenderingHeadersInLivePreview: boolean;
Expand Down Expand Up @@ -57,3 +59,10 @@ export const DEFAULT_SETTINGS: Settings = {
displayCustomPropertyList: "",
pluginSupportKanban: false,
};

export function isEnabledForMode(settings: Settings, sourceMode: boolean): boolean {
if (sourceMode) {
return settings.displayInlineReferencesInSourceMode;
}
return settings.displayInlineReferencesLivePreview;
}
2 changes: 1 addition & 1 deletion src/ui/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class SettingsTab extends PluginSettingTab {
});
});

new Setting(containerEl).setHeading().setName("Enable reference types in Live Preview Mode");
new Setting(containerEl).setHeading().setName("Enable reference types in Live Preview Mode and Source Mode");
containerEl.createEl("sup", {
text: "(requires reopening documents to take effect)",
});
Expand Down
7 changes: 5 additions & 2 deletions src/view-extensions/gutters-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BlockInfo, EditorView } from "@codemirror/view";
import { editorInfoField } from "obsidian";
import { getSNWCacheByFile, parseLinkTextToFullPath } from "src/indexer";
import type SNWPlugin from "src/main";
import { isEnabledForMode } from "src/settings";
import { htmlDecorationForReferencesElement } from "src/view-extensions/htmlDecorations";

let plugin: SNWPlugin;
Expand Down Expand Up @@ -53,8 +54,10 @@ const ReferenceGutterExtension = gutter({
lineMarker(editorView: EditorView, line: BlockInfo) {
const mdView = editorView.state.field(editorInfoField);

// @ts-ignore - Check if should show in source mode
if (mdView.currentMode?.sourceMode === true && plugin.settings.displayInlineReferencesInSourceMode === false) return null;
// @ts-ignore
const sourceMode = mdView.currentMode?.sourceMode === true;
// Check if should show in source mode or live preview mode
if (!isEnabledForMode(plugin.settings, sourceMode)) return null;

if (!mdView.file) return null;
const transformedCache = getSNWCacheByFile(mdView.file);
Expand Down
27 changes: 11 additions & 16 deletions src/view-extensions/references-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Decoration, type DecorationSet, type EditorView, MatchDecorator, ViewPl
import { type TFile, editorInfoField, parseLinktext, stripHeading } from "obsidian";
import { getSNWCacheByFile, parseLinkTextToFullPath } from "src/indexer";
import type SNWPlugin from "src/main";
import { isEnabledForMode } from "src/settings";
import type { TransformedCachedItem } from "../types";
import { htmlDecorationForReferencesElement } from "./htmlDecorations";

Expand All @@ -26,14 +27,10 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(

constructor(public view: EditorView) {
// The constructor seems to be called only once when a file is viewed. The decorator is called multipe times.
const enableLivePreview = plugin.settings.displayInlineReferencesLivePreview;
if (enableLivePreview && plugin.settings.enableRenderingBlockIdInLivePreview) this.regxPattern = "(\\s\\^)(\\S+)$";
if (enableLivePreview && plugin.settings.enableRenderingEmbedsInLivePreview)
this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}!\\[\\[(.*?)\\]\\]`;
if (enableLivePreview && plugin.settings.enableRenderingLinksInLivePreview)
this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}\\[\\[(.*?)\\]\\]`;
if (enableLivePreview && plugin.settings.enableRenderingHeadersInLivePreview)
this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}^#+\\s.+`;
if (plugin.settings.enableRenderingBlockIdInLivePreview) this.regxPattern = "(\\s\\^)(\\S+)$";
if (plugin.settings.enableRenderingEmbedsInLivePreview) this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}!\\[\\[(.*?)\\]\\]`;
if (plugin.settings.enableRenderingLinksInLivePreview) this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}\\[\\[(.*?)\\]\\]`;
if (plugin.settings.enableRenderingHeadersInLivePreview) this.regxPattern += `${this.regxPattern !== "" ? "|" : ""}^#+\\s.+`;

//if there is no regex pattern, then don't go further
if (this.regxPattern === "") return;
Expand All @@ -54,11 +51,7 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(
let mdViewFile: TFile | null = null;

// there is no file, likely a canvas file, look for links and embeds, process it with snwApi.references
if (
!mdView.file &&
enableLivePreview &&
(plugin.settings.enableRenderingEmbedsInLivePreview || plugin.settings.enableRenderingLinksInLivePreview)
) {
if (!mdView.file && (plugin.settings.enableRenderingEmbedsInLivePreview || plugin.settings.enableRenderingLinksInLivePreview)) {
const ref = match[0].replace(/^\[\[|\]\]$|^!\[\[|\]\]$/g, "");
const key = parseLinkTextToFullPath(ref).toLocaleUpperCase();
if (key) {
Expand Down Expand Up @@ -87,8 +80,10 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(
} else {
// If we get this far, then it is a file, and process it using getSNWCacheByFile

// @ts-ignore && Check if should show in source mode
if (plugin.settings.displayInlineReferencesInSourceMode === false && mdView.currentMode?.sourceMode === true) return null;
// @ts-ignore
const sourceMode = mdView.currentMode?.sourceMode === true;
// Check if should show in source mode or live preview mode
if (!isEnabledForMode(plugin.settings, sourceMode)) return null;

mdViewFile = mdView.file as TFile;

Expand Down Expand Up @@ -125,7 +120,7 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(
from: to,
to: to,
});
if (enableLivePreview && plugin.settings.enableRenderingLinksInLivePreview) {
if (plugin.settings.enableRenderingLinksInLivePreview) {
// this was not working with mobile from 0.16.4 so had to convert it to a string
const linksinHeader = match[0].match(/\[\[(.*?)\]\]|!\[\[(.*?)\]\]/g);
if (linksinHeader)
Expand Down