Skip to content

Commit

Permalink
Work to get block references on their own line to render properly
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Sep 8, 2022
1 parent 466fa5f commit 57c338d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 66 deletions.
3 changes: 3 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ I am not known to be lazy, but for this plugin I had to be. This plugin leverage
# Prototype insatity
So I have invested 0 time into the presentation and formatting. So I am sorry it is ugly. But its a prototype. Let us prove the concept before adding the polish. Having said that, I left many breadcrumbs in the CSS for you to format the presentation. Consult the sytles css for the class definitions: https://github.com/TfTHacker/obsidian42-strange-new-worlds/blob/master/src/styles.css

# odd issues
Modifying the underyling Obsidian rendering engine is not easy. It is a complex piece of software. There are some issues I have not been able to resolve, so have had to compromise. I will list such examples as they surface here:
- An embed marked to render its display that is on its own line will force the reference counter to the next line. So if you have typed on a line something like this, all by itself: `![[MyPage#header1]]` it will show any block reference counts on the following line. However if the same text is entered as: `- ![[MyPage#header1]]`, that is, with any other text, it will render the block references as by design.

## Credit
I need to thank [Sam](https://github.com/Shabegom) and [Murf Man](https://github.com/gitmurf) for all their inspiration and work from this plugin: https://github.com/shabegom/obsidian-reference-count.
Expand Down
60 changes: 1 addition & 59 deletions src/htmlDecorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function htmlDecorationForReferencesElement(thePlugin: ThePlugin, count:
element.setAttribute("data-snw-type", referenceType);
element.setAttribute("data-snw-link", link);
element.ariaLabel = ariaLabel;


element.onclick = (e: MouseEvent ) => processHtmlDecorationReferenceEvent(e, thePlugin);

Expand All @@ -39,61 +38,4 @@ export function htmlDecorationForReferencesElement(thePlugin: ThePlugin, count:




// element.addClass("internal-link")
// element.setAttr("data-href", "MALISEK Kasija")


// const h = document.createElement("div");



// const view:MarkdownView = app.workspace.getActiveViewOfType(MarkdownView);
// console.log(view)
// window.xyz = view

// const mv = new MarkdownView(view.leaf);

// console.log("mv",mv)

// const wrapper = document.createElement('div');
// mv.contentEl.appendChild(wrapper)
// h.appendChild(mv.contentEl);
// const x = new HoverPopover(this, e.srcElement);
// x.hoverEl.append(h);

// console.log(h)

// // new Notice(`<a data-href="zTESTING/main" href="link file 1" class="internal-link snw-link-preview" target="_blank" rel="noopener">link file 1</a>`, 10000)
// h.innerHTML =`<div class="markdown-preview-view markdown-rendered"><a data-href="zTESTING/main" href="zTESTING/main" class="internal-link snw-link-preview" target="_blank" rel="noopener">Unttiled</a></div>`;
// wrapper.style.display = 'hidden';
// document.body.appendChild(wrapper);
// await MarkdownRenderer.renderMarkdown("this is my document [[main]] and text", wrapper, , null);
// const x:HTMLElement = document.createElement("div");


// x.innerHTML= `<a data-href="main" href="main" class="internal-link snw-link-preview" target="_blank" rel="noopener">Unttiled</a>`
// x.style.fontSize="20pt"
// console.log(e)
// e.srcElement.appendChild(x)
// app.workspace.trigger("hover-link", {
// event: e,
// source: "preview",
// hoverParent: e.target,
// targetEl: null, //hoverPreviewTarget,
// linktext: "test2",
// sourcePath: "test2",
// });

// }


// app.workspace.trigger("hover-link", {
// event: e,
// source: 'source',
// hoverParent: document.querySelector(".markdown-preview-view"),
// targetEl: null,
// linktext: 'zTESTING/main.md',
// });
// // sourcePath: 'zTESTING/main.md',
// }

26 changes: 19 additions & 7 deletions src/references-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ class InlineReferenceWidget extends WidgetType {
}

function matchAll(source: string, find: string) {
// console.log("matchall", source, find)
const result = [];
for(let i=0;i<source.length; ++i)
if (source.substring(i, i + find.length) == find)
if (source.substring(i, i + find.length) == find) {
result.push(i);
}
return result;
}

Expand Down Expand Up @@ -107,20 +109,27 @@ function calclulateInlineReferences(view: EditorView, theApp: App, mdView: Markd
break;
}

if(transformedCache.embeds)
for (const value of transformedCache.embedsWithDuplicates)
if(transformedCache.embedsWithDuplicates)
for (const value of transformedCache.embedsWithDuplicates) {
if(value.references.length>1)
matchAll(t, value.key).forEach(match=>
matchAll(t, value.key).forEach(match=> {
if(t==value.references[0].reference.original) {
match +=1; // for embeds that are on a line by themselves, they won't render unles the position is incremented by one
// unfortunately this push it down a line
}
referenceLocations.push({ type:"embed", count: value.references.length-1,
pos: currentLocationInDocument + match + value.key.length +2, key: value.key,
pos: currentLocationInDocument + match + value.key.length +2,
key: value.key,
link: value.references[0].reference.link,
arialLabel: generateArialLabel(CurrentFile, value)}
));
);
});
}

if(transformedCache.linksWithoutDuplicates)
for (const value of transformedCache.linksWithoutDuplicates)
if(value.references.length>1)
matchAll(t, value.key).forEach(match=>
matchAll(t, value.key).forEach(match=>
referenceLocations.push({ type:"link", count: value.references.length-1,
pos: currentLocationInDocument + match + value.key.length, key: value.key,
link: value.references[0].reference.link,
Expand All @@ -142,6 +151,9 @@ function calclulateInlineReferences(view: EditorView, theApp: App, mdView: Markd
currentLocationInDocument += t.length + 1;
});
}

// console.log("transformedCache", transformedCache);
// console.log("referenceLocations", referenceLocations)

referenceLocations.sort((a,b)=>a.pos-b.pos).forEach((r)=>{
rangeSetBuilder.add(
Expand Down

0 comments on commit 57c338d

Please sign in to comment.