Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 8225688

Browse files
committed
trying to improve markdown render
1 parent 72451b0 commit 8225688

File tree

7 files changed

+96
-76
lines changed

7 files changed

+96
-76
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"obsidian": "0.14.6",
4343
"obsidian-dataview": "0.5.20",
4444
"rollup": "2.72.0",
45+
"rollup-plugin-typescript2": "0.31.2",
4546
"ts-jest": "27.1.4",
4647
"tslib": "2.4.0",
4748
"typescript": "4.6.4",

rollup.config.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
import typescript from "@rollup/plugin-typescript";
21
import { nodeResolve } from "@rollup/plugin-node-resolve";
32
import commonjs from "@rollup/plugin-commonjs";
43
import json from "@rollup/plugin-json";
5-
4+
import typescript2 from "rollup-plugin-typescript2";
65
const isProd = (process.env.BUILD === 'production');
76

8-
export default (commandLineArgs) => ({
9-
input: 'src/main.ts',
7+
const BASE_CONFIG = {
8+
input: "src/main.ts",
9+
external: ["obsidian","obsidian-dataview/lib/data-model/value"],
10+
onwarn: (warning, warn) => {
11+
// Sorry rollup, but we're using eval...
12+
if (/Use of eval is strongly discouraged/.test(warning.message)) return;
13+
warn(warning);
14+
},
15+
};
16+
17+
const getRollupPlugins = (tsconfig, ...plugins) =>
18+
[
19+
typescript2(tsconfig),
20+
nodeResolve({ browser: true }),
21+
json(),
22+
commonjs(),
23+
].concat(plugins);
24+
25+
const PROD_PLUGIN_CONFIG = {
26+
...BASE_CONFIG,
1027
output: {
1128
dir: 'dist',
1229
sourcemap: 'inline',
1330
sourcemapExcludeSources: isProd,
14-
format: 'cjs',
15-
exports: 'default',
31+
format: "cjs",
32+
exports: "default",
33+
name: "Database Folder (Production)",
1634
},
17-
external: ['obsidian'],
18-
plugins: [
19-
typescript(),
20-
nodeResolve({browser: true}),
21-
commonjs(),
22-
json(),
23-
]
24-
});
35+
plugins: getRollupPlugins(),
36+
};
37+
let configs = [];
38+
configs.push(PROD_PLUGIN_CONFIG);
39+
40+
export default configs;

src/components/Cell.tsx

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ import { c } from "helpers/StylesHelper";
1111
import CalendarPortal from "./portals/CalendarPortal";
1212
import { TableColumn } from "cdm/FolderModel";
1313

14-
/**
15-
* Obtain the path of the file inside cellValue
16-
* i.e. if cellValue is "[[path/to/file.md|File Name]]" then return "path/to/file.md"
17-
* i.e. if cellValue is "[[path/to/file.md]]" then return "path/to/file.md"
18-
* i.e. if cellValue is "[[file.md]]" then return "file.md"
19-
* @param cellValue
20-
*/
21-
function getFilePath(cellValue: string): string {
22-
const regex = /\[\[(.*)\]\]/;
23-
const matches = regex.exec(cellValue);
24-
if (matches && matches.length > 1) {
25-
return matches[1];
26-
}
27-
return "";
28-
}
29-
3014
export default function DefaultCell(cellProperties: Cell) {
3115
const dataDispatch = (cellProperties as any).dataDispatch;
3216
/** Initial state of cell */
@@ -37,6 +21,8 @@ export default function DefaultCell(cellProperties: Cell) {
3721
const dataType = (cellProperties.column as any).dataType;
3822
/** Note info of current Cell */
3923
const note: NoteInfo = (cellProperties.row.original as any).note;
24+
/** Ref to cell container */
25+
const containerCellRef = useRef<HTMLDivElement>();
4026
/** state of cell value */
4127
const [contextValue, setContextValue] = useState({
4228
value: initialValue,
@@ -59,6 +45,19 @@ export default function DefaultCell(cellProperties: Cell) {
5945
}
6046
}, []);
6147

48+
useLayoutEffect(() => {
49+
if (!dirtyCell && containerCellRef.current) {
50+
//TODO - this is a hack. find why is layout effect called twice
51+
containerCellRef.current.innerHTML = "";
52+
MarkdownRenderer.renderMarkdown(
53+
contextValue.value,
54+
containerCellRef.current,
55+
note.getFile().path,
56+
null
57+
);
58+
}
59+
}, [dirtyCell]);
60+
6261
const handleKeyDown = (event: any) => {
6362
if (event.key === "Enter") {
6463
event.target.blur();
@@ -95,49 +94,43 @@ export default function DefaultCell(cellProperties: Cell) {
9594
setDirtyCell(false);
9695
}
9796

98-
function obsidianMarkdownContainer(mdContain: string) {
99-
const containerRef = useRef<HTMLElement>();
100-
useLayoutEffect(() => {
101-
MarkdownRenderer.renderMarkdown(
102-
initialValue,
103-
containerRef.current,
104-
mdContain,
105-
null
106-
);
107-
});
108-
return <span ref={containerRef} className={`${c("md_cell")}`}></span>;
109-
}
110-
111-
function contentEditableContainer(className: string) {
112-
return (
113-
<ContentEditable
114-
html={(contextValue.value && contextValue.value.toString()) || ""}
115-
onChange={handleOnChange}
116-
onKeyDown={handleKeyDown}
117-
onBlur={() =>
118-
setContextValue((old) => ({ value: old.value, update: true }))
119-
}
120-
className={className}
121-
/>
122-
);
123-
}
124-
12597
function getCellElement() {
12698
switch (dataType) {
12799
/** Plain text option */
128100
case DataTypes.TEXT:
129101
return (cellProperties.column as any).isMetadata ? (
130102
<span className="data-input">{contextValue.value.toString()}</span>
131103
) : (
132-
contentEditableContainer("data-input")
104+
<ContentEditable
105+
html={(contextValue.value && contextValue.value.toString()) || ""}
106+
onChange={handleOnChange}
107+
onKeyDown={handleKeyDown}
108+
onBlur={() =>
109+
setContextValue((old) => ({ value: old.value, update: true }))
110+
}
111+
className={"data-input"}
112+
innerRef={containerCellRef}
113+
/>
133114
);
134115
/** Number option */
135116
case DataTypes.NUMBER:
136-
return contentEditableContainer("data-input text-align-right");
117+
return (
118+
<ContentEditable
119+
html={(contextValue.value && contextValue.value.toString()) || ""}
120+
onChange={handleOnChange}
121+
onKeyDown={handleKeyDown}
122+
onBlur={() =>
123+
setContextValue((old) => ({ value: old.value, update: true }))
124+
}
125+
className="data-input text-align-right"
126+
/>
127+
);
137128

138129
/** Markdown option */
139130
case DataTypes.MARKDOWN:
140-
return obsidianMarkdownContainer(getFilePath(initialValue));
131+
return (
132+
<span ref={containerCellRef} className={`${c("md_cell")}`}></span>
133+
);
141134
/** Selector option */
142135
case DataTypes.SELECT:
143136
return (

src/components/portals/CalendarPortal.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ const CalendarPortal = (calendarProps: CalendarProps) => {
2929

3030
/** Note info of current Cell */
3131
const note: NoteInfo = (cellProperties.row.original as any).note;
32-
const [calendarState, setCalendarState] = useState(
33-
(
34-
DataviewService.parseLiteral(
35-
cellProperties.value,
36-
DataTypes.CALENDAR
37-
) as DateTime
38-
).toJSDate()
39-
);
32+
const [calendarState, setCalendarState] = useState(null);
4033

4134
function handleCalendarChange(date: Date) {
4235
const newValue = DateTime.fromJSDate(date);
@@ -67,6 +60,12 @@ const CalendarPortal = (calendarProps: CalendarProps) => {
6760
portalActive.focus();
6861
portalActive.blur();
6962
}
63+
// Check value when click on calendar to parse it if its not a date
64+
setCalendarState(
65+
DateTime.isDateTime(contextValue.value)
66+
? contextValue.value.toJSDate()
67+
: new Date()
68+
);
7069
setShowCalendar(!showCalendar);
7170
}
7271
}

src/helpers/VaultManagement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const noBreakSpace = /\u00A0/g;
1717
* @param data
1818
* @returns
1919
*/
20-
function hasFrontmatterKey(data: string): boolean {
20+
export function hasFrontmatterKey(data: string): boolean {
2121
const frontmatterRegex = /^---\n+.*---\n/g
2222
return frontmatterRegex.test(data);
2323
}

src/services/DataviewService.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,35 @@ class DataviewProxy {
6262
}
6363

6464
parseLiteral(literal: Literal | NoteInfo, dataTypeDst: string): Literal | NoteInfo {
65-
let parsedLiteral: Literal | NoteInfo;
65+
if (literal === null || literal === undefined) return "";
66+
let parsedLiteral: Literal | NoteInfo = literal;
6667
// Check empty or undefined literals
6768
switch (dataTypeDst) {
6869
case DataTypes.CALENDAR:
69-
// Check if original literal is a date. If not convert to current date
70-
parsedLiteral = DateTime.isDateTime(literal) ? literal : DateTime.now();
70+
switch (typeof literal) {
71+
case "string":
72+
parsedLiteral = DateTime.fromISO(literal);
73+
break;
74+
}
7175
break;
7276
case DataTypes.NUMBER:
7377
parsedLiteral = isNaN(literal as any) ? ""
7478
: Number.parseInt(literal as string);
7579
break;
7680
default:
77-
parsedLiteral = DateTime.isDateTime(literal) ? literal.toFormat("yyyy-MM-dd")
78-
: literal;
79-
81+
// TODO Values of dataview fails when Obsidian is loaded
82+
switch (typeof literal) {
83+
case 'object':
84+
if (DateTime.isDateTime(literal) && typeof literal.toFormat === 'function') {
85+
parsedLiteral = literal.toFormat("yyyy-MM-dd");
86+
} else {
87+
parsedLiteral = literal.toString();
88+
}
89+
}
8090
}
8191
return parsedLiteral;
8292
}
93+
8394
/**
8495
* Singleton instance
8596
* @returns {VaultManager}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"baseUrl": "src",
4-
"inlineSourceMap": true,
54
"inlineSources": true,
5+
"sourceMap": true,
66
"module": "ESNext",
77
"target": "es6",
88
"allowJs": true,

0 commit comments

Comments
 (0)