mirror of
https://github.com/tmcw/obsidian-freeform.git
synced 2026-07-22 10:10:32 +00:00
Compare commits
No commits in common. "main" and "1.0.2" have entirely different histories.
4 changed files with 11 additions and 147 deletions
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
|
|
@ -11,9 +11,9 @@ jobs:
|
|||
with:
|
||||
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v4
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "20.x"
|
||||
node-version: "18.x"
|
||||
- name: Get Version
|
||||
id: version
|
||||
run: |
|
||||
|
|
|
|||
21
LICENSE
21
LICENSE
|
|
@ -1,21 +0,0 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2024 Tom MacWright
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
119
README.md
119
README.md
|
|
@ -9,10 +9,6 @@ This brings a taste of [Observable](https://observablehq.com/) to
|
|||
[Obsidian](https://obsidian.md/). Some common elements include:
|
||||
|
||||
- You can write blocks of code which run in a light sandboxed environment
|
||||
- You can use `import` statements to import modules from URLs, using services
|
||||
like esm.sh or jsdelivr.
|
||||
- `export` statements are supported too, but have no effect. `require()` is not supported.
|
||||
JSX is also not supported, yet.
|
||||
- There's a `display()` function to show values and elements, just like
|
||||
Observable Framework's [explicit display system](https://observablehq.com/framework/javascript#explicit-display).
|
||||
|
||||
|
|
@ -37,7 +33,7 @@ of this readme for some examples.
|
|||
|
||||
## Demo
|
||||
|
||||
<https://github.com/tmcw/obsidian-freeform/assets/32314/56b4e23a-2837-4a06-84c7-ee35b09c2634>
|
||||
https://github.com/tmcw/obsidian-freeform/assets/32314/56b4e23a-2837-4a06-84c7-ee35b09c2634
|
||||
|
||||
### Examples
|
||||
|
||||
|
|
@ -67,59 +63,10 @@ it with Freeform.
|
|||
}).plot({ height: 400, width }))
|
||||
```
|
||||
|
||||
#### Tracking habits
|
||||
|
||||
Use this to track your habits in a calendar interface! It makes a few assumptions that
|
||||
you'll want to tweak and are noted in the code!
|
||||
|
||||
This assumes that you're tracking habits as boolean properties in your daily notes.
|
||||
|
||||
```js
|
||||
import * as d3 from "https://esm.run/d3@7";
|
||||
import * as Plot from "https://esm.run/@observablehq/plot@0.6.16";
|
||||
|
||||
// NOTE:
|
||||
// I store my daily note in a folder called "01 Daily."
|
||||
// Adjust that. Also adjust habit-scale and habit-gym to whatever
|
||||
// the properties you track your habits are.
|
||||
const items = await window.top.app.plugins.plugins.dataview.api
|
||||
.query(`table habit-scale, habit-gym
|
||||
from "01 Daily"`);
|
||||
|
||||
const parsed = items.value.values.map(row => {
|
||||
// Note: this assumes that your daily note file format is parseable
|
||||
// as dates. Mine are stored like 2024-09-24. Adjust if yours are different!
|
||||
const date = new Date(row[0].path.split('/')[1].replace('.md', ''));
|
||||
return {
|
||||
Date: date,
|
||||
habits: row.slice(1).filter(Boolean).length
|
||||
};
|
||||
});
|
||||
|
||||
document.body.appendChild(Plot.plot({
|
||||
padding: 0,
|
||||
x: {axis: null},
|
||||
y: {tickFormat: Plot.formatWeekday("en", "narrow"), tickSize: 0},
|
||||
fx: {tickFormat: ""},
|
||||
color: {scheme: "Blues", legend: true, label: "Habit completion"},
|
||||
marks: [
|
||||
Plot.cell(parsed, {
|
||||
y: (d) => d3.utcWeek.count(d3.utcYear(d.Date), d.Date),
|
||||
x: (d) => d.Date.getUTCDay(),
|
||||
fx: (d) => d.Date.getUTCFullYear(),
|
||||
fill: (d, i) => d.habits,
|
||||
stroke: d3.interpolateBlues(0.1),
|
||||
title: (d, i) => d.habits,
|
||||
inset: 2
|
||||
})
|
||||
]
|
||||
}))
|
||||
```
|
||||
|
||||
#### Importing a module from esm.sh
|
||||
|
||||
Most npm modules that are compatible with browsers are available from
|
||||
<https://esm.sh/>, jsdelivr, unpkg, or skypack. Observable Plot is an especially
|
||||
https://esm.sh/, jsdelivr, unpkg, or skypack. Observable Plot is an especially
|
||||
tricky one, but most "just work."
|
||||
|
||||
```freeform
|
||||
|
|
@ -139,64 +86,6 @@ like Preact [can work without it](https://preactjs.com/guide/v10/getting-started
|
|||
render(app, document.body);
|
||||
```
|
||||
|
||||
#### Querying DataView
|
||||
|
||||
DataView is accessible via `window.top.app.plugins.plugins.dataview.api`.
|
||||
Here's an example of using it:
|
||||
|
||||
```freeform
|
||||
import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm";
|
||||
|
||||
const items = await window.top.app.plugins.plugins.dataview.api
|
||||
.query(`table price, purchased, color
|
||||
from "03 Stuff"
|
||||
where price and sold = undefined
|
||||
sort purchased desc`);
|
||||
|
||||
const mapped = items.value.values
|
||||
.map((item) => {
|
||||
if (!item[2]) return;
|
||||
return {
|
||||
price: item[1],
|
||||
date: new Date(item[2].toMillis()),
|
||||
};
|
||||
})
|
||||
.filter((r) => r);
|
||||
|
||||
display(Plot.dot(mapped, { y: "price", x: "date" }).plot());
|
||||
```
|
||||
|
||||
If you're doing more than one thing with the DataView API, you will probably
|
||||
want to alias the variable, like
|
||||
|
||||
```js
|
||||
const dv = window.top.app.plugins.plugins.dataview.api
|
||||
```
|
||||
|
||||
Also, note that `Date` objects that you get from DataView queries are
|
||||
originated from the top frame, so some code might not recognize them as Date
|
||||
instances. Recreating them with `new Date`, as in the example above, will
|
||||
fix that issue.
|
||||
|
||||
#### Inserting a stylesheet
|
||||
|
||||
Obsidian's [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) forbids
|
||||
`<link>` elements bringing in external stylesheets. You can work around this
|
||||
by a helper function that fetches external CSS and inlines it into a new
|
||||
style on the page:
|
||||
|
||||
```js
|
||||
async function addStyle(url) {
|
||||
const style = new CSSStyleSheet();
|
||||
style.replaceSync(await fetch(url).then(r => r.text()))
|
||||
document.adoptedStyleSheets = [...document.adoptedStyleSheets, style];
|
||||
}
|
||||
```
|
||||
|
||||
Note that some stylesheets you import this way will have relative references
|
||||
to images or they might import other stylesheets via `@import`, and those things
|
||||
won't work.
|
||||
|
||||
### Notes
|
||||
|
||||
- There is a `width` variable, much like [Observable's](https://observablehq.com/framework/javascript#width), but
|
||||
|
|
@ -205,8 +94,8 @@ won't work.
|
|||
way as it does on any webpage.
|
||||
- Only HTTP ESM imports are supported. This isn't Node.js or Deno - there
|
||||
isn't a node_modules directory, and you don't have short names for dependencies.
|
||||
Thankfully, this usually isn't a problem because you can use <https://esm.sh/>
|
||||
<https://www.jsdelivr.com/> and more to import modules.
|
||||
Thankfully, this usually isn't a problem because you can use https://esm.sh/
|
||||
https://www.jsdelivr.com/ and more to import modules.
|
||||
|
||||
## Components
|
||||
|
||||
|
|
|
|||
14
main.js
14
main.js
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue