Skip to content

Commit f3e4760

Browse files
authored
fix: prevent Obsidian 1.3.0 log spam (#1952)
* Partial fix to #1951 * Added a fix for the log spam for Markdown rendering (e.g. Reading Mode) based on this instruction by licat: https://discord.com/channels/686053708261228577/716028884885307432/1110789810160939048
1 parent 0c0b362 commit f3e4760

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/InlineRenderer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { MarkdownPostProcessorContext, Plugin } from 'obsidian';
2+
import { MarkdownRenderChild } from 'obsidian';
23
import { GlobalFilter } from './Config/GlobalFilter';
34
import { Task } from './Task';
45
import { TaskLocation } from './TaskLocation';
@@ -15,6 +16,11 @@ export class InlineRenderer {
1516
* of QueryRenderer (e.g. it removes the global filter and handles other formatting).
1617
*/
1718
private async _markdownPostProcessor(element: HTMLElement, context: MarkdownPostProcessorContext): Promise<void> {
19+
// As of Obsidian 1.3.0, it is required by Obsidian to create and/or pass a Component object
20+
// when using its Markdown rendering methods
21+
const childComponent = new MarkdownRenderChild(element);
22+
context.addChild(childComponent);
23+
1824
const renderedElements = element.findAll('.task-list-item').filter((taskItem) => {
1925
const linesText = taskItem.textContent?.split('\n');
2026
if (linesText === undefined) {
@@ -96,6 +102,7 @@ export class InlineRenderer {
96102
const taskElement = await task.toLi({
97103
parentUlElement: element,
98104
listIndex,
105+
obsidianComponent: childComponent,
99106
});
100107

101108
// If the rendered element contains a sub-list or sub-div (e.g. the

src/QueryRenderer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class QueryRenderChild extends MarkdownRenderChild {
202202
layoutOptions: this.query.layoutOptions,
203203
isFilenameUnique,
204204
taskLayout: layout,
205+
obsidianComponent: this,
205206
});
206207

207208
// Remove all footnotes. They don't re-appear in another document.

src/TaskLineRenderer.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type TaskLineRenderDetails = {
1212
parentUlElement: HTMLElement;
1313
/** The nth item in this list (including non-tasks). */
1414
listIndex: number;
15+
obsidianComponent: Component | null;
1516
layoutOptions?: LayoutOptions;
1617
isFilenameUnique?: boolean;
1718
taskLayout?: TaskLayout;
@@ -35,10 +36,21 @@ const DAY_VALUE_OVER_RANGE_POSTFIX = 'far';
3536
/**
3637
* The function used to render a Markdown task line into an existing HTML element.
3738
*/
38-
export type TextRenderer = (text: string, element: HTMLSpanElement, path: string) => Promise<void>;
39-
40-
async function obsidianMarkdownRenderer(text: string, element: HTMLSpanElement, path: string) {
41-
await MarkdownRenderer.renderMarkdown(text, element, path, null as unknown as Component);
39+
export type TextRenderer = (
40+
text: string,
41+
element: HTMLSpanElement,
42+
path: string,
43+
obsidianComponent: Component | null, // null is allowed here only for tests
44+
) => Promise<void>;
45+
46+
async function obsidianMarkdownRenderer(
47+
text: string,
48+
element: HTMLSpanElement,
49+
path: string,
50+
obsidianComponent: Component | null,
51+
) {
52+
if (!obsidianComponent) throw new Error('Must call the Obsidian renderer with an Obsidian Component object');
53+
await MarkdownRenderer.renderMarkdown(text, element, path, obsidianComponent);
4254
}
4355

4456
/**
@@ -138,7 +150,14 @@ async function taskToHtml(
138150
// to do things like surrouding only the text (rather than its whole placeholder) with a highlight
139151
const internalSpan = document.createElement('span');
140152
span.appendChild(internalSpan);
141-
await renderComponentText(internalSpan, componentString, component, task, textRenderer);
153+
await renderComponentText(
154+
internalSpan,
155+
componentString,
156+
component,
157+
task,
158+
textRenderer,
159+
renderDetails.obsidianComponent,
160+
);
142161
const [genericClasses, dataAttributes] = getComponentClassesAndData(component, task);
143162
addInternalClasses(component, internalSpan);
144163
// Add the generic classes that apply to what this component is (priority, due date etc)
@@ -178,14 +197,15 @@ async function renderComponentText(
178197
component: TaskLayoutComponent,
179198
task: Task,
180199
textRenderer: TextRenderer,
200+
obsidianComponent: Component | null,
181201
) {
182202
if (component === 'description') {
183203
const { debugSettings } = getSettings();
184204
if (debugSettings.showTaskHiddenData) {
185205
// Add some debug output to enable hidden information in the task to be inspected.
186206
componentString += `<br>🐛 <b>${task.lineNumber}</b> . ${task.sectionStart} . ${task.sectionIndex} . '<code>${task.originalMarkdown}</code>'<br>'<code>${task.path}</code>' > '<code>${task.precedingHeader}</code>'<br>`;
187207
}
188-
await textRenderer(componentString, span, task.path);
208+
await textRenderer(componentString, span, task.path, obsidianComponent);
189209

190210
// If the task is a block quote, the block quote wraps the p-tag that contains the content.
191211
// In that case, we need to unwrap the p-tag *inside* the surrounding block quote.

tests/TaskLineRenderer.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async function createMockParentAndRender(task: Task, layoutOptions?: LayoutOptio
3333
parentUlElement: parentElement,
3434
listIndex: 0,
3535
layoutOptions: layoutOptions,
36+
obsidianComponent: null,
3637
},
3738
mockTextRenderer,
3839
);

0 commit comments

Comments
 (0)