Skip to content

Commit

Permalink
0.0.22 Link in heading support
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Oct 4, 2022
1 parent 63f770d commit 75a073b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian42-strange-new-worlds",
"name": "Obsidian42 - Strange New Worlds",
"version": "0.0.21",
"version": "0.0.22",
"minAppVersion": "0.16.3",
"description": "Revealing networked thought and the strange new worlds created by your vault",
"author": "TfTHacker",
Expand Down
83 changes: 56 additions & 27 deletions src/cm-extensions/references-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(class {
decorations: DecorationSet = Decoration.none;
regxPattern = "";

constructor(public view: EditorView) {
constructor(public view: EditorView) {
if(thePlugin.settings.enableRenderingBlockIdInLivePreview)
this.regxPattern = "(\\s\\^)(\\S+)$";
if(thePlugin.settings.enableRenderingLinksInLivePreview)
Expand All @@ -41,7 +41,7 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(class {
this.regxPattern += (this.regxPattern != "" ? "|" : "") + "!\\[\\[(.*?)\\]\\]";
if(thePlugin.settings.enableRenderingHeadersInLivePreview)
this.regxPattern += (this.regxPattern != "" ? "|" : "") + "^#+\\s.+";

//if there is no regex pattern, then don't go further
if(this.regxPattern==="") return;

Expand All @@ -50,39 +50,64 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(class {
decorate: (add, from, to, match, view) => {
const mdView = view.state.field( editorInfoField );
const firstCharacterMatch = match[0].charAt(0);
let refType = "";
let key = "";
const transformedCache = getSNWCacheByFile(mdView.file);
let transformedCachedItem: TransformedCachedItem[] = null;
let wdgt: InlineReferenceWidget = null;
const widgetsToAdd: {key: string, transformedCachedItem: TransformedCachedItem[], refType: string, from: number, to: number}[] = []

if(firstCharacterMatch===" " && transformedCache?.blocks?.length>0) {
key = mdView.file.path.replace(".md","") + match[0].replace(" ^","#^"); //change this to match the references cache
transformedCachedItem = transformedCache.blocks;
refType = "block";
widgetsToAdd.push({
key: mdView.file.path.replace(".md","") + match[0].replace(" ^","#^"), //change this to match the references cache
transformedCachedItem: transformedCache.blocks,
refType: "block",
from: to,
to: to
});
} else if(firstCharacterMatch==="!" && transformedCache?.embeds?.length>0) { //embeds
key = match[0].replace("![[","").replace("]]","");
transformedCachedItem = transformedCache.embeds;
refType = "embed";
widgetsToAdd.push({
key: match[0].replace("![[","").replace("]]",""),
transformedCachedItem: transformedCache.embeds,
refType:"embed",
from: to,
to: to
});
} else if(firstCharacterMatch==="[" && transformedCache?.links?.length>0) { //link
key = match[0].replace("[[","").replace("]]","");
transformedCachedItem = transformedCache.links
refType = "link";
widgetsToAdd.push({
key: match[0].replace("[[","").replace("]]",""),
transformedCachedItem: transformedCache.links,
refType: "link",
from: to,
to: to
});
} else if(firstCharacterMatch==="#" && transformedCache?.headings?.length>0) { //link
// @ts-ignore
key = match[0].replaceAll("#","").substring(1);
transformedCachedItem = transformedCache.headings
refType = "heading";
widgetsToAdd.push({
// @ts-ignore
key: match[0].replaceAll("#","").substring(1),
transformedCachedItem: transformedCache.headings,
refType: "heading",
from: to,
to: to
});
const linksinHeader = match[0].match(/(?<=[^!])\[\[(.*?)\]\]|!\[\[(.*?)\]\]/g);
if(linksinHeader)
for (const l of linksinHeader) {
widgetsToAdd.push({
key: l.replace("![[","").replace("[[","").replace("]]",""), //change this to match the references cache
transformedCachedItem: l.startsWith("!") ? transformedCache.embeds : transformedCache.links,
refType: "link",
from: (to - match[0].length) + (match[0].indexOf(l) + l.length),
to: (to - match[0].length) + (match[0].indexOf(l) + l.length)
});
}
}

for (const ref of widgetsToAdd.sort((a,b)=>a.to-b.to) ) {
if(ref.key!="") {
const wdgt = constructWidgetForInlineReference(ref.refType, ref.key, ref.transformedCachedItem, mdView.file.path);
if(wdgt!=null) {
add(ref.from, ref.to, Decoration.widget({widget: wdgt, side: 1}));
}
}
} // end for

if((refType==="embed" || refType==="link") && key.contains("|")) // check for aliased references
key = key.substring(0, key.search(/\|/));

if(key!="") {
wdgt = constructWidgetForInlineReference(refType, key, transformedCachedItem, mdView.file.path);
if(wdgt!=null)
add(to, to, Decoration.widget({widget: wdgt, side: 1}));
}
},
})

Expand Down Expand Up @@ -111,13 +136,17 @@ export const InlineReferenceExtension = ViewPlugin.fromClass(class {
* @return {*} {InlineReferenceWidget}
*/
const constructWidgetForInlineReference = (refType: string, key: string, references: TransformedCachedItem[], filePath: string): InlineReferenceWidget => {
if((refType==="embed" || refType==="link") && key.contains("|")) // check for aliased references
key = key.substring(0, key.search(/\|/));

for (let i = 0; i < references.length; i++) {
const ref = references[i];
let matchKey = ref.key;
if(refType==="heading") {
matchKey = ref.headerMatch; // headers require special comparison
key = key.replace(/^\s+|\s+$/g,''); // should be not leading spaces
}

if(matchKey===key) {
if(ref?.references.length>=thePlugin.settings.minimumRefCountThreshold)
return new InlineReferenceWidget(ref.references.length, ref.type, ref.key, ref.references[0].resolvedFile.path.replace(".md",""), null, ref.pos.start.line);
Expand Down

0 comments on commit 75a073b

Please sign in to comment.