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

Commit af7225a

Browse files
authored
Merge pull request #71 from trey-wallis/dev
Updates for Obsidian plugin store
2 parents a6394b7 + ee31ec6 commit af7225a

30 files changed

+586
-409
lines changed

.readme/add-table-command.png

52.2 KB
Loading

.readme/cell-error-1.png

-7.1 KB
Loading

.readme/cell-error-2.png

-49.1 KB
Loading

.readme/header.png

92.7 KB
Loading

.readme/internal-link-edit.png

104 KB
Loading

.readme/markdown.png

17.2 KB
Loading

.readme/preview.png

-17.8 KB
Loading

.readme/table-id-row.png

209 KB
Loading

.readme/tag-menu.png

210 KB
Loading

.readme/text-link-1.png

-72.3 KB
Binary file not shown.

.readme/text-link-2.png

-70.6 KB
Binary file not shown.

.readme/type-def-error-1.png

-215 KB
Binary file not shown.

.readme/type-def-error-2.png

-41.2 KB
Binary file not shown.

.readme/type-definition-row.png

202 KB
Loading

.readme/url.png

72 KB
Loading

README.md

+52-25
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,106 @@ Obsidian Notion-Like Tables allows you to create markdown tables using an interf
99
## Roadmap
1010

1111
- 0.1.0
12-
- MVP
12+
- MVP (basic NLT table)
1313
- 0.2.0
14-
- Semantic table tags
14+
- Semantic table tags for theming (table, tr, td, etc)
1515
- 0.3.0
16-
- Notion-Like sort menu
16+
- Notion-Like sort menu (default, ascending, descending)
1717
- 1.0.0
1818
- Settings cache system to persist app data
19-
- Added jest tests
2019
- Sorting updates source markdown
2120
- 1.1.0
2221
- Support for http/https links
2322
- 1.2.0
2423
- UI updates
2524
- Insert row above or below an existing row
26-
- 1.3.0
25+
- 2.0.0
26+
- Table id row support & upgrading caching system
27+
- 2.1.0
2728
- Create new row with keyboard
28-
- Add new tag colors
29-
- 1.4.0
29+
- Insert new column
30+
- New tag colors
31+
- 2.2.0
3032
- Navigate cells with arrow key and tab key
31-
- 1.5.0
33+
- 2.3.0
3234
- TBA
3335

3436
## Usage
3537

3638
### Making a Table via Command
3739

38-
To quickly make a table you can use the add table command. Press `CMD-P` on your keyboard search "Add table".
40+
To quickly make a table you can use the add table command. Press `cmd + p` on your keyboard search "Add table".
3941

40-
Note: you must be in editing mode for this command to appear
42+
Note: you must be in editing mode for this command to appear.
4143

42-
Toggle to preview mode and the table will automatically render.
44+
Toggle to reading mode and the table will automatically render.
45+
46+
![Screenshot](.readme/add-table-command.png)
4347

4448
### Making a Table Manually
4549

46-
Make a table manually using normal markdown syntax. Under the hyphen row, specify the types of each column.
50+
A Notion-Like Table uses normal Obsidian table markdown syntax with 2 additional rows:
51+
52+
- A table id row
53+
- A type definition row
54+
55+
#### Table ID Row
56+
57+
The table id row is a normal markdown row with the first column containing a unique string. This string must be unique per table per file. If you use the same id in another file that's fine. The id is used to map a table to its data in the settings. If you change this id, your table will not be able to find its settings and will create new ones. If you omit this id, your table will not be rendered as an NLT table.
58+
59+
![Screenshot](.readme/table-id-row.png)
4760

48-
The plugin currently supports 3 cell types: `text`, `number`, and `tag`.
61+
##### Type Definition Row
4962

50-
![Screenshot](.readme/markdown.png)
63+
The type definition row is a normal markdown row with each column defining the type of data you want that column to accept. The plugin currently supports 3 column types: `text`, `number` and `tag`.
5164

52-
Toggle to preview mode and the table will automatically render.
65+
![Screenshot](.readme/type-definition-row.png)
5366

5467
### Editing Cells
5568

5669
To edit a cell, just click on it. Changes made to the cell will be propagated to the markdown.
5770

71+
#### Tags
72+
73+
Tags have a special notion-like menu that will appear. Tags are scoped to each column of a table. You can type text to filter existing tags and select one. Or you can create a new tag by typing text and clicking "Create New" or pressing enter.
74+
75+
![Screenshot](.readme/tag-menu.png)
76+
5877
### Headers
5978

