Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline rendering in reading view #2377

Merged
merged 4 commits into from
Jun 20, 2024
Merged
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
39 changes: 19 additions & 20 deletions src/ui/views/inline-field.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { InlineField, extractInlineFields, parseInlineValue } from "data-import/inline-field";
import { Values } from "data-model/value";
import { MarkdownPostProcessorContext, MarkdownRenderChild } from "obsidian";
import { h, render } from "preact";
import { DataviewContext, DataviewInit, Lit } from "ui/markdown";
import { canonicalizeVarName } from "util/normalize";

/** Replaces raw textual inline fields in text containers with pretty HTML equivalents. */
export async function replaceInlineFields(ctx: MarkdownPostProcessorContext, init: DataviewInit): Promise<void> {
let inlineFields = extractInlineFields(init.container.innerHTML);
const inlineFields = extractInlineFields(init.container.innerHTML);
if (inlineFields.length == 0) return;

const text = ctx.getSectionInfo(init.container)?.text;
let inlineFieldsFromText: InlineField[] | null = null;
if (text) {
inlineFieldsFromText = extractInlineFields(text);
}

let component = new MarkdownRenderChild(init.container);
const component = new MarkdownRenderChild(init.container);
ctx.addChild(component);

// Iterate through the raw HTML and replace inline field matches with corresponding rendered values.
Expand Down Expand Up @@ -60,27 +53,33 @@ export async function replaceInlineFields(ctx: MarkdownPostProcessorContext, ini
// Replace the container children with the new rendered children.
// TODO: Replace this with a dom-to-dom diff to reduce the actual amount of updates.
init.container.replaceChildren(...template.content.childNodes);

let inlineFieldsFromText: InlineField[] | undefined;
let hasRetrievedText: boolean = false;
for (let index = 0; index < inlineFields.length; index++) {
const box = init.container.querySelector("#dataview-inline-field-" + index);
if (!box) continue;

const context = Object.assign({}, init, { container: box, component: component });

let parsedValue;
if (inlineFieldsFromText && inlineFieldsFromText[index].key == inlineFields[index].key) {
const parsedValueFromText = parseInlineValue(inlineFieldsFromText[index].value);
if (Values.isString(parsedValueFromText)) {
parsedValue = parsedValueFromText;
const parseInlineValueWrapper = (fieldVal: string) => {
if (fieldVal.startsWith("<span class=\"math\"")) {
// allows math symbols to be rendered in reading view
if (!hasRetrievedText) {
hasRetrievedText = true;
let text = ctx.getSectionInfo(init.container)?.text;
if (text) {
inlineFieldsFromText = extractInlineFields(text);
}
}
if (!inlineFieldsFromText) return parseInlineValue(fieldVal);
return parseInlineValue(inlineFieldsFromText[index].value);
} else {
return parseInlineValue(fieldVal);
}
}
if (parsedValue === undefined) {
parsedValue = parseInlineValue(inlineFields[index].value);
}

render(
<DataviewContext.Provider value={context}>
<Lit value={parsedValue} inline={true} sourcePath={ctx.sourcePath} />
<Lit value={parseInlineValueWrapper(inlineFields[index].value)} inline={true} sourcePath={ctx.sourcePath} />
</DataviewContext.Provider>,
box
);
Expand Down
Loading