60-
Click on a header name to edit the header title and change the column type.
79+
Click on a header name to view the header menu. In the header menu you can rename the header, sort your column values or change the header type.
6180

6281
![Screenshot](.readme/header.png)
6382

64-
Please do not edit headers in the markdown once you have rendered your table for the first time. These are what are used to identify a unique table. I plan on finding a better system for this, potentially using the line ID of the table in the document. If you have any suggestions for this, please place it in a feature request.
65-
6683
### Links
6784

6885
If you want to render a link in text, just add square brackets [[My Link]].
6986

70-
![Screenshot](.readme/text-link-1.png)
71-
![Screenshot](.readme/text-link-2.png)
87+
![Screenshot](.readme/internal-link-edit.png)
88+
89+
### URLs
90+
91+
If you want to display a url, type the url making sure it begins with `http://` or `https://`. NLT will automatically render it in the table.
92+
93+
![Screenshot](.readme/url.png)
94+
95+
### Undoing a Type Change
96+
97+
If you accidently change your column to a different type, all data will be erased. It this happens, go to editing mode and use `ctrl+z` or `option-z` on your keyboard to undo the change.
7298

7399
### Errors
74100

101+
#### Not Rendering
102+
103+
If your table is missing a table id row or type definition row then it will not be rendered as a Notion-Like Table. Likewise, if you use an invalid column type other than the accepted types `text`, `number`, or `tag`. The table will not be rendered.
104+
105+
#### Cell Type Errors
106+
75107
A cell type error will occur if you enter data which doesn't match the column data type. Please correct this error in your markdown to continue.
76108

77109
![Screenshot](.readme/cell-error-1.png)
78110
![Screenshot](.readme/cell-error-2.png)
79111

80-
### Live Preview
81-
82-
April 9, 2022:
83-
Tables display with live preview is still being developed in Obsidian. If you're using live preview, please always use the table in "Reading" mode not "Editing" mode.
84-
85112
### Theming
86113

87114
NLT tables uses normal table semantic elements (`table`, `th`, `tr`, `td`, etc) to render. If you wish to edit the display of the table, just style those elements in your CSS.

main.ts

+13-99
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import { Plugin, Editor } from "obsidian";
1+
import { Plugin, Editor, MarkdownView } from "obsidian";
22

33
import { NLTTable } from "src/NLTTable";
44
import { NltSettings, DEFAULT_SETTINGS } from "src/app/services/state";
5-
import { Header } from "src/app/services/state";
6-
import {
7-
findMarkdownTablesFromFileData,
8-
parseTableFromMarkdown,
9-
validTypeDefinitionRow,
10-
findAppData,
11-
hashParsedTable,
12-
mergeAppData,
13-
} from "src/app/services/utils";
5+
import { createEmptyTable, randomTableId } from "src/app/services/utils";
146
export default class NltPlugin extends Plugin {
157
settings: NltSettings;
168

@@ -42,8 +34,8 @@ export default class NltPlugin extends Plugin {
4234

4335
registerFileHandlers() {
4436
//Our persisted data uses a key of the file path and then stores an object mapping
45-
//to a CRC32 key and an AppData object
46-
//If the file we have data for changes, we want to update our cache
37+
//to a table id and an AppData object.
38+
//If the file path changes, we want to update our cache so that the data is still accessible.
4739
this.registerEvent(
4840
this.app.vault.on("rename", (file, oldPath) => {
4941
if (this.settings.appData[oldPath]) {
@@ -55,94 +47,18 @@ export default class NltPlugin extends Plugin {
5547
}
5648
})
5749
);
58-
59-
//The loadAppData and saveAppData functions handle markdown editing caused from the app.
60-
//However if a user updates the source markdown of a table and a cached version already existed,
61-
//since the CRC32 value is different, it will recreate brand new app data. This will cause things
62-
//like the tag colors to change.
63-
//We can avoid this by handling editor changes. If a file that was edited contains a table
64-
//and that table has a reference in the cache, then we should update it with new data.
65-
this.registerEvent(
66-
this.app.workspace.on("editor-change", async (editor) => {
67-
const file = this.app.workspace.getActiveFile();
68-
const markdownTables = findMarkdownTablesFromFileData(
69-
editor.getValue()
70-
);
71-
if (markdownTables.length !== 0) {
72-
markdownTables.forEach((markdownTable) => {
73-
const parsedTable =
74-
parseTableFromMarkdown(markdownTable);
75-
//Validate table
76-
if (!validTypeDefinitionRow(parsedTable)) return;
77-
78-
const headers = parsedTable[0];
79-
80-
//Get the saved entry
81-
if (this.settings.appData[file.path]) {
82-
const savedData = this.settings.appData[file.path];
83-
//Check headers of the save data
84-
Object.entries(savedData).forEach((entry) => {
85-
const [key, value] = entry;
86-
//If the headers match, update the data and the CRC
87-
if (
88-
value.headers.every((header: Header) =>
89-
headers.includes(header.content)
90-
)
91-
) {
92-
const hash = hashParsedTable(parsedTable);
93-
94-
//If you change something in reading mode, we will persist
95-
//that data in the settings cache. However, if you go back
96-
//to editing mode, the editor-change callback will be ran.
97-
//Since the hashes are the same, no data has updated, so we
98-
//don't need to do anything
99-
if (parseInt(key) === hash) return;
100-
101-
const newAppData = findAppData(parsedTable);
102-
const merged = mergeAppData(
103-
this.settings.appData[file.path][key],
104-
newAppData
105-
);
106-
this.settings.appData[file.path][hash] =
107-
merged;
108-
delete this.settings.appData[file.path][
109-
key
110-
];
111-
this.saveSettings();
112-
}
113-
});
114-
}
115-
});
116-
}
117-
})
118-
);
11950
}
12051

12152
registerCommands() {
12253
this.addCommand({
12354
id: "nlt-add-table",
12455
name: "Add table",
12556
editorCallback: (editor: Editor) => {
126-
editor.replaceSelection(this.emptyTable());
57+
editor.replaceSelection(createEmptyTable(randomTableId()));
12758
},
12859
});
12960
}
13061

131-
/**
132-
* Creates a 1 column NLT markdown table
133-
* @returns An NLT markdown table
134-
*/
135-
emptyTable(): string {
136-
const columnName = "Column 1";
137-
const rows = [];
138-
rows[0] = `| ${columnName} |`;
139-
rows[1] = `| ${Array(columnName.length).fill("-").join("")} |`;
140-
rows[2] = `| text ${Array(columnName.length - 3)
141-
.fill(" ")
142-
.join("")} |`;
143-
return rows.join("\n");
144-
}
145-
14662
async loadSettings() {
14763
this.settings = Object.assign(
14864
{},
@@ -163,20 +79,18 @@ export default class NltPlugin extends Plugin {
16379
await this.forcePostProcessorReload();
16480
}
16581

166-
/**
82+
/*
16783
* Forces the post processor to be called again.
16884
* This is necessary for clean up purposes on unload and causing NLT tables
16985
* to be rendered onload.
17086
*/
17187
async forcePostProcessorReload() {
172-
const leaves = [
173-
...this.app.workspace.getLeavesOfType("markdown"),
174-
...this.app.workspace.getLeavesOfType("edit"),
175-
];
176-
for (let i = 0; i < leaves.length; i++) {
177-
const leaf = leaves[i];
178-
this.app.workspace.duplicateLeaf(leaf);
179-
leaf.detach();
180-
}
88+
this.app.workspace.iterateAllLeaves((leaf) => {
89+
const view = leaf.view;
90+
if (view.getViewType() === "markdown") {
91+
if (view instanceof MarkdownView)
92+
view.previewMode.rerender(true);
93+
}
94+
});
18195
}
18296
}

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "notion-like-tables",
33
"name": "Notion-Like Tables",
4-
"version": "1.2.0",
4+
"version": "2.0.0",
55
"minAppVersion": "0.12.0",
66
"description": "Create markdown tables using an interface similar to that found in Notion.so.",
77
"author": "Trey Wallis",

package-lock.json

+2-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-notion-like-tables",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"description": "Notion-like tables for Obsidian.md",
55
"main": "main.js",
66
"scripts": {
@@ -36,7 +36,6 @@
3636
},
3737
"dependencies": {
3838
"@mui/icons-material": "^5.5.0",
39-
"crc-32": "^1.2.2",
4039
"html-react-parser": "^1.4.9",
4140
"react": "^17.0.2",
4241
"react-dom": "^17.0.2",

src/NLTTable.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export class NLTTable extends MarkdownRenderChild {
3131
}
3232

3333
async onload() {
34-
const data = loadAppData(
34+
const { tableId, data } = loadAppData(
3535
this.plugin,
36-
this.app,
3736
this.settings,
3837
this.containerEl,
3938
this.sourcePath
@@ -50,6 +49,7 @@ export class NLTTable extends MarkdownRenderChild {
5049
settings={this.settings}
5150
data={data}
5251
sourcePath={this.sourcePath}
52+
tableId={tableId}
5353
/>
5454
</AppContext.Provider>,
5555
this.el

0 commit comments

Comments
 (0)