Compare commits

...

72 Commits

Author SHA1 Message Date
Riccardo Ferretti
b25152d115 v0.25.11 2024-03-18 13:17:27 +01:00
Riccardo Ferretti
1545079c62 Prepare for release 2024-03-18 13:17:04 +01:00
Riccardo Ferretti
4835164902 Fire onDidUpdate even only after full graph recomputed 2024-03-18 13:16:26 +01:00
Riccardo Ferretti
06efdc2865 v0.25.10 2024-03-18 10:09:31 +01:00
Riccardo Ferretti
b68fd7e138 Prepare for release 2024-03-18 10:09:04 +01:00
Riccardo Ferretti
d8baa2fd36 Fixed graph computation issue 2024-03-18 10:06:18 +01:00
Riccardo Ferretti
7f587095e8 v0.25.9 2024-03-17 20:51:24 +01:00
Riccardo Ferretti
77ad245319 Prepare next release 2024-03-17 20:51:16 +01:00
Riccardo
b892c783da Rename placeholder on note creation so it can update it if necessary (#1344)
* Introduced Location
* Passing a reference to the source link to the create-note command

Also
* Added withTiming fn for performance logging
* Added extra test to check incoming wikilink with sections
* Tweaked creation of vscode URI to also support raw objects
2024-03-17 20:49:11 +01:00
Andrew Thiesen
e4f6259104 Update recipes.md (#1341)
REM foamy-js. https://github.com/yenly/foamy-nextjs has been Archived and is no longer being maintained.
2024-03-10 15:18:37 +01:00
Riccardo Ferretti
aa197239fc v0.25.8 2024-02-21 09:53:40 +01:00
Riccardo Ferretti
8f3c23dd60 Prepare for release 2024-02-21 09:47:23 +01:00
Richard C Yeh
9a027c08ba Bump dateformat from 3.0.3 to 4.5.1. Closes #1324. (#1326) 2024-02-19 10:15:34 +01:00
dependabot[bot]
959d0f1ea1 Bump axios from 1.3.2 to 1.6.1 (#1306)
Bumps [axios](https://github.com/axios/axios) from 1.3.2 to 1.6.1.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md)
- [Commits](https://github.com/axios/axios/compare/v1.3.2...v1.6.1)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-23 19:48:34 +01:00
dependabot[bot]
57e32c4349 Bump @babel/traverse from 7.20.13 to 7.23.2 (#1297)
Bumps [@babel/traverse](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse) from 7.20.13 to 7.23.2.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.23.2/packages/babel-traverse)

---
updated-dependencies:
- dependency-name: "@babel/traverse"
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-23 19:48:10 +01:00
dependabot[bot]
f168f66368 Bump follow-redirects from 1.15.2 to 1.15.4 (#1318)
Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.15.2 to 1.15.4.
- [Release notes](https://github.com/follow-redirects/follow-redirects/releases)
- [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.15.2...v1.15.4)

---
updated-dependencies:
- dependency-name: follow-redirects
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-23 19:47:46 +01:00
Riccardo Ferretti
103ff12b2d v0.25.7 2024-01-16 14:47:01 +01:00
Riccardo Ferretti
96a3afa132 Prepare for release 2024-01-16 14:46:11 +01:00
Riccardo Ferretti
d586e63104 Removed "use" from quick fix list elements 2024-01-16 14:29:54 +01:00
Miguel Angel Bruni Montero
2fba6e9008 Modifies url encoding to target only the filename and skip spaces (#1322) 2024-01-15 20:22:04 +01:00
Riccardo
cdbb965661 Update commands.md 2023-12-15 22:31:54 +01:00
Riccardo Ferretti
0c958e31f6 v0.25.6 2023-12-13 20:01:32 +01:00
Riccardo Ferretti
fd84fcfd74 Preparing next release 2023-12-13 20:01:00 +01:00
allcontributors[bot]
becf495edc add MABruni as a contributor for code (#1313)
* update docs/index.md [skip ci]

* update readme.md [skip ci]

* update .all-contributorsrc [skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-12-13 19:59:11 +01:00
Miguel Angel Bruni Montero
4d99883c03 Fix #1298. Wikilink definitions links not working. (#1311)
* Added encoding to handle special characters when wikilink definitions are generated

* Added tests for wikilinks encoding and updated previous tests to support encoded links
2023-12-13 19:58:36 +01:00
Riccardo Ferretti
8bd679c751 v0.25.5 2023-11-30 19:19:26 +01:00
Riccardo Ferretti
8986ee286c Preparation for next release 2023-11-30 19:19:03 +01:00
Riccardo
51b1af1981 Using note title in preview panel (#1309)
See conversation in https://github.com/foambubble/foam/pull/1150
2023-11-30 19:17:50 +01:00
Daniel Carosone
4276e8043f extension descriptions now reflect .vscode dir (#1302)
* extension descriptions now reflect .vscode dir

including that image pasting is now supported natively

* Markdown AiO is still recommended

---------

Co-authored-by: Daniel Carosone <dan@geek.com.au>
2023-10-26 10:24:51 +02:00
Riccardo Ferretti
8d1e9b15ce Readding markdown-all-in-one in extension list and fixing script 2023-10-26 10:23:45 +02:00
jonathan berger
bfcfad32e8 Add more detail to Support for sections. (#1296)
* Add more detail to Support for `sections`.

* Removed extra quote

---------

Co-authored-by: Riccardo <code@riccardoferretti.com>
2023-10-25 12:53:00 +02:00
allcontributors[bot]
abe18cc961 add dcarosone as a contributor for doc (#1301)
* update docs/index.md [skip ci]

* update readme.md [skip ci]

* update .all-contributorsrc [skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-10-25 11:43:29 +02:00
Riccardo Ferretti
9490aa2dad Fixed typo 2023-10-25 11:36:34 +02:00
Riccardo Ferretti
6c3f4588b3 Explicitly create .vscode folder in sync script 2023-10-25 11:26:07 +02:00
Riccardo Ferretti
21b8c5b827 Updated default extensions and settings for foam docs and template
Also monitoring the folder to trigger the appropriate sync job
2023-10-25 11:22:46 +02:00
Daniel Carosone
c66ed74aea replicate .vscode dir to template (#1300)
fixes #1299

Co-authored-by: Daniel Carosone <dan@geek.com.au>
2023-10-25 11:17:43 +02:00
Riccardo Ferretti
3876811fb6 v0.25.4 2023-10-19 17:41:13 +02:00
Riccardo Ferretti
d9299ee9d4 Prepare for next release 2023-10-19 17:40:29 +02:00
Joe Taber
23e21a62f3 Copy assets to foam-template repo (#1238) 2023-10-12 17:22:38 +02:00
Riccardo
e7c8d5a4eb Added support for linking to sections in current file (#1289)
And improved support for links to sections that need to be slugified
2023-10-12 17:01:13 +02:00
Daniel Wang
3ef1b69b2e Fix embedded notes not generating proper reference links (#1286) 2023-09-25 10:09:13 +02:00
Riccardo Ferretti
5859b2a9c6 v0.25.3 2023-09-07 13:07:26 +02:00
Riccardo Ferretti
54086fdd7e Preparation for next release 2023-09-07 13:07:06 +02:00
Daniel Wang
a308dfd109 Handle a case where the embedded note does not exist (#1283) 2023-09-07 09:03:25 +02:00
Daniel Wang
e327115673 Feat #879: Note Embedding Syntax (#1281)
* Removed deprecated preview.embedNoteInContainer

* Integrate explicit type modifiers to wikilink syntax

* Add to documentation
2023-09-06 00:08:56 +02:00
Riccardo Ferretti
c019767476 Fix #1242 - attachments are not considered in the context of orphan notes
A note that only has outbound links to attachments is now still considered an orphan.
2023-09-03 11:46:31 +02:00
Riccardo Ferretti
5e8b817a82 v0.25.2 2023-09-02 19:02:14 +02:00
Riccardo Ferretti
e0acc0ba8c Updated changelog 2023-09-02 19:01:49 +02:00
Riccardo
df4efc5138 Expand all button in tree views (#1276)
* Added expand all util function

* Added expand-all command for notes explorer

* Added expand-all command for placeholder tree view

* Added expand-all in tags explorer

* Added id field to tree items to make `reveal` work for all items
2023-08-30 16:19:14 +02:00
allcontributors[bot]
1e2b3b1bc3 add thara as a contributor for doc (#1280)
* update docs/index.md [skip ci]

* update readme.md [skip ci]

* update .all-contributorsrc [skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-08-30 16:17:45 +02:00
Daniel Wang
eee364de50 Feat #879: Content embedding style (#1279)
* Add content extractor and enable it as an option for note embed type

* Fix content extractor edgecase where note title has lines above it
2023-08-30 16:17:25 +02:00
Tomochika Hara
3ed2bcd37b Fix deadlink (#1278) 2023-08-30 16:02:53 +02:00
Riccardo Ferretti
797aa9f29a v0.25.1 2023-08-23 19:31:27 +02:00
Riccardo Ferretti
30ab58485c Preparation for next release 2023-08-23 19:31:11 +02:00
Jim Graham
ba98f1990c Add last week's notes to snippet provider (#1248)
Adds snippets for

* `/-monday` -- last monday
* `/-tuesday` -- last tuesday

etc.
2023-08-23 19:26:17 +02:00
Daniel Wang
6c1d6868f7 Feat #879: Different embedding styles prep (#1273)
* Refactor note embedding to be extensible

* Prepare new setting to replace preview.embedNoteInContainer

* Fix wikilink-embed e2e tests

* Improve readability

* Set embedNoteInContainer to null in e2e tests to more closely mimic live state
2023-08-23 19:22:00 +02:00
Riccardo
e773e1ff68 Tags explorer improvements (#1275)
* Updated FolderTreeProvider to allow for values at any level of the tree

* Added utility method "walk" and passing node to createValueTreeItem

* Moved tag explorer to use FolderTreeProvider

* Added path in node info

* Added commands for showing tags in workspace or current file only

* Added support for grouping or flattening tags hierarchy

* Moved e2e test cleanup in the suite so that it also works when running the tests from VS Code debug task

* Added flag for registering commands in tags-explorer (needed for testing :( )
2023-08-22 12:36:32 +02:00
Riccardo
86e2bb1ba0 Improved test cleanup and config management (#1274)
* clean up test workspace before running tests

* improved function that runs code with modified config to properly restore former value
2023-08-21 14:30:21 +02:00
allcontributors[bot]
fc2fb6a0ab add nicholas-l as a contributor for code (#1272)
* update docs/index.md [skip ci]

* update readme.md [skip ci]

* update .all-contributorsrc [skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-08-17 10:44:30 +02:00
Nicholas Latham
956d0119be Update Jest to v29 (#1271)
* Update Jest to v29

* Add stream to workspace test

Without `--stream` in the workspace package, the command ends
successfully without correctly running the tests.
2023-08-17 10:43:51 +02:00
allcontributors[bot]
8ddb6a2d12 add tcheneau as a contributor for doc (#1268)
* update docs/index.md [skip ci]

* update readme.md [skip ci]

* update .all-contributorsrc [skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-08-05 11:49:59 +02:00
Tony Cheneau
fb9447630f Update note-templates.md (#1267)
Fix typo (backticks instead of simple quotes)
2023-08-04 16:07:17 +02:00
Riccardo
06a5988a52 Added support for path parameter in filter (#1250) 2023-07-01 14:08:24 +02:00
Riccardo Ferretti
fac2247382 v0.25.0 2023-06-30 14:12:50 +02:00
Riccardo Ferretti
b089b997bb Preparation for next release 2023-06-30 14:12:30 +02:00
Riccardo
a504054504 Support for multiple file extensions and default extension (#1235)
* Added multiple extension support for markdown provider

* Added default extension support

* Injecting extensions params to FoamWorkspace and MarkdownProvider (to avoid dependencies on non-core code)

* Inject extensions to attachment provider
2023-06-30 14:08:59 +02:00
Riccardo Ferretti
a00d18cfbb Fixed connections panel documentation (fixes @1244) 2023-06-19 13:08:16 +02:00
Riccardo Ferretti
5ca7c3eb52 Fixed #1236 - show full name in "Create note with template" hover 2023-05-23 22:33:08 +02:00
Riccardo
571b6a3528 Add FOAM_TITLE_SAFE variable (#1232)
* Introduced FOAM_TITLE_SAFE, which is the FOAM_TITLE variable with all the invalid path characters replaced by '-'

* (out of scope) In notes explorer show item description only when name is different from note title
2023-05-22 17:14:21 +02:00
Riccardo Ferretti
b6c9eac86c Notes explorer ignores untitled documents 2023-05-22 15:56:44 +02:00
dependabot[bot]
557330413c Bump yaml from 1.10.2 to 2.2.2 (#1210)
Bumps [yaml](https://github.com/eemeli/yaml) from 1.10.2 to 2.2.2.
- [Release notes](https://github.com/eemeli/yaml/releases)
- [Commits](https://github.com/eemeli/yaml/compare/v1.10.2...v2.2.2)

---
updated-dependencies:
- dependency-name: yaml
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-05-21 18:48:07 +02:00
Riccardo
20894a1166 Connections panel tweaks (#1233)
* Renamed commands and labels in connections panel

* renamed backlinks.ts -> connections.ts

* (out of scope) In notes explorer show item description only when name is different from note title
2023-05-21 18:37:53 +02:00
72 changed files with 2960 additions and 494 deletions

View File

@@ -1031,8 +1031,54 @@
"contributions": [
"tool"
]
},
{
"login": "tcheneau",
"name": "Tony Cheneau",
"avatar_url": "https://avatars.githubusercontent.com/u/952059?v=4",
"profile": "https://amnesiak.org/me",
"contributions": [
"doc"
]
},
{
"login": "nicholas-l",
"name": "Nicholas Latham",
"avatar_url": "https://avatars.githubusercontent.com/u/12977174?v=4",
"profile": "https://github.com/nicholas-l",
"contributions": [
"code"
]
},
{
"login": "thara",
"name": "Tomochika Hara",
"avatar_url": "https://avatars.githubusercontent.com/u/1532891?v=4",
"profile": "https://thara.dev",
"contributions": [
"doc"
]
},
{
"login": "dcarosone",
"name": "Daniel Carosone",
"avatar_url": "https://avatars.githubusercontent.com/u/11495017?v=4",
"profile": "https://github.com/dcarosone",
"contributions": [
"doc"
]
},
{
"login": "MABruni",
"name": "Miguel Angel Bruni Montero",
"avatar_url": "https://avatars.githubusercontent.com/u/100445384?v=4",
"profile": "https://github.com/MABruni",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
"skipCi": true
"skipCi": true,
"commitType": "docs"
}

View File

@@ -6,6 +6,8 @@ on:
- master
paths:
- docs/user/**/*
- docs/.vscode/**/*
- docs/assets/**/*
workflow_dispatch:
jobs:
@@ -20,11 +22,15 @@ jobs:
- uses: actions/checkout@v3
with:
path: foam
- name: Copy and fixup user docs files
- name: Copy and fixup user docs files
id: copy
run: |
rm -r foam-template/docs
rm -r foam-template/assets
rm -r foam-template/.vscode
cp -r foam/docs/user foam-template/docs
cp -r foam/docs/assets foam-template/assets
cp -r foam/docs/.vscode foam-template/.vscode
# Strip autogenerated wikileaks references because
# they are not an appropriate default user experience.

View File

@@ -5,17 +5,11 @@
// Foam's own extension
"foam.foam-vscode",
// Prettier for auto formatting code
"esbenp.prettier-vscode",
// GitLens for seeing version history inline
"eamodio.gitlens",
// Tons of markdown goodies (lists, tables of content, so much more)
"yzhang.markdown-all-in-one",
// Graph visualizer
"tchayen.markdown-links",
// Prettier for auto formatting code
"esbenp.prettier-vscode",
// Understated grayscale theme (light and dark variants)
"philipbe.theme-gray-matter"

View File

@@ -1,4 +0,0 @@
{
"purpose": "this file exists to tell the foam-vscode plugin that it's currently in a foam workspace",
"future": "we may use this for custom configuration"
}

View File

@@ -3,6 +3,6 @@
[
{
"key": "cmd+shift+n",
"command": "vscodeMarkdownNotes.newNote"
"command": "foam-vscode.create-note"
}
]

View File

@@ -5,7 +5,6 @@
"editor.overviewRulerBorder": false,
"editor.lineHeight": 24,
"foam.edit.linkReferenceDefinitions": "withExtensions",
"vscodeMarkdownNotes.noteCompletionConvention": "noExtension",
"[markdown]": {
"editor.quickSuggestions": {
"other": true,
@@ -13,21 +12,11 @@
"strings": false
}
},
"cSpell.language": "en-GB",
"git.enableSmartCommit": true,
"git.postCommitCommand": "sync",
"spellright.language": [
"en"
],
"spellright.documentTypes": [
"markdown",
"plaintext"
],
"files.exclude": {
"_site/**": true
},
"files.insertFinalNewline": true,
"markdown.styles": [
".vscode/custom-tag-style.css"
]
"markdown.styles": [".vscode/custom-tag-style.css"]
}

View File

@@ -1 +0,0 @@
Backlinking

Binary file not shown.

After

Width:  |  Height:  |  Size: 568 KiB

View File

@@ -93,7 +93,7 @@ After you have made your changes to your copy of the project, it is time to try
1. Return to the project's [home repository page](https://github.com/foambubble/foam).
2. Github should show you an button called "Compare & pull request" linking your forked repository to the community repository.
3. Click that button and confirm that your repository is going to be merged into the community repository. See [this guide](https://sqldbawithabeard.com/2019/11/29/how-to-fork-a-github-repository-and-contribute-to-an-open-source-project/) for more specifics.
3. Click that button and confirm that your repository is going to be merged into the community repository. See [this guide](https://blog.robsewell.com/blog/how-to-fork-a-github-repository-and-contribute-to-an-open-source-project/) for more specifics.
4. Add as many relevant details to the PR message to make it clear to the project maintainers and other members of the community what you have accomplished with your new changes. Link to any issues the changes are related to.
5. Your PR will then need to be reviewed and accepted by the other members of the community. Any discussion about the changes will occur in your PR thread.
6. Once reviewed and accept you can complete the merge request!

View File

@@ -251,6 +251,13 @@ If that sounds like something you're interested in, I'd love to have you along o
<td align="center" valign="top" width="14.28%"><a href="http://www.jim-graham.net/"><img src="https://avatars.githubusercontent.com/u/430293?v=4?s=60" width="60px;" alt="Jim Graham"/><br /><sub><b>Jim Graham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jimgraham" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://t.me/littlepoint"><img src="https://avatars.githubusercontent.com/u/7611700?v=4?s=60" width="60px;" alt="Zhizhen He"/><br /><sub><b>Zhizhen He</b></sub></a><br /><a href="#tool-hezhizhen" title="Tools">🔧</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://amnesiak.org/me"><img src="https://avatars.githubusercontent.com/u/952059?v=4?s=60" width="60px;" alt="Tony Cheneau"/><br /><sub><b>Tony Cheneau</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=tcheneau" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/nicholas-l"><img src="https://avatars.githubusercontent.com/u/12977174?v=4?s=60" width="60px;" alt="Nicholas Latham"/><br /><sub><b>Nicholas Latham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=nicholas-l" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://thara.dev"><img src="https://avatars.githubusercontent.com/u/1532891?v=4?s=60" width="60px;" alt="Tomochika Hara"/><br /><sub><b>Tomochika Hara</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=thara" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dcarosone"><img src="https://avatars.githubusercontent.com/u/11495017?v=4?s=60" width="60px;" alt="Daniel Carosone"/><br /><sub><b>Daniel Carosone</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=dcarosone" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/MABruni"><img src="https://avatars.githubusercontent.com/u/100445384?v=4?s=60" width="60px;" alt="Miguel Angel Bruni Montero"/><br /><sub><b>Miguel Angel Bruni Montero</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=MABruni" title="Code">💻</a></td>
</tr>
</tbody>
</table>

View File

@@ -1,10 +1,11 @@
# Backlinking
When using [[wikilinks]], you can find all notes that link to a specific note in the **Backlinks Explorer**
When using [[wikilinks]], you can find all notes that link to a specific note in the **Connections Explorer**
- Run `Cmd` + `Shift` + `P` (`Ctrl` + `Shift` + `P` for Windows), type "backlinks" and run the **Explorer: Focus on Backlinks** view.
- Run `Cmd` + `Shift` + `P` (`Ctrl` + `Shift` + `P` for Windows), type "connections" and run the **Explorer: Focus on Connections** view.
- Keep this pane always visible to discover relationships between your thoughts
- You can drag the backlinks pane to a different section in VS Code if you prefer. See: [[make-backlinks-more-prominent]]
- You can drag the connections panel to a different section in VS Code if you prefer. See: [[make-backlinks-more-prominent]]
- You can filter the connections to see just backlinks, forward links, or all connections
- Finding backlinks in published Foam workspaces via [[materialized-backlinks]] is on the [[roadmap]] but not yet implemented.
[//begin]: # "Autogenerated link references for markdown compatibility"

View File

@@ -0,0 +1,29 @@
# Built-In Note Embedding Types
When embedding a note, there are a few ways to modify the scope of the content as well as its display style. The following are Foam keywords that are used to describe note embedding.
Note, this only applies to note embedding, not embedding of attachments or images.
![Note Embed Types GIF](../../assets/images/note-embed-type-demo.gif)
## Scope
- `full` - the entire note in the case of `![[note]]` or the entire section in the case of `![[note#section1]]`
- `content` - everything excluding the title of the section. So the entire note minus the title for `![[note]]`, or the entire section minus the section header for `![[note#section1]]`
## Style
- `card` - outlines the embedded note with a border
- `inline` - adds the note continuously as if the text were part of the calling note
## Default Setting
Foam expresses note display type as `<scope>-<style>`.
By default, Foam configures note embedding to be `full-card`. That is, whenever the standard embedding syntax is used, `![[note]]`, the note will have `full` scope and `card` style display. This setting is stored under `foam.preview.embedNoteStyle` and can be modified.
## Explicit Modifiers
Prepend the wikilink with one of the scope or style keywords, or a combination of the two to explicitly modify a note embedding if you would like to override the default setting.
For example, given your `foam.embedNoteStyle` is set to `content-card`, embedding a note with standard syntax `![[note-a]]` would show a bordered note without its title. Say, for a specific `note-b` you would like to display the title. You can simply use one of the above keywords to override your default setting like so: `full![[note-b]]`. In this case, `full` overrides the default `content` scope and because a style is not specified, it falls back to the default style setting, `card`. If you would like it to be inline, override that as well: `full-inline![[note-b]]`.

View File

@@ -10,13 +10,13 @@ This command creates a note.
Although it works fine on its own, it can be customized to achieve various use cases.
Here are the settings available for the command:
- notePath: The path of the note to create. If relative it will be resolved against the workspace root.
- templatePath: The path of the template to use. If relative it will be resolved against the workspace root.
- title: The title of the note (that is, the `FOAM_TITLE` variable)
- text: The text to use for the note. If also a template is provided, the template has precedence
- variables: Variables to use in the text or template
- date: The date used to resolve the FOAM*DATE*\* variables. in `YYYY-MM-DD` format
- onFileExists?: 'overwrite' | 'open' | 'ask' | 'cancel': What to do in case the target file already exists
- `notePath`: The path of the note to create. If relative it will be resolved against the workspace root.
- `templatePath`: The path of the template to use. If relative it will be resolved against the workspace root.
- `title`: The title of the note (that is, the `FOAM_TITLE` variable)
- `text`: The text to use for the note. If also a template is provided, the template has precedence
- `variables`: Variables to use in the text or template
- `date`: The date used to resolve the FOAM*DATE*\* variables. in `YYYY-MM-DD` format
- `onFileExists?: 'overwrite' | 'open' | 'ask' | 'cancel'`: What to do in case the target file already exists
To customize a command and associate a key binding to it, open the key binding settings and add the appropriate configuration, here are some examples:

View File

@@ -8,13 +8,16 @@ Including a note can be done by adding an `!` before a wikilink definition. For
## Custom styling
Displaying the inclusion of notes allows for some custom styling, see [[custom-markdown-preview-styles]]
To modify how an embedded note looks and the scope of its content, see [[built-in-note-embedding-types]]
For more fine-grained custom styling, see [[custom-markdown-preview-styles]]
## Future possibilities
Work on this feature is evolving and progressing. See the [[inclusion-of-notes]] proposal for the current discussion.
[//begin]: # "Autogenerated link references for markdown compatibility"
[custom-markdown-preview-styles]: custom-markdown-preview-styles.md "Custom Markdown Preview Styles"
[inclusion-of-notes]: ../../dev/proposals/inclusion-of-notes.md "Inclusion of notes Proposal "
[//end]: # "Autogenerated link references"
[//begin]: # 'Autogenerated link references for markdown compatibility'
[built-in-note-embedding-types]: built-in-note-embedding-types.md 'Built-In Note Embedding Types'
[custom-markdown-preview-styles]: custom-markdown-preview-styles.md 'Custom Markdown Preview Styles'
[inclusion-of-notes]: ../../dev/proposals/inclusion-of-notes.md 'Inclusion of notes Proposal '
[//end]: # 'Autogenerated link references'

View File

@@ -58,6 +58,7 @@ In addition, you can also use variables provided by Foam:
| -------------------- | ------------ |
| `FOAM_SELECTED_TEXT` | Foam will fill it with selected text when creating a new note, if any text is selected. Selected text will be replaced with a wikilink to the new |
| `FOAM_TITLE` | The title of the note. If used, Foam will prompt you to enter a title for the note. |
| `FOAM_TITLE_SAFE` | The title of the note in a file system safe format. If used, Foam will prompt you to enter a title for the note unless `FOAM_TITLE` has already caused the prompt. |
| `FOAM_SLUG` | The sluggified title of the note (using the default github slug method). If used, Foam will prompt you to enter a title for the note unless `FOAM_TITLE` has already caused the prompt. |
| `FOAM_DATE_*` | `FOAM_DATE_YEAR`, `FOAM_DATE_MONTH`, `FOAM_DATE_WEEK` etc. Foam-specific versions of [VS Code's datetime snippet variables](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables). Prefer these versions over VS Code's. |
@@ -193,7 +194,7 @@ You can add the template metadata to its own YAML Frontmatter block at the start
foam_template:
name: My Note Template
description: This is my note template
filepath: `journal/$FOAM_TITLE.md`
filepath: 'journal/$FOAM_TITLE.md'
---
This is the rest of the template
```
@@ -205,7 +206,7 @@ If the note already has a Frontmatter block, a Foam-specific Frontmatter block c
foam_template:
name: My Note Template
description: This is my note template
filepath: `journal/$FOAM_TITLE.md`
filepath: 'journal/$FOAM_TITLE.md'
---
---

View File

@@ -20,7 +20,10 @@ Remember, with `CTRL/CMD+click` on a wikilink you can navigate to the note, or c
## Support for sections
Foam supports autocompletion, navigation, embedding and diagnostics for note sections. Just use the standard wiki syntax of `[[resource#Section Title]]`.
Foam supports autocompletion, navigation, embedding and diagnostics for note sections. Just use the standard wiki syntax of `[[resource#Section Title]]`.
- If it's an external file, `[your link will need the filename](other-file.md#that-section-I-want-to-link-to)`, but
- if it's an anchor within the same document, `[you just need an octothorpe and the section name](#that-section-above)`.
- Doesn't matter what heading-level the anchor is; whether you're linking to an `H1` like `# MEN WALK ON MOON` or an `H2` like `## Astronauts Land on Plain`, the link syntax uses a single octothorpe: `[Walk!](#men-walk-on-moon)` and `[Land!](#astronauts-land-on-plain-collect-rocks-plant-flag)`. Autocomplete is your friend here.
## Markdown compatibility

View File

@@ -6,11 +6,11 @@ This list is subject to change.
- [Foam for VSCode](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode) (alpha)
- [Markdown All In One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one)
- [Paste Image](https://marketplace.visualstudio.com/items?itemName=mushan.vscode-paste-image)
- [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
## Extensions For Additional Features
These extensions are not (yet?) defined in `.vscode/extensions.json`, but have been used by others and shown to play nice with Foam.
These extensions are not defined in `.vscode/extensions.json`, but have been used by others and shown to play nice with Foam.
- [Emojisense](https://marketplace.visualstudio.com/items?itemName=bierner.emojisense)
- [Markdown Emoji](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-emoji) (adds `:smile:` syntax, works with emojisense to provide autocomplete for this syntax)

View File

@@ -2,8 +2,12 @@
This #recipe allows you to paste images on to your notes.
You can directly link and paste images that are copied to the clipboard using either the [Paste
Image](https://marketplace.visualstudio.com/items?itemName=mushan.vscode-paste-image)
extension, or the [Markdown Image](https://marketplace.visualstudio.com/items?itemName=hancel.markdown-image) extension.
VScode (since
[1.79](https://code.visualstudio.com/updates/v1_79#_copy-external-media-files-into-workspace-on-drop-or-paste-for-markdown))
now has the ability to paste images from the clipboard, or drag-and-drop image
files, directly into markdown documents. The file will be created in the
workspace, and a link generated in Markdown format.
The former does not have MDX support (yet), the latter does.
VSCode settings under `Markdown > Copy Files` and `Markdown > Editor > Drop` can
be used to configure where the files get placed in your workspace, how they're
named, how conflicts with existing files are handled, and more.

View File

@@ -75,7 +75,7 @@ A #recipe is a guide, tip or strategy for getting the most out of your Foam work
- Publish using community templates
- [[publish-to-netlify-with-eleventy]] by [@juanfrank77](https://github.com/juanfrank77)
- [[generate-gatsby-site]] by [@mathieudutour](https://github.com/mathieudutour) and [@hikerpig](https://github.com/hikerpig)
- [foamy-nextjs](https://github.com/yenly/foamy-nextjs) by [@yenly](https://github.com/yenly)
- Make the site your own by [[publish-to-github]].
- Render math symbols, by either
- adding client-side [[math-support-with-mathjax]] to the default [[publish-to-github-pages]] site

View File

@@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.24.0"
"version": "0.25.11"
}

View File

@@ -16,7 +16,7 @@
"reset": "yarn && yarn clean && yarn build",
"clean": "lerna run clean",
"build": "lerna run build",
"test": "yarn workspace foam-vscode test",
"test": "yarn workspace foam-vscode test --stream",
"lint": "lerna run lint",
"watch": "lerna run watch --concurrency 20 --stream"
},

View File

@@ -4,6 +4,96 @@ All notable changes to the "foam-vscode" extension will be documented in this fi
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [0.25.11] - 2024-03-18
Fixes and Improvements:
- Actually fixed bug in graph computation (#1345)
## [0.25.10] - 2024-03-18
Fixes and Improvements:
- Fixed bug in graph computation (#1345)
## [0.25.9] - 2024-03-17
Fixes and Improvements:
- Improved note creation from placeholder (#1344)
## [0.25.8] - 2024-02-21
Fixes and Improvements:
- Upgraded dataformat to improve support for daily note naming (#1326 - thanks @rcyeh)
## [0.25.7] - 2024-01-16
Fixes and Improvements:
- Modifies url encoding to target only the filename and skip spaces (#1322 - thanks @MABruni)
- Minor tweak to quick action menu with suggestions for section name
## [0.25.6] - 2023-12-13
Fixes and Improvements:
- Fixed wikilink definition encoding (#1311 - thanks @MABruni)
## [0.25.5] - 2023-11-30
Fixes and Improvements:
- Using note title in preview (#1309)
## [0.25.4] - 2023-09-19
Fixes and Improvements:
- Added support for linking sections within same document (#1289)
- Fixed note embedding bug (#1286 - thanks @badsketch)
## [0.25.3] - 2023-09-07
Fixes and Improvements:
- Fixed incorrect handling of embedding of non-existing notes (#1283 - thanks @badsketch)
- Introduced Note Embedding Sytanx (#1281 - thanks @badsketch)
- Attachments are not considered when computing orphan notes (#1242)
## [0.25.2] - 2023-09-02
Fixes and Improvements:
- Added content-only embed styles (#1279 - thanks @badsketch)
- Added expand-all button to tree views (#1276)
## [0.25.1] - 2023-08-23
Fixes and Improvements:
- Added support for path parameter in filter (#1250)
- Added grouping and filtering to tag explorer (#1275)
- Added new setting to control note embedding (#1273 - thanks @badsketch)
- Added last week's days to snippets (#1248 - thanks @jimgraham)
Internal:
- Updated jest to v29 (#1271 - thanks @nicholas-l)
- Improved test cleanup and management (#1274)
## [0.25.0] - 2023-06-30
Features:
- Support for multiple extensions and custom default extension (#1235)
- Added `FOAM_TITLE_SAFE` template variable (#1232)
Fixes and Improvements:
- Connections panel tweaks (#1233)
## [0.24.0] - 2023-05-19
Features:

View File

@@ -8,7 +8,7 @@
"type": "git"
},
"homepage": "https://github.com/foambubble/foam",
"version": "0.24.0",
"version": "0.25.11",
"license": "MIT",
"publisher": "foam",
"engines": {
@@ -109,17 +109,17 @@
"view/title": [
{
"command": "foam-vscode.views.connections.show:backlinks",
"when": "view == foam-vscode.connections && foam-vscode.views.connections.show == 'connections'",
"when": "view == foam-vscode.connections && foam-vscode.views.connections.show == 'all links'",
"group": "navigation"
},
{
"command": "foam-vscode.views.connections.show:links",
"command": "foam-vscode.views.connections.show:forward-links",
"when": "view == foam-vscode.connections && foam-vscode.views.connections.show == 'backlinks'",
"group": "navigation"
},
{
"command": "foam-vscode.views.connections.show:connections",
"when": "view == foam-vscode.connections && foam-vscode.views.connections.show == 'links'",
"command": "foam-vscode.views.connections.show:all-links",
"when": "view == foam-vscode.connections && foam-vscode.views.connections.show == 'forward links'",
"group": "navigation"
},
{
@@ -132,6 +132,31 @@
"when": "view == foam-vscode.orphans && foam-vscode.views.orphans.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.show == 'all'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.show == 'for-current-file'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.group-by == 'off'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"when": "view == foam-vscode.tags-explorer",
"group": "navigation"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.show == 'all'",
@@ -152,6 +177,11 @@
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"when": "view == foam-vscode.placeholders",
"group": "navigation"
},
{
"command": "foam-vscode.views.notes-explorer.show:notes",
"when": "view == foam-vscode.notes-explorer && foam-vscode.views.notes-explorer.show == 'all'",
@@ -161,6 +191,11 @@
"command": "foam-vscode.views.notes-explorer.show:all",
"when": "view == foam-vscode.notes-explorer && foam-vscode.views.notes-explorer.show == 'notes-only'",
"group": "navigation"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"when": "view == foam-vscode.notes-explorer",
"group": "navigation"
}
],
"commandPalette": [
@@ -173,7 +208,7 @@
"when": "false"
},
{
"command": "foam-vscode.views.connections.show:connections",
"command": "foam-vscode.views.connections.show:all-links",
"when": "false"
},
{
@@ -181,7 +216,7 @@
"when": "false"
},
{
"command": "foam-vscode.views.connections.show:links",
"command": "foam-vscode.views.connections.show:forward-links",
"when": "false"
},
{
@@ -192,6 +227,26 @@
"command": "foam-vscode.views.orphans.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"when": "false"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "false"
@@ -208,6 +263,10 @@
"command": "foam-vscode.views.placeholders.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"when": "false"
},
{
"command": "foam-vscode.views.notes-explorer.show:all",
"when": "false"
@@ -216,6 +275,10 @@
"command": "foam-vscode.views.notes-explorer.show:notes",
"when": "false"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"when": "false"
},
{
"command": "foam-vscode.open-resource",
"when": "false"
@@ -294,12 +357,12 @@
"icon": "$(arrow-left)"
},
{
"command": "foam-vscode.views.connections.show:links",
"command": "foam-vscode.views.connections.show:forward-links",
"title": "Show Links",
"icon": "$(arrow-right)"
},
{
"command": "foam-vscode.views.connections.show:connections",
"command": "foam-vscode.views.connections.show:all-links",
"title": "Show All",
"icon": "$(arrow-swap)"
},
@@ -308,6 +371,31 @@
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"title": "Show tags in current file",
"icon": "$(file)"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"title": "Show tags in workspace",
"icon": "$(files)"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"title": "Group By Folder",
"icon": "$(list-tree)"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"title": "Show placeholders in current file",
@@ -328,11 +416,21 @@
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.notes-explorer.show:all",
"title": "Show all resources",
"icon": "$(files)"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.notes-explorer.show:notes",
"title": "Show only notes",
@@ -395,6 +493,16 @@
"default": "pdf mp3 webm wav m4a mp4 avi mov rtf txt doc docx pages xls xlsx numbers ppt pptm pptx",
"description": "Space separated list of file extensions that will be considered attachments"
},
"foam.files.notesExtensions": {
"type": "string",
"default": "",
"description": "Space separated list of extra file extensions that will be considered text notes (e.g. 'mdx txt markdown')"
},
"foam.files.defaultNoteExtension": {
"type": "string",
"default": "md",
"description": "The default extension for new notes"
},
"foam.files.newNotePath": {
"type": "string",
"default": "root",
@@ -501,10 +609,21 @@
],
"description": "Whether or not to navigate to the target daily note when a daily note snippet is selected."
},
"foam.preview.embedNoteInContainer": {
"type": "boolean",
"default": true,
"description": "Wrap embedded notes in a container when displayed in preview panel"
"foam.preview.embedNoteType": {
"type": "string",
"default": "full-card",
"enum": [
"full-inline",
"full-card",
"content-inline",
"content-card"
],
"enumDescriptions": [
"Include the section with title and style inline",
"Include the section with title and style it within a container",
"Include the section without title and style inline",
"Include the section without title and style it within a container"
]
},
"foam.graph.titleMaxLength": {
"type": "number",
@@ -551,7 +670,7 @@
},
"devDependencies": {
"@types/dateformat": "^3.0.1",
"@types/jest": "^27.5.1",
"@types/jest": "^29.5.3",
"@types/lodash": "^4.14.157",
"@types/markdown-it": "^12.0.1",
"@types/micromatch": "^4.0.1",
@@ -568,19 +687,19 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"husky": "^4.2.5",
"jest": "^27.5.1",
"jest": "^29.6.2",
"jest-extended": "^3.2.3",
"markdown-it": "^12.0.4",
"micromatch": "^4.0.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.5",
"ts-jest": "^29.1.1",
"tslib": "^2.0.0",
"typescript": "^4.9.5",
"vscode-test": "^1.3.0",
"wait-for-expect": "^3.0.2"
},
"dependencies": {
"dateformat": "^3.0.3",
"dateformat": "4.5.1",
"detect-newline": "^3.1.0",
"github-slugger": "^1.4.0",
"gray-matter": "^4.0.2",
@@ -593,7 +712,7 @@
"title-case": "^3.0.2",
"unified": "^9.0.0",
"unist-util-visit": "^2.0.2",
"yaml": "^1.10.0"
"yaml": "^2.2.2"
},
"__metadata": {
"id": "b85c6625-454b-4b61-8a22-c42f3d0f2e1e",

View File

@@ -5,7 +5,7 @@ import { FoamGraph } from './graph';
import { ResourceParser } from './note';
import { ResourceProvider } from './provider';
import { FoamTags } from './tags';
import { Logger } from '../utils/log';
import { Logger, withTiming, withTimingAsync } from '../utils/log';
export interface Services {
dataStore: IDataStore;
@@ -25,25 +25,28 @@ export const bootstrap = async (
watcher: IWatcher | undefined,
dataStore: IDataStore,
parser: ResourceParser,
initialProviders: ResourceProvider[]
initialProviders: ResourceProvider[],
defaultExtension: string = '.md'
) => {
const tsStart = Date.now();
const workspace = await FoamWorkspace.fromProviders(
initialProviders,
dataStore
const workspace = await withTimingAsync(
() =>
FoamWorkspace.fromProviders(
initialProviders,
dataStore,
defaultExtension
),
ms => Logger.info(`Workspace loaded in ${ms}ms`)
);
const tsWsDone = Date.now();
Logger.info(`Workspace loaded in ${tsWsDone - tsStart}ms`);
const graph = withTiming(
() => FoamGraph.fromWorkspace(workspace, true),
ms => Logger.info(`Graph loaded in ${ms}ms`)
);
const graph = FoamGraph.fromWorkspace(workspace, true);
const tsGraphDone = Date.now();
Logger.info(`Graph loaded in ${tsGraphDone - tsWsDone}ms`);
const tags = FoamTags.fromWorkspace(workspace, true);
const tsTagsEnd = Date.now();
Logger.info(`Tags loaded in ${tsTagsEnd - tsGraphDone}ms`);
const tags = withTiming(
() => FoamTags.fromWorkspace(workspace, true),
ms => Logger.info(`Tags loaded in ${ms}ms`)
);
watcher?.onDidChange(async uri => {
if (matcher.isMatch(uri)) {

View File

@@ -139,6 +139,21 @@ describe('Graph', () => {
).toEqual(['/path/another/page-c.md', '/somewhere/page-b.md']);
});
it('should create inbound connections when targeting a section', () => {
const noteA = createTestNote({
uri: '/path/to/page-a.md',
links: [{ slug: 'page-b#section 2' }],
});
const noteB = createTestNote({
uri: '/somewhere/page-b.md',
text: '## Section 1\n\n## Section 2',
});
const ws = createTestWorkspace().set(noteA).set(noteB);
const graph = FoamGraph.fromWorkspace(ws);
expect(graph.getBacklinks(noteB.uri).length).toEqual(1);
});
it('should support attachments', () => {
const noteA = createTestNote({
uri: '/path/to/page-a.md',

View File

@@ -3,7 +3,7 @@ import { ResourceLink } from './note';
import { URI } from './uri';
import { FoamWorkspace } from './workspace';
import { IDisposable } from '../common/lifecycle';
import { Logger } from '../utils/log';
import { Logger, withTiming } from '../utils/log';
import { Emitter } from '../common/event';
export type Connection = {

View File

@@ -0,0 +1,35 @@
import { Range } from './range';
import { URI } from './uri';
import { ResourceLink } from './note';
/**
* Represents a location inside a resource, such as a line
* inside a text file.
*/
export interface Location<T> {
/**
* The resource identifier of this location.
*/
uri: URI;
/**
* The document range of this locations.
*/
range: Range;
/**
* The data associated to this location.
*/
data: T;
}
export abstract class Location<T> {
static create<T>(uri: URI, range: Range, data: T): Location<T> {
return { uri, range, data };
}
static forObjectWithRange<T extends { range: Range }>(
uri: URI,
obj: T
): Location<T> {
return Location.create(uri, obj.range, obj);
}
}

View File

@@ -66,6 +66,10 @@ export abstract class Resource {
return a.title.localeCompare(b.title);
}
public static sortByPath(a: Resource, b: Resource) {
return a.uri.path.localeCompare(b.uri.path);
}
public static isResource(thing: any): thing is Resource {
if (!thing) {
return false;

View File

@@ -107,7 +107,7 @@ describe('Identifier computation', () => {
const third = createTestNote({
uri: '/another/path/for/page-a.md',
});
const ws = new FoamWorkspace().set(first).set(second).set(third);
const ws = new FoamWorkspace('.md').set(first).set(second).set(third);
expect(ws.getIdentifier(first.uri)).toEqual('to/page-a');
expect(ws.getIdentifier(second.uri)).toEqual('way/for/page-a');
@@ -124,7 +124,7 @@ describe('Identifier computation', () => {
const third = createTestNote({
uri: '/another/path/for/page-a.md',
});
const ws = new FoamWorkspace().set(first).set(second).set(third);
const ws = new FoamWorkspace('.md').set(first).set(second).set(third);
expect(ws.getIdentifier(first.uri.withFragment('section name'))).toEqual(
'to/page-a#section name'
@@ -170,7 +170,7 @@ describe('Identifier computation', () => {
});
it('should ignore elements from the exclude list', () => {
const workspace = new FoamWorkspace();
const workspace = new FoamWorkspace('.md');
const noteA = createTestNote({ uri: '/path/to/note-a.md' });
const noteB = createTestNote({ uri: '/path/to/note-b.md' });
const noteC = createTestNote({ uri: '/path/to/note-c.md' });

View File

@@ -22,6 +22,11 @@ export class FoamWorkspace implements IDisposable {
*/
private _resources: Map<string, Resource> = new Map();
/**
* @param defaultExtension: The default extension for notes in this workspace (e.g. `.md`)
*/
constructor(public defaultExtension: string = '.md') {}
registerProvider(provider: ResourceProvider) {
this.providers.push(provider);
}
@@ -67,14 +72,16 @@ export class FoamWorkspace implements IDisposable {
public listByIdentifier(identifier: string): Resource[] {
const needle = normalize('/' + identifier);
const mdNeedle =
getExtension(needle) !== '.md' ? needle + '.md' : undefined;
const resources = [];
getExtension(needle) !== this.defaultExtension
? needle + this.defaultExtension
: undefined;
const resources: Resource[] = [];
for (const key of this._resources.keys()) {
if ((mdNeedle && key.endsWith(mdNeedle)) || key.endsWith(needle)) {
if (key.endsWith(mdNeedle) || key.endsWith(needle)) {
resources.push(this._resources.get(normalize(key)));
}
}
return resources.sort((a, b) => a.uri.path.localeCompare(b.uri.path));
return resources.sort(Resource.sortByPath);
}
/**
@@ -105,7 +112,7 @@ export class FoamWorkspace implements IDisposable {
forResource.path,
amongst.map(uri => uri.path)
);
identifier = changeExtension(identifier, '.md', '');
identifier = changeExtension(identifier, this.defaultExtension, '');
if (forResource.fragment) {
identifier += `#${forResource.fragment}`;
}
@@ -121,7 +128,7 @@ export class FoamWorkspace implements IDisposable {
if (FoamWorkspace.isIdentifier(path)) {
resource = this.listByIdentifier(path)[0];
} else {
const candidates = [path, path + '.md'];
const candidates = [path, path + this.defaultExtension];
for (const candidate of candidates) {
const searchKey = isAbsolute(candidate)
? candidate
@@ -141,7 +148,6 @@ export class FoamWorkspace implements IDisposable {
}
public resolveLink(resource: Resource, link: ResourceLink): URI {
// TODO add tests
for (const provider of this.providers) {
if (provider.supports(resource.uri)) {
return provider.resolveLink(this, resource, link);
@@ -237,9 +243,10 @@ export class FoamWorkspace implements IDisposable {
static async fromProviders(
providers: ResourceProvider[],
dataStore: IDataStore
dataStore: IDataStore,
defaultExtension: string = '.md'
): Promise<FoamWorkspace> {
const workspace = new FoamWorkspace();
const workspace = new FoamWorkspace(defaultExtension);
await Promise.all(providers.map(p => workspace.registerProvider(p)));
const files = await dataStore.list();
await Promise.all(files.map(f => workspace.fetchAndSet(f)));

View File

@@ -3,17 +3,15 @@ import { URI } from '../model/uri';
import { FoamWorkspace } from '../model/workspace';
import { IDisposable } from '../common/lifecycle';
import { ResourceProvider } from '../model/provider';
import { getFoamVsCodeConfig } from '../../services/config';
const attachmentExtConfig = getFoamVsCodeConfig(
'files.attachmentExtensions',
''
)
.split(' ')
.map(ext => '.' + ext.trim());
const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp'];
const attachmentExtensions = [...attachmentExtConfig, ...imageExtensions];
export const imageExtensions = [
'.png',
'.jpg',
'.jpeg',
'.gif',
'.svg',
'.webp',
];
const asResource = (uri: URI): Resource => {
const type = imageExtensions.includes(uri.getExtension())
@@ -34,9 +32,14 @@ const asResource = (uri: URI): Resource => {
export class AttachmentResourceProvider implements ResourceProvider {
private disposables: IDisposable[] = [];
public readonly attachmentExtensions: string[];
constructor(attachmentExtensions: string[] = []) {
this.attachmentExtensions = [...imageExtensions, ...attachmentExtensions];
}
supports(uri: URI) {
return attachmentExtensions.includes(
return this.attachmentExtensions.includes(
uri.getExtension().toLocaleLowerCase()
);
}

View File

@@ -166,6 +166,65 @@ describe('Link resolution', () => {
noteA.uri.withFragment('section')
);
});
it('should resolve wikilinks with special characters', () => {
const ws = createTestWorkspace();
const noteA = createNoteFromMarkdown(
`Link to [[page: a]] and [[page %b%]] and [[page? c]] and [[[page] d]] and
[[page ^e^]] and [[page \`f\`]] and [[page {g}]] and [[page ~i]] and
[[page /j]]`
);
const noteB = createNoteFromMarkdown(
'Note containing :',
'/dir1/page: a.md'
);
const noteC = createNoteFromMarkdown(
'Note containing %',
'/dir1/page %b%.md'
);
const noteD = createNoteFromMarkdown(
'Note containing ?',
'/dir1/page? c.md'
);
const noteE = createNoteFromMarkdown(
'Note containing ]',
'/dir1/[page] d.md'
);
const noteF = createNoteFromMarkdown(
'Note containing ^',
'/dir1/page ^e^.md'
);
const noteG = createNoteFromMarkdown(
'Note containing `',
'/dir1/page `f`.md'
);
const noteH = createNoteFromMarkdown(
'Note containing { and }',
'/dir1/page {g}.md'
);
const noteI = createNoteFromMarkdown(
'Note containing ~',
'/dir1/page ~i.md'
);
ws.set(noteA)
.set(noteB)
.set(noteC)
.set(noteD)
.set(noteE)
.set(noteF)
.set(noteG)
.set(noteH)
.set(noteI);
expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(noteB.uri);
expect(ws.resolveLink(noteA, noteA.links[1])).toEqual(noteC.uri);
expect(ws.resolveLink(noteA, noteA.links[2])).toEqual(noteD.uri);
expect(ws.resolveLink(noteA, noteA.links[3])).toEqual(noteE.uri);
expect(ws.resolveLink(noteA, noteA.links[4])).toEqual(noteF.uri);
expect(ws.resolveLink(noteA, noteA.links[5])).toEqual(noteG.uri);
expect(ws.resolveLink(noteA, noteA.links[6])).toEqual(noteH.uri);
expect(ws.resolveLink(noteA, noteA.links[7])).toEqual(noteI.uri);
});
});
describe('Markdown direct links', () => {
@@ -311,9 +370,78 @@ describe('Generation of markdown references', () => {
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));
const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => r.url)).toEqual([
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir2/page-b.md',
'../dir3/page-c.md',
]);
});
it('should generate links for embedded notes that are formatted properly', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
'Link to ![[page-b]] and [[page-c]]',
'/dir1/page-a.md'
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Content of note B', '/dir2/page-b.md'))
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));
const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => [decodeURIComponent(r.url), r.label])).toEqual([
['../dir2/page-b.md', 'page-b'],
['../dir3/page-c.md', 'page-c'],
]);
});
it('should not generate links for placeholders', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
'Link to ![[page-b]] and [[page-c]] and [[does-not-exist]] and ![[does-not-exist-either]]',
'/dir1/page-a.md'
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Content of note B', '/dir2/page-b.md'))
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));
const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir2/page-b.md',
'../dir3/page-c.md',
]);
});
it('should encode special characters in links', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
`Link to [[page: a]] and [[page %b%]] and [[page? c]] and [[[page] d]] and
[[page ^e^]] and [[page \`f\`]] and [[page {g}]] and [[page ~i]] and
[[page /j]]`
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Note containing :', '/dir1/page: a.md'))
.set(createNoteFromMarkdown('Note containing %', '/dir1/page %b%.md'))
.set(createNoteFromMarkdown('Note containing ?', '/dir1/page? c.md'))
.set(createNoteFromMarkdown('Note containing ]', '/dir1/[page] d.md'))
.set(createNoteFromMarkdown('Note containing ^', '/dir1/page ^e^.md'))
.set(createNoteFromMarkdown('Note containing `', '/dir1/page `f`.md'))
.set(
createNoteFromMarkdown('Note containing { and }', '/dir1/page {g}.md')
)
.set(createNoteFromMarkdown('Note containing ~', '/dir1/page ~i.md'));
const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir1/page: a.md',
'../dir1/page %b%.md',
'../dir1/page? c.md',
'../dir1/[page] d.md',
'../dir1/page ^e^.md',
'../dir1/page `f`.md',
'../dir1/page {g}.md',
'../dir1/page ~i.md',
]);
});
});

View File

@@ -19,11 +19,12 @@ export class MarkdownResourceProvider implements ResourceProvider {
constructor(
private readonly dataStore: IDataStore,
private readonly parser: ResourceParser
private readonly parser: ResourceParser,
public readonly noteExtensions: string[] = ['.md']
) {}
supports(uri: URI) {
return uri.isMarkdown();
return this.noteExtensions.includes(uri.getExtension());
}
async readAsMarkdown(uri: URI): Promise<string | null> {
@@ -129,17 +130,28 @@ export function createMarkdownReferences(
}
let relativeUri = target.uri.relativeTo(resource.uri.getDirectory());
if (!includeExtension && relativeUri.path.endsWith('.md')) {
if (
!includeExtension &&
relativeUri.path.endsWith(workspace.defaultExtension)
) {
relativeUri = relativeUri.changeExtension('*', '');
}
// Extract base path and link name separately.
const basePath = relativeUri.path.split('/').slice(0, -1).join('/');
const linkName = relativeUri.path.split('/').pop();
const encodedURL = encodeURIComponent(linkName).replace(/%20/g, ' ');
// [wikilink-text]: path/to/file.md "Page title"
return {
label:
link.rawText.indexOf('[[') > -1
? link.rawText.substring(2, link.rawText.length - 2)
: link.rawText,
url: relativeUri.path,
// embedded looks like ![[note-a]]
// regular note looks like [[note-a]]
label: link.rawText.substring(
link.isEmbed ? 3 : 2,
link.rawText.length - 2
),
url: `${basePath ? basePath + '/' : ''}${encodedURL}`,
title: target.title,
};
})

View File

@@ -6,6 +6,22 @@ Logger.setLevel('error');
describe('Resource Filter', () => {
describe('Filter parameters', () => {
it('should support the path regex', () => {
const noteA = createTestNote({
uri: '/path/to/foo.md',
type: 'type-1',
});
const noteB = createTestNote({
uri: 'note-b.md',
type: '/path/to/bar.md',
});
const filter = createFilter({ path: 'foo' }, false);
expect(filter(noteA)).toBeTruthy();
expect(filter(noteB)).toBeFalsy();
});
it('should support expressions when code execution is enabled', () => {
const noteA = createTestNote({
uri: 'note-a.md',

View File

@@ -53,6 +53,9 @@ export function createFilter(
if (expressionFn && !expressionFn(resource)) {
return false;
}
if (filter.path && !resource.uri.path.match(filter.path)) {
return false;
}
if (filter.type && resource.type !== filter.type) {
return false;
}

View File

@@ -89,3 +89,25 @@ export class Logger {
Logger.defaultLogger = logger;
}
}
export const withTiming = <T>(
fn: () => T,
onDidComplete: (elapsed: number) => void
): T => {
const tsStart = Date.now();
const res = fn();
const tsEnd = Date.now();
onDidComplete(tsEnd - tsStart);
return res;
};
export const withTimingAsync = async <T>(
fn: () => Promise<T>,
onDidComplete: (elapsed: number) => void
): Promise<T> => {
const tsStart = Date.now();
const res = await fn();
const tsEnd = Date.now();
onDidComplete(tsEnd - tsStart);
return res;
};

View File

@@ -7,12 +7,17 @@ import { Logger } from './core/utils/log';
import { features } from './features';
import { VsCodeOutputLogger, exposeLogger } from './services/logging';
import { getIgnoredFilesSetting } from './settings';
import {
getAttachmentsExtensions,
getIgnoredFilesSetting,
getNotesExtensions,
} from './settings';
import { AttachmentResourceProvider } from './core/services/attachment-provider';
import { VsCodeWatcher } from './services/watcher';
import { createMarkdownParser } from './core/services/markdown-parser';
import VsCodeBasedParserCache from './services/cache';
import { createMatcherAndDataStore } from './services/editor';
import { getFoamVsCodeConfig } from './services/config';
export async function activate(context: ExtensionContext) {
const logger = new VsCodeOutputLogger();
@@ -45,16 +50,32 @@ export async function activate(context: ExtensionContext) {
const parserCache = new VsCodeBasedParserCache(context);
const parser = createMarkdownParser([], parserCache);
const markdownProvider = new MarkdownResourceProvider(dataStore, parser);
const attachmentProvider = new AttachmentResourceProvider();
const { notesExtensions, defaultExtension } = getNotesExtensions();
const foamPromise = bootstrap(matcher, watcher, dataStore, parser, [
markdownProvider,
attachmentProvider,
]);
const markdownProvider = new MarkdownResourceProvider(
dataStore,
parser,
notesExtensions
);
const attachmentExtConfig = getAttachmentsExtensions();
const attachmentProvider = new AttachmentResourceProvider(
attachmentExtConfig
);
const foamPromise = bootstrap(
matcher,
watcher,
dataStore,
parser,
[markdownProvider, attachmentProvider],
defaultExtension
);
// Load the features
const resPromises = features.map(feature => feature(context, foamPromise));
const featuresPromises = features.map(feature =>
feature(context, foamPromise)
);
const foam = await foamPromise;
Logger.info(`Loaded ${foam.workspace.list().length} resources`);
@@ -66,17 +87,32 @@ export async function activate(context: ExtensionContext) {
attachmentProvider,
commands.registerCommand('foam-vscode.clear-cache', () =>
parserCache.clear()
)
),
workspace.onDidChangeConfiguration(e => {
if (
[
'foam.files.ignore',
'foam.files.attachmentExtensions',
'foam.files.noteExtensions',
'foam.files.defaultNoteExtension',
].some(setting => e.affectsConfiguration(setting))
) {
window.showInformationMessage(
'Foam: Reload the window to use the updated settings'
);
}
})
);
const res = (await Promise.all(resPromises)).filter(r => r != null);
const feats = (await Promise.all(featuresPromises)).filter(r => r != null);
return {
extendMarkdownIt: (md: markdownit) => {
return res.reduce((acc: markdownit, r: any) => {
return feats.reduce((acc: markdownit, r: any) => {
return r.extendMarkdownIt ? r.extendMarkdownIt(acc) : acc;
}, md);
},
foam,
};
} catch (e) {
Logger.error('An error occurred while bootstrapping Foam', e);

View File

@@ -10,7 +10,12 @@ import {
showInEditor,
} from '../../test/test-utils-vscode';
import { fromVsCodeUri } from '../../utils/vsc-utils';
import { CREATE_NOTE_COMMAND } from './create-note';
import { CREATE_NOTE_COMMAND, createNote } from './create-note';
import { Location } from '../../core/model/location';
import { Range } from '../../core/model/range';
import { ResourceLink } from '../../core/model/note';
import { MarkdownResourceProvider } from '../../core/services/markdown-provider';
import { createMarkdownParser } from '../../core/services/markdown-parser';
describe('create-note command', () => {
afterEach(() => {
@@ -194,12 +199,57 @@ describe('factories', () => {
describe('forPlaceholder', () => {
it('adds the .md extension to notes created for placeholders', async () => {
await closeEditors();
const command = CREATE_NOTE_COMMAND.forPlaceholder('my-placeholder');
const link: ResourceLink = {
type: 'wikilink',
rawText: '[[my-placeholder]]',
range: Range.create(0, 0, 0, 0),
isEmbed: false,
};
const command = CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(URI.file(''), link),
'.md'
);
await commands.executeCommand(command.name, command.params);
const doc = window.activeTextEditor.document;
expect(doc.uri.path).toMatch(/my-placeholder.md$/);
expect(doc.getText()).toMatch(/^# my-placeholder/);
});
it('replaces the original placeholder based on the new note identifier (#1327)', async () => {
await closeEditors();
const templateA = await createFile(
`---
foam_template:
name: 'Example Template'
description: 'An example for reproducing a bug'
filepath: '$FOAM_SLUG-world.md'
---`,
['.foam', 'templates', 'template-a.md']
);
const noteA = await createFile(`this is my [[hello]]`);
const parser = createMarkdownParser();
const res = parser.parse(noteA.uri, noteA.content);
const command = CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(noteA.uri, res.links[0]),
'.md',
{
templatePath: templateA.uri.path,
}
);
const results: Awaited<ReturnType<typeof createNote>> =
await commands.executeCommand(command.name, command.params);
expect(results.didCreateFile).toBeTruthy();
expect(results.uri.path.endsWith('hello-world.md')).toBeTruthy();
const newNoteDoc = window.activeTextEditor.document;
expect(newNoteDoc.uri.path).toMatch(/hello-world.md$/);
const { doc } = await showInEditor(noteA.uri);
expect(doc.getText()).toEqual(`this is my [[hello-world]]`);
});
});
});

View File

@@ -10,10 +10,21 @@ import { Resolver } from '../../services/variable-resolver';
import { asAbsoluteWorkspaceUri, fileExists } from '../../services/editor';
import { isSome } from '../../core/utils';
import { CommandDescriptor } from '../../utils/commands';
import { Foam } from '../../core/model/foam';
import { Location } from '../../core/model/location';
import { MarkdownLink } from '../../core/services/markdown-link';
import { ResourceLink } from '../../core/model/note';
import { toVsCodeRange, toVsCodeUri } from '../../utils/vsc-utils';
export default async function activate(context: vscode.ExtensionContext) {
export default async function activate(
context: vscode.ExtensionContext,
foamPromise: Promise<Foam>
) {
const foam = await foamPromise;
context.subscriptions.push(
vscode.commands.registerCommand(CREATE_NOTE_COMMAND.command, createNote)
vscode.commands.registerCommand(CREATE_NOTE_COMMAND.command, args =>
createNote(args, foam)
)
);
}
@@ -48,6 +59,11 @@ interface CreateNoteArgs {
* The title of the note (translates into the FOAM_TITLE variable)
*/
title?: string;
/**
* The source link that triggered the creation of the note.
* It will be updated with the appropriate identifier to the note, if necessary.
*/
sourceLink?: Location<ResourceLink>;
/**
* What to do in case the target file already exists
*/
@@ -66,7 +82,7 @@ const DEFAULT_NEW_NOTE_TEXT = `# \${FOAM_TITLE}
\${FOAM_SELECTED_TEXT}`;
async function createNote(args: CreateNoteArgs) {
export async function createNote(args: CreateNoteArgs, foam: Foam) {
args = args ?? {};
const date = isSome(args.date) ? new Date(Date.parse(args.date)) : new Date();
const resolver = new Resolver(
@@ -92,43 +108,71 @@ async function createNote(args: CreateNoteArgs) {
: getDefaultTemplateUri();
}
if (await fileExists(templateUri)) {
return NoteFactory.createFromTemplate(
templateUri,
resolver,
noteUri,
text,
args.onFileExists
);
} else {
return NoteFactory.createNote(
noteUri ?? (await getPathFromTitle(resolver)),
text,
resolver,
args.onFileExists,
args.onRelativeNotePath
);
const createdNote = (await fileExists(templateUri))
? await NoteFactory.createFromTemplate(
templateUri,
resolver,
noteUri,
text,
args.onFileExists
)
: await NoteFactory.createNote(
noteUri ?? (await getPathFromTitle(resolver)),
text,
resolver,
args.onFileExists,
args.onRelativeNotePath
);
if (args.sourceLink) {
const identifier = foam.workspace.getIdentifier(createdNote.uri);
const edit = MarkdownLink.createUpdateLinkEdit(args.sourceLink.data, {
target: identifier,
});
if (edit.newText != args.sourceLink.data.rawText) {
const updateLink = new vscode.WorkspaceEdit();
const uri = toVsCodeUri(args.sourceLink.uri);
updateLink.replace(
uri,
toVsCodeRange(args.sourceLink.range),
edit.newText
);
await vscode.workspace.applyEdit(updateLink);
}
}
return createdNote;
}
export const CREATE_NOTE_COMMAND = {
command: 'foam-vscode.create-note',
/**
* Creates a command descriptor to create a note from the given placeholder.
*
* @param placeholder the placeholder
* @param defaultExtension the default extension (e.g. '.md')
* @param extra extra command arguments
* @returns the command descriptor
*/
forPlaceholder: (
placeholder: string,
sourceLink: Location<ResourceLink>,
defaultExtension: string,
extra: Partial<CreateNoteArgs> = {}
): CommandDescriptor<CreateNoteArgs> => {
const title = placeholder.endsWith('.md')
? placeholder.replace(/\.md$/, '')
const endsWithDefaultExtension = new RegExp(defaultExtension + '$');
const { target: placeholder } = MarkdownLink.analyzeLink(sourceLink.data);
const title = placeholder.endsWith(defaultExtension)
? placeholder.replace(endsWithDefaultExtension, '')
: placeholder;
const notePath = placeholder.endsWith('.md')
const notePath = placeholder.endsWith(defaultExtension)
? placeholder
: placeholder + '.md';
: placeholder + defaultExtension;
return {
name: CREATE_NOTE_COMMAND.command,
params: {
title,
notePath,
sourceLink,
...extra,
},
};

View File

@@ -38,21 +38,42 @@ const daysOfWeek = [
];
const generateDayOfWeekSnippets = (): DateSnippet[] => {
const getTarget = (day: number) => {
const getFutureTarget = (day: number) => {
const target = new Date();
const currentDay = target.getDay();
const distance = (day + 7 - currentDay) % 7;
target.setDate(target.getDate() + distance);
return target;
};
// needs work
const getPastTarget = (day: number) => {
const target = new Date();
const currentDay = target.getDay();
const distance = currentDay === day ? 7 : (7 + currentDay - day) % 7;
target.setDate(target.getDate() - distance);
return target;
};
const snippets = daysOfWeek.map(({ day, index }) => {
const target = getTarget(index);
const target = getFutureTarget(index);
return {
date: target,
detail: `Get a daily note link for ${day}`,
snippet: `/${day}`,
};
});
// append snippets previous days
snippets.push(
...daysOfWeek.map(({ day, index }) => {
const target = getPastTarget(index);
return {
date: target,
detail: `Get a daily note link for last ${day}`,
snippet: `/-${day}`,
};
})
);
return snippets;
};

View File

@@ -14,6 +14,7 @@ import { FoamGraph } from '../core/model/graph';
import { OPEN_COMMAND } from './commands/open-resource';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { commandAsURI } from '../utils/commands';
import { Location } from '../core/model/location';
export const CONFIG_KEY = 'links.hover.enable';
@@ -106,12 +107,16 @@ export class HoverProvider implements vscode.HoverProvider {
: this.workspace.get(targetUri).title;
}
const command = CREATE_NOTE_COMMAND.forPlaceholder(targetUri.path, {
askForTemplate: true,
onFileExists: 'open',
});
const command = CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(documentUri, targetLink),
this.workspace.defaultExtension,
{
askForTemplate: true,
onFileExists: 'open',
}
);
const newNoteFromTemplate = new vscode.MarkdownString(
`[Create note from template for '${targetUri.getName()}'](${commandAsURI(
`[Create note from template for '${targetUri.getBasename()}'](${commandAsURI(
command
).toString()})`
);

View File

@@ -12,6 +12,10 @@ import { createMarkdownParser } from '../core/services/markdown-parser';
import { FoamGraph } from '../core/model/graph';
import { commandAsURI } from '../utils/commands';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { Location } from '../core/model/location';
import { URI } from '../core/model/uri';
import { Range } from '../core/model/range';
import { ResourceLink } from '../core/model/note';
describe('Document navigation', () => {
const parser = createMarkdownParser([]);
@@ -71,9 +75,8 @@ describe('Document navigation', () => {
it('should create links for placeholders', async () => {
const fileA = await createFile(`this is a link to [[a placeholder]].`);
const ws = createTestWorkspace().set(
parser.parse(fileA.uri, fileA.content)
);
const noteA = parser.parse(fileA.uri, fileA.content);
const ws = createTestWorkspace().set(noteA);
const graph = FoamGraph.fromWorkspace(ws);
const { doc } = await showInEditor(fileA.uri);
@@ -83,9 +86,13 @@ describe('Document navigation', () => {
expect(links.length).toEqual(1);
expect(links[0].target).toEqual(
commandAsURI(
CREATE_NOTE_COMMAND.forPlaceholder('a placeholder', {
onFileExists: 'open',
})
CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(noteA.uri, noteA.links[0]),
'.md',
{
onFileExists: 'open',
}
)
)
);
expect(links[0].range).toEqual(new vscode.Range(0, 20, 0, 33));

View File

@@ -10,6 +10,7 @@ import { FoamGraph } from '../core/model/graph';
import { Position } from '../core/model/position';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { commandAsURI } from '../utils/commands';
import { Location } from '../core/model/location';
export default async function activate(
context: vscode.ExtensionContext,
@@ -146,10 +147,8 @@ export class NavigationProvider
public provideDocumentLinks(
document: vscode.TextDocument
): vscode.DocumentLink[] {
const resource = this.parser.parse(
fromVsCodeUri(document.uri),
document.getText()
);
const documentUri = fromVsCodeUri(document.uri);
const resource = this.parser.parse(documentUri, document.getText());
const targets: { link: ResourceLink; target: URI }[] = resource.links.map(
link => ({
@@ -161,9 +160,13 @@ export class NavigationProvider
return targets
.filter(o => o.target.isPlaceholder()) // links to resources are managed by the definition provider
.map(o => {
const command = CREATE_NOTE_COMMAND.forPlaceholder(o.target.path, {
onFileExists: 'open',
});
const command = CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(documentUri, o.link),
this.workspace.defaultExtension,
{
onFileExists: 'open',
}
);
const documentLink = new vscode.DocumentLink(
new vscode.Range(

View File

@@ -6,7 +6,7 @@ import {
createNote,
getUriInWorkspace,
} from '../../test/test-utils-vscode';
import { ConnectionsTreeDataProvider } from './backlinks';
import { ConnectionsTreeDataProvider } from './connections';
import { MapBasedMemento, toVsCodeUri } from '../../utils/vsc-utils';
import { FoamGraph } from '../../core/model/graph';
import {

View File

@@ -53,10 +53,10 @@ export default async function activate(
}
export class ConnectionsTreeDataProvider extends BaseTreeProvider<vscode.TreeItem> {
public show = new ContextMemento<'connections' | 'backlinks' | 'links'>(
public show = new ContextMemento<'all links' | 'backlinks' | 'forward links'>(
this.state,
`foam-vscode.views.connections.show`,
'connections',
'all links',
true
);
public target?: URI = undefined;
@@ -75,9 +75,9 @@ export class ConnectionsTreeDataProvider extends BaseTreeProvider<vscode.TreeIte
}
this.disposables.push(
vscode.commands.registerCommand(
`foam-vscode.views.connections.show:connections`,
`foam-vscode.views.connections.show:all-links`,
() => {
this.show.update('connections');
this.show.update('all links');
this.refresh();
}
),
@@ -89,9 +89,9 @@ export class ConnectionsTreeDataProvider extends BaseTreeProvider<vscode.TreeIte
}
),
vscode.commands.registerCommand(
`foam-vscode.views.connections.show:links`,
`foam-vscode.views.connections.show:forward-links`,
() => {
this.show.update('links');
this.show.update('forward links');
this.refresh();
}
)
@@ -113,9 +113,9 @@ export class ConnectionsTreeDataProvider extends BaseTreeProvider<vscode.TreeIte
.asPlain()
.isEqual(this.target);
return (
this.show.get() === 'connections' ||
this.show.get() === 'all links' ||
(isBacklink && this.show.get() === 'backlinks') ||
(!isBacklink && this.show.get() === 'links')
(!isBacklink && this.show.get() === 'forward links')
);
}
);

View File

@@ -1,4 +1,4 @@
export { default as backlinks } from './backlinks';
export { default as backlinks } from './connections';
export { default as dataviz } from './dataviz';
export { default as orphans } from './orphans';
export { default as placeholders } from './placeholders';

View File

@@ -5,6 +5,7 @@ import {
ResourceRangeTreeItem,
ResourceTreeItem,
createBacklinkItemsForResource as createBacklinkTreeItemsForResource,
expandAll,
} from './utils/tree-view-utils';
import { Resource } from '../../core/model/note';
import { FoamGraph } from '../../core/model/graph';
@@ -43,6 +44,7 @@ export default async function activate(
// cause the tree view to take the focus from the item when
// browsing the notes explorer
if (
item &&
!treeView.selection.find(
i => i.resourceUri?.path === item.resourceUri.path
)
@@ -59,6 +61,11 @@ export default async function activate(
foam.graph.onDidUpdate(() => {
provider.refresh();
}),
vscode.commands.registerCommand(
`foam-vscode.views.notes-explorer.expand-all`,
(...args) =>
expandAll(treeView, provider, node => node.contextValue === 'folder')
),
vscode.window.onDidChangeActiveTextEditor(revealTextEditorItem),
treeView.onDidChangeVisibility(revealTextEditorItem)
);
@@ -133,26 +140,23 @@ export class NotesProvider extends FolderTreeProvider<
return parts;
}
isValueType(value: Resource): value is Resource {
return value.uri != null;
}
createValueTreeItem(
value: Resource,
parent: FolderTreeItem<Resource>
): NotesTreeItems {
const res = new ResourceTreeItem(value, this.workspace, {
const item = new ResourceTreeItem(value, this.workspace, {
parent,
collapsibleState:
this.graph.getBacklinks(value.uri).length > 0
? vscode.TreeItemCollapsibleState.Collapsed
: vscode.TreeItemCollapsibleState.None,
});
res.getChildren = async () => {
item.id = value.uri.toString();
item.getChildren = async () => {
const backlinks = await createBacklinkTreeItemsForResource(
this.workspace,
this.graph,
res.uri
item.uri
);
backlinks.forEach(item => {
item.description = item.label;
@@ -160,6 +164,11 @@ export class NotesProvider extends FolderTreeProvider<
});
return backlinks;
};
return res;
item.description =
value.uri.getName().toLocaleLowerCase() ===
value.title.toLocaleLowerCase()
? undefined
: value.uri.getBasename();
return item;
}
}

View File

@@ -1,12 +1,14 @@
import * as vscode from 'vscode';
import { Foam } from '../../core/model/foam';
import { createMatcherAndDataStore } from '../../services/editor';
import { getOrphansConfig } from '../../settings';
import { getAttachmentsExtensions, getOrphansConfig } from '../../settings';
import { GroupedResourcesTreeDataProvider } from './utils/grouped-resources-tree-data-provider';
import { ResourceTreeItem, UriTreeItem } from './utils/tree-view-utils';
import { IMatcher } from '../../core/services/datastore';
import { FoamWorkspace } from '../../core/model/workspace';
import { FoamGraph } from '../../core/model/graph';
import { URI } from '../../core/model/uri';
import { imageExtensions } from '../../core/services/attachment-provider';
const EXCLUDE_TYPES = ['image', 'attachment'];
export default async function activate(
@@ -66,6 +68,13 @@ export class OrphanTreeView extends GroupedResourcesTreeDataProvider {
.filter(
uri =>
!EXCLUDE_TYPES.includes(this.workspace.find(uri)?.type) &&
this.graph.getConnections(uri).length === 0
this.graph.getBacklinks(uri).length === 0 &&
this.graph.getLinks(uri).filter(c => !isAttachment(c.target))
.length === 0
);
}
function isAttachment(uri: URI) {
const ext = [...getAttachmentsExtensions(), ...imageExtensions];
return ext.includes(uri.getExtension());
}

View File

@@ -6,6 +6,7 @@ import { GroupedResourcesTreeDataProvider } from './utils/grouped-resources-tree
import {
UriTreeItem,
createBacklinkItemsForResource,
expandAll,
groupRangesByResource,
} from './utils/tree-view-utils';
import { IMatcher } from '../../core/services/datastore';
@@ -47,6 +48,17 @@ export default async function activate(
provider.onDidChangeTreeData(() => {
treeView.title = baseTitle + ` (${provider.nValues})`;
}),
vscode.commands.registerCommand(
`foam-vscode.views.placeholders.expand-all`,
() =>
expandAll(
treeView,
provider,
node =>
node.contextValue === 'placeholder' ||
node.contextValue === 'folder'
)
),
vscode.window.onDidChangeActiveTextEditor(() => {
if (provider.show.get() === 'for-current-file') {
provider.refresh();
@@ -92,6 +104,8 @@ export class PlaceholderTreeView extends GroupedResourcesTreeDataProvider {
parent,
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
});
item.contextValue = 'placeholder';
item.id = uri.toString();
item.getChildren = async () => {
return groupRangesByResource(
this.workspace,

View File

@@ -25,7 +25,7 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();
const treeItems = (await provider.getChildren()) as TagItem[];
@@ -43,12 +43,12 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();
const parentTreeItems = (await provider.getChildren()) as TagItem[];
const parentTagItem = parentTreeItems.pop();
expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');
const childTreeItems = (await provider.getChildren(
parentTagItem
@@ -57,7 +57,7 @@ describe('Tags tree panel', () => {
childTreeItems.forEach(child => {
if (child instanceof TagItem) {
// eslint-disable-next-line jest/no-conditional-expect
expect(child.title).toEqual('child');
expect(child.label).toEqual('child');
}
});
});
@@ -73,7 +73,7 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA).set(noteB);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();
const parentTreeItems = (await provider.getChildren()) as TagItem[];
@@ -81,7 +81,7 @@ describe('Tags tree panel', () => {
item => item instanceof TagItem
)[0];
expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');
expect(parentTreeItems).toHaveLength(1);
const childTreeItems = (await provider.getChildren(
@@ -91,12 +91,12 @@ describe('Tags tree panel', () => {
childTreeItems.forEach(child => {
if (child instanceof TagItem) {
// eslint-disable-next-line jest/no-conditional-expect
expect(['child', 'subchild']).toContain(child.title);
expect(['child', 'subchild']).toContain(child.label);
// eslint-disable-next-line jest/no-conditional-expect
expect(child.title).not.toEqual('parent');
expect(child.label).not.toEqual('parent');
}
});
expect(childTreeItems).toHaveLength(3);
expect(childTreeItems).toHaveLength(2);
});
it('handles a parent and child tag in the same note', async () => {
@@ -107,7 +107,7 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteC);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();
@@ -116,7 +116,7 @@ describe('Tags tree panel', () => {
item => item instanceof TagItem
)[0];
expect(parentTagItem.title).toEqual('main');
expect(parentTagItem.label).toEqual('main');
const childTreeItems = (await provider.getChildren(
parentTagItem
@@ -132,10 +132,10 @@ describe('Tags tree panel', () => {
.filter(item => item instanceof TagItem)
.forEach(item => {
expect(['main/subtopic']).toContain(item.tag);
expect(item.title).toEqual('subtopic');
expect(item.label).toEqual('subtopic');
});
expect(childTreeItems).toHaveLength(3);
expect(childTreeItems).toHaveLength(2);
});
it('handles a tag with multiple levels of hierarchy - #1134', async () => {
@@ -145,28 +145,26 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();
const parentTreeItems = (await provider.getChildren()) as TagItem[];
const parentTagItem = parentTreeItems.pop();
expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');
const childTreeItems = (await provider.getChildren(
parentTagItem
)) as TagItem[];
expect(childTreeItems).toHaveLength(2);
expect(childTreeItems[0].label).toMatch(/^Search.*/);
expect(childTreeItems[1].label).toEqual('child');
expect(childTreeItems).toHaveLength(1);
expect(childTreeItems[0].label).toEqual('child');
const grandchildTreeItems = (await provider.getChildren(
childTreeItems[1]
childTreeItems[0]
)) as TagItem[];
expect(grandchildTreeItems).toHaveLength(2);
expect(grandchildTreeItems[0].label).toMatch(/^Search.*/);
expect(grandchildTreeItems[1].label).toEqual('second');
expect(grandchildTreeItems).toHaveLength(1);
expect(grandchildTreeItems[0].label).toEqual('second');
});
});

View File

@@ -6,8 +6,20 @@ import { FoamTags } from '../../core/model/tags';
import {
ResourceRangeTreeItem,
ResourceTreeItem,
expandAll,
groupRangesByResource,
} from './utils/tree-view-utils';
import {
Folder,
FolderTreeItem,
FolderTreeProvider,
walk,
} from './utils/folder-tree-provider';
import {
ContextMemento,
MapBasedMemento,
fromVsCodeUri,
} from '../../utils/vsc-utils';
const TAG_SEPARATOR = '/';
export default async function activate(
@@ -20,52 +32,136 @@ export default async function activate(
treeDataProvider: provider,
showCollapseAll: true,
});
provider.refresh();
const baseTitle = treeView.title;
treeView.title = baseTitle + ` (${foam.tags.tags.size})`;
context.subscriptions.push(
treeView,
foam.tags.onDidUpdate(() => {
provider.refresh();
treeView.title = baseTitle + ` (${foam.tags.tags.size})`;
})
}),
vscode.window.onDidChangeActiveTextEditor(() => {
if (provider.show.get() === 'for-current-file') {
provider.refresh();
}
}),
vscode.commands.registerCommand(
`foam-vscode.views.${provider.providerId}.expand-all`,
() =>
expandAll(
treeView,
provider,
node => node.contextValue === 'tag' || node.contextValue === 'folder'
)
)
);
}
export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
// prettier-ignore
private _onDidChangeTreeData: vscode.EventEmitter<
TagTreeItem | undefined | void
> = new vscode.EventEmitter<TagTreeItem | undefined | void>();
// prettier-ignore
readonly onDidChangeTreeData: vscode.Event<TagTreeItem | undefined | void> =
this._onDidChangeTreeData.event;
export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
public providerId = 'tags-explorer';
public show = new ContextMemento<'all' | 'for-current-file'>(
new MapBasedMemento(),
`foam-vscode.views.${this.providerId}.show`,
'all'
);
public groupBy = new ContextMemento<'off' | 'folder'>(
new MapBasedMemento(),
`foam-vscode.views.${this.providerId}.group-by`,
'folder'
);
private tags: {
tag: string;
notes: URI[];
}[];
private foamTags: FoamTags;
constructor(tags: FoamTags, private workspace: FoamWorkspace) {
this.foamTags = tags;
this.computeTags();
constructor(
private foamTags: FoamTags,
private workspace: FoamWorkspace,
registerCommands: boolean = true
) {
super();
if (!registerCommands) {
return;
}
this.disposables.push(
vscode.commands.registerCommand(
`foam-vscode.views.${this.providerId}.show:all`,
() => {
this.show.update('all');
this.refresh();
}
),
vscode.commands.registerCommand(
`foam-vscode.views.${this.providerId}.show:for-current-file`,
() => {
this.show.update('for-current-file');
this.refresh();
}
),
vscode.commands.registerCommand(
`foam-vscode.views.${this.providerId}.group-by:folder`,
() => {
this.groupBy.update('folder');
this.refresh();
}
),
vscode.commands.registerCommand(
`foam-vscode.views.${this.providerId}.group-by:off`,
() => {
this.groupBy.update('off');
this.refresh();
}
)
);
}
refresh(): void {
this.computeTags();
this._onDidChangeTreeData.fire();
}
private computeTags() {
this.tags = [...this.foamTags.tags]
.map(([tag, notes]) => ({ tag, notes }))
.sort((a, b) => a.tag.localeCompare(b.tag));
super.refresh();
}
getTreeItem(element: TagTreeItem): vscode.TreeItem {
return element;
getValues(): string[] {
if (this.show.get() === 'for-current-file') {
const uriInEditor = vscode.window.activeTextEditor?.document.uri;
const currentResource = this.workspace.find(fromVsCodeUri(uriInEditor));
return currentResource?.tags.map(t => t.label) ?? [];
}
return Array.from(this.tags.values()).map(tag => tag.tag);
}
valueToPath(value: string) {
return this.groupBy.get() === 'off' ? [value] : value.split(TAG_SEPARATOR);
}
private countResourcesInSubtree(node: Folder<string>) {
const nChildren = walk(
node,
tag => this.foamTags.tags.get(tag)?.length ?? 0
).reduce((acc, nResources) => acc + nResources, 0);
return nChildren;
}
createFolderTreeItem(
node: Folder<string>,
name: string,
parent: FolderTreeItem<string>
): FolderTreeItem<string> {
const nChildren = this.countResourcesInSubtree(node);
return new TagItem(node, nChildren, [], parent);
}
createValueTreeItem(
value: string,
parent: FolderTreeItem<string>,
node: Folder<string>
): TagItem {
const nChildren = this.countResourcesInSubtree(node);
const resources = this.foamTags.tags.get(value) ?? [];
return new TagItem(node, nChildren, resources, parent);
}
async getChildren(element?: TagItem): Promise<TagTreeItem[]> {
@@ -73,37 +169,11 @@ export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
const children = await (element as any).getChildren();
return children;
}
const parentTag = element ? element.tag : '';
const parentPrefix = element ? parentTag + TAG_SEPARATOR : '';
const tagsAtThisLevel = this.tags
.filter(({ tag }) => tag.startsWith(parentPrefix))
.map(({ tag }) => {
const nextSeparator = tag.indexOf(TAG_SEPARATOR, parentPrefix.length);
const label =
nextSeparator > -1
? tag.substring(parentPrefix.length, nextSeparator)
: tag.substring(parentPrefix.length);
const tagId = parentPrefix + label;
return { label, tagId, tag };
})
.reduce((acc, { label, tagId, tag }) => {
const existing = acc.has(label);
const nResources = this.foamTags.tags.get(tag).length ?? 0;
if (!existing) {
acc.set(label, { label, tagId, nResources: 0 });
}
acc.get(label).nResources += nResources;
return acc;
}, new Map() as Map<string, { label: string; tagId: string; nResources: number }>);
const subtags = Array.from(tagsAtThisLevel.values())
.map(({ label, tagId, nResources }) => {
const resources = this.foamTags.tags.get(tagId) ?? [];
return new TagItem(tagId, label, nResources, resources);
})
.sort((a, b) => a.title.localeCompare(b.title));
// Subtags are managed by the FolderTreeProvider
const subtags = await super.getChildren(element);
// Compute the resources children
const resourceTags: ResourceRangeTreeItem[] = (element?.notes ?? [])
.map(uri => this.workspace.get(uri))
.reduce((acc, note) => {
@@ -118,41 +188,31 @@ export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
);
return [...acc, ...items];
}, []);
const resources = (
await groupRangesByResource(this.workspace, resourceTags)
).map(item => {
item.id = element.tag + ' / ' + item.uri.toString();
return item;
});
const resources = await groupRangesByResource(this.workspace, resourceTags);
return Promise.resolve(
[element && new TagSearch(element.tag), ...subtags, ...resources].filter(
Boolean
)
);
}
async resolveTreeItem(item: TagTreeItem): Promise<TagTreeItem> {
if (
item instanceof ResourceTreeItem ||
item instanceof ResourceRangeTreeItem
) {
return item.resolveTreeItem();
}
return Promise.resolve(item);
return [...subtags, ...resources];
}
}
type TagTreeItem =
| TagItem
| TagSearch
| ResourceTreeItem
| ResourceRangeTreeItem;
type TagTreeItem = TagItem | ResourceTreeItem | ResourceRangeTreeItem;
export class TagItem extends FolderTreeItem<string> {
public readonly tag: string;
export class TagItem extends vscode.TreeItem {
constructor(
public readonly tag: string,
public readonly title: string,
public readonly node: Folder<string>,
public readonly nResourcesInSubtree: number,
public readonly notes: URI[]
public readonly notes: URI[],
public readonly parentElement?: FolderTreeItem<string>
) {
super(title, vscode.TreeItemCollapsibleState.Collapsed);
super(node, node.path.slice(-1)[0], parentElement);
this.tag = node.path.join(TAG_SEPARATOR);
this.id = this.tag;
this.description = `${nResourcesInSubtree} reference${
nResourcesInSubtree !== 1 ? 's' : ''
}`;
@@ -161,27 +221,3 @@ export class TagItem extends vscode.TreeItem {
iconPath = new vscode.ThemeIcon('symbol-number');
contextValue = 'tag';
}
export class TagSearch extends vscode.TreeItem {
public readonly title: string;
constructor(public readonly tag: string) {
super(`Search #${tag}`, vscode.TreeItemCollapsibleState.None);
const searchString = `#${tag}`;
this.tooltip = `Search ${searchString} in workspace`;
this.command = {
command: 'workbench.action.findInFiles',
arguments: [
{
query: searchString,
triggerSearch: true,
matchWholeWord: true,
isCaseSensitive: true,
},
],
title: 'Search',
};
}
iconPath = new vscode.ThemeIcon('search');
contextValue = 'tag-search';
}

View File

@@ -6,7 +6,11 @@ import { BaseTreeItem, ResourceTreeItem } from './tree-view-utils';
* A folder is a map of basenames to either folders or values (e.g. resources).
*/
export interface Folder<T> {
[basename: string]: Folder<T> | T;
children: {
[basename: string]: Folder<T>;
};
value?: T;
path: string[];
}
/**
@@ -18,7 +22,7 @@ export class FolderTreeItem<T> extends vscode.TreeItem {
iconPath = new vscode.ThemeIcon('folder');
constructor(
public parent: Folder<T>,
public node: Folder<T>,
public name: string,
public parentElement?: FolderTreeItem<T>
) {
@@ -52,11 +56,11 @@ export abstract class FolderTreeProvider<I, T> extends BaseTreeProvider<I> {
}
createFolderTreeItem(
value: Folder<T>,
node: Folder<T>,
name: string,
parent: FolderTreeItem<T>
) {
return new FolderTreeItem<T>(value, name, parent);
return new FolderTreeItem<T>(node, name, parent);
}
async getChildren(item?: I): Promise<I[]> {
@@ -64,42 +68,52 @@ export abstract class FolderTreeProvider<I, T> extends BaseTreeProvider<I> {
return item.getChildren() as Promise<I[]>;
}
const parent = (item as any)?.parent ?? this.root;
const parent: Folder<T> = (item as any)?.node ?? this.root;
const children: vscode.TreeItem[] = Object.keys(parent).map(name => {
const value = parent[name];
if (this.isValueType(value)) {
return this.createValueTreeItem(value, undefined);
} else {
return this.createFolderTreeItem(
value,
name,
item as unknown as FolderTreeItem<T>
);
const children: vscode.TreeItem[] = Object.keys(parent?.children ?? []).map(
name => {
const node = parent.children[name];
if (node.value != null) {
return this.createValueTreeItem(node.value, undefined, node);
} else {
return this.createFolderTreeItem(
node,
name,
item as unknown as FolderTreeItem<T>
);
}
}
});
);
return children.sort((a, b) => sortFolderTreeItems(a, b)) as any;
}
createTree(values: T[], filterFn: (value: T) => boolean): Folder<T> {
const root: Folder<T> = {};
const root: Folder<T> = {
children: {},
path: [],
};
for (const r of values) {
const parts = this.valueToPath(r);
let currentNode: Folder<T> = root;
parts.forEach((part, index) => {
if (!currentNode[part]) {
if (!currentNode.children[part]) {
if (index < parts.length - 1) {
currentNode[part] = {};
} else {
if (filterFn(r)) {
currentNode[part] = r;
}
currentNode.children[part] = {
children: {},
path: parts.slice(0, index + 1),
};
} else if (filterFn(r)) {
currentNode.children[part] = {
children: {},
path: parts.slice(0, index + 1),
value: r,
};
}
}
currentNode = currentNode[part] as Folder<T>;
currentNode = currentNode.children[part];
});
}
@@ -109,24 +123,25 @@ export abstract class FolderTreeProvider<I, T> extends BaseTreeProvider<I> {
getTreeItemsHierarchy(path: string[]): vscode.TreeItem[] {
const treeItemsHierarchy: vscode.TreeItem[] = [];
let currentNode: Folder<T> | T = this.root;
let currentNode: Folder<T> = this.root;
for (const part of path) {
if (currentNode[part] !== undefined) {
currentNode = currentNode[part] as Folder<T> | T;
if (this.isValueType(currentNode as T)) {
if (currentNode.children[part] !== undefined) {
currentNode = currentNode.children[part] as Folder<T>;
if (currentNode.value) {
treeItemsHierarchy.push(
this.createValueTreeItem(
currentNode as T,
currentNode.value,
treeItemsHierarchy[
treeItemsHierarchy.length - 1
] as FolderTreeItem<T>
] as FolderTreeItem<T>,
currentNode
)
);
} else {
treeItemsHierarchy.push(
new FolderTreeItem(
currentNode as Folder<T>,
currentNode,
part,
treeItemsHierarchy[
treeItemsHierarchy.length - 1
@@ -172,16 +187,36 @@ export abstract class FolderTreeProvider<I, T> extends BaseTreeProvider<I> {
*/
abstract getValues(): T[];
/**
* Returns true if the given value is of the type that should be displayed
* as a leaf in the tree. That is, not as a folder.
*/
abstract isValueType(value: T): value is T;
/**
* Creates a tree item for the given value.
*/
abstract createValueTreeItem(value: T, parent: FolderTreeItem<T>): I;
abstract createValueTreeItem(
value: T,
parent: FolderTreeItem<T>,
node: Folder<T>
): I;
}
/**
* walks the node and performs an action on each value
* @returns
*/
export function walk<T, R>(node: Folder<T>, fn: (value: T) => R): R[] {
const results: R[] = [];
function traverse(node: Folder<T>) {
if (node.value) {
results.push(fn(node.value));
}
Object.values(node.children).forEach(child => {
traverse(child);
});
}
traverse(node);
return results;
}
function sortFolderTreeItems(a: vscode.TreeItem, b: vscode.TreeItem): number {

View File

@@ -92,18 +92,14 @@ export abstract class GroupedResourcesTreeDataProvider extends FolderTreeProvide
return uris.filter(uri => this.matcher.isMatch(uri));
}
isValueType(value: URI): value is URI {
return value instanceof URI;
}
createFolderTreeItem(
value: Folder<URI>,
node: Folder<URI>,
name: string,
parent: FolderTreeItem<URI>
) {
const item = super.createFolderTreeItem(value, name, parent);
const item = super.createFolderTreeItem(node, name, parent);
item.label = item.label || '(Not Created)';
item.description = `(${Object.keys(value).length})`;
item.description = `(${Object.keys(node.children).length})`;
return item;
}

View File

@@ -9,6 +9,7 @@ import { getNoteTooltip } from '../../../utils';
import { isSome } from '../../../core/utils';
import { getBlockFor } from '../../../core/services/markdown-parser';
import { Connection, FoamGraph } from '../../../core/model/graph';
import { Logger } from '../../../core/utils/log';
export class BaseTreeItem extends vscode.TreeItem {
resolveTreeItem(): Promise<vscode.TreeItem> {
@@ -227,3 +228,57 @@ export function createConnectionItemsForResource(
});
return Promise.all(backlinkItems);
}
/**
* Expands a node and its children in a tree view that match a given predicate
*
* @param treeView - The tree view to expand nodes in
* @param provider - The tree data provider for the view
* @param element - The element to expand
* @param when - A function that returns true if the node should be expanded
*/
export async function expandNode<T>(
treeView: vscode.TreeView<any>,
provider: vscode.TreeDataProvider<T>,
element: T,
when: (element: T) => boolean
) {
try {
if (when(element)) {
await treeView.reveal(element, {
select: false,
focus: false,
expand: true,
});
}
} catch (e) {
const obj = element as any;
const label = obj.label ?? obj.toString();
Logger.warn(
`Could not expand element: ${label}. Try setting the ID property of the TreeItem`
);
}
const children = await provider.getChildren(element);
for (const child of children) {
await expandNode(treeView, provider, child, when);
}
}
/**
* Expands all items in a tree view that match a given predicate
*
* @param treeView - The tree view to expand items in
* @param provider - The tree data provider for the view
* @param when - A function that returns true if the node should be expanded
*/
export async function expandAll<T>(
treeView: vscode.TreeView<T>,
provider: vscode.TreeDataProvider<T>,
when: (element: T) => boolean = () => true
) {
const elements = await provider.getChildren(undefined);
for (const element of elements) {
await expandNode(treeView, provider, element, when);
}
}

View File

@@ -8,21 +8,21 @@ import {
} from '../../test/test-utils-vscode';
import {
default as markdownItWikilinkEmbed,
CONFIG_EMBED_NOTE_IN_CONTAINER,
CONFIG_EMBED_NOTE_TYPE,
} from './wikilink-embed';
const parser = createMarkdownParser();
describe('Displaying included notes in preview', () => {
it('should render an included note in flat mode', async () => {
it('should render an included note in full inline mode', async () => {
const note = await createFile('This is the text of note A', [
'preview',
'note-a.md',
]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_IN_CONTAINER,
false,
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
@@ -40,16 +40,16 @@ describe('Displaying included notes in preview', () => {
await deleteFile(note);
});
it('should render an included note in container mode', async () => {
it('should render an included note in full card mode', async () => {
const note = await createFile('This is the text of note A', [
'preview',
'note-a.md',
]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_IN_CONTAINER,
true,
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-card',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
@@ -62,7 +62,7 @@ describe('Displaying included notes in preview', () => {
await deleteFile(note);
});
it('should render an included section', async () => {
it('should render an included section in full inline mode', async () => {
// here we use createFile as the test note doesn't fill in
// all the metadata we need
const note = await createFile(
@@ -83,8 +83,8 @@ This is the third section of note E
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_IN_CONTAINER,
false,
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
expect(
md.render(`This is the root node.
@@ -102,7 +102,7 @@ This is the third section of note E
await deleteFile(note);
});
it('should render an included section in container mode', async () => {
it('should render an included section in full card mode', async () => {
const note = await createFile(
`
# Section 1
@@ -120,8 +120,8 @@ This is the third section of note E
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_IN_CONTAINER,
true,
CONFIG_EMBED_NOTE_TYPE,
'full-card',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
@@ -138,6 +138,220 @@ This is the third section of note E
await deleteFile(note);
});
it('should not render the title of a note in content inline mode', async () => {
const note = await createFile(
`
# Title
## Section 1
This is the first section of note E`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'content-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
expect(
md.render(`This is the root node.
![[note-e]]`)
).toMatch(
`<p>This is the root node.</p>
<p><h2>Section 1</h2>
<p>This is the first section of note E</p>
</p>`
);
}
);
await deleteFile(note);
});
it('should not render the title of a note in content card mode', async () => {
const note = await createFile(
`# Title
## Section 1
This is the first section of note E
`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'content-card',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
const res = md.render(`This is the root node. ![[note-e.md]]`);
expect(res).toContain('This is the root node');
expect(res).toContain('embed-container-note');
expect(res).toContain('Section 1');
expect(res).toContain('This is the first section of note E');
expect(res).not.toContain('Title');
}
);
await deleteFile(note);
});
it('should not render the section title, but still render subsection titles in content inline mode', async () => {
const note = await createFile(
`# Title
## Section 1
This is the first section of note E
### Subsection a
This is the first subsection of note E
`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'content-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
expect(
md.render(`This is the root node.
![[note-e#Section 1]]`)
).toMatch(
`<p>This is the root node.</p>
<p><p>This is the first section of note E</p>
<h3>Subsection a</h3>
<p>This is the first subsection of note E</p>
</p>`
);
}
);
await deleteFile(note);
});
it('should not render the subsection title in content mode if you link to it and regardless of its level', async () => {
const note = await createFile(
`# Title
## Section 1
This is the first section of note E
### Subsection a
This is the first subsection of note E`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'content-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
expect(
md.render(`This is the root node.
![[note-e#Subsection a]]`)
).toMatch(
`<p>This is the root node.</p>
<p><p>This is the first subsection of note E</p>
</p>`
);
}
);
await deleteFile(note);
});
it('should allow a note embedding type to be overridden if a modifier is passed in', async () => {
const note = await createFile(
`
# Section 1
This is the first section of note E
# Section 2
This is the second section of note E
# Section 3
This is the third section of note E
`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
expect(
md.render(`This is the root node.
content![[note-e#Section 2]]
full![[note-e#Section 3]]`)
).toMatch(
`<p>This is the root node.</p>
<p><p>This is the second section of note E</p>
</p>
<p><h1>Section 3</h1>
<p>This is the third section of note E</p>
</p>
`
);
}
);
await deleteFile(note);
});
it('should allow a note embedding type to be overridden if two modifiers are passed in', async () => {
const note = await createFile(
`
# Section 1
This is the first section of note E
# Section 2
This is the second section of note E
`,
['note-e.md']
);
const parser = createMarkdownParser([]);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
const res = md.render(`This is the root node.
content-card![[note-e#Section 2]]`);
expect(res).toContain('This is the root node');
expect(res).toContain('embed-container-note');
expect(res).toContain('This is the second section of note E');
expect(res).not.toContain('Section 2');
}
);
await deleteFile(note);
});
it('should fallback to the bare text when the note is not found', () => {
const md = markdownItWikilinkEmbed(
MarkdownIt(),
@@ -150,6 +364,27 @@ This is the third section of note E
);
});
it('should render the bare text for an embedded note that is embedding a note that is not found', async () => {
const note = await createFile(
'This is the text of note A which includes ![[does-not-exist]]',
['note.md']
);
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
expect(md.render(`This is the root node. ![[note]]`)).toMatch(
`<p>This is the root node. <p>This is the text of note A which includes ![[does-not-exist]]</p>
</p>`
);
}
);
});
it('should display a warning in case of cyclical inclusions', async () => {
const noteA = await createFile(
'This is the text of note A which includes ![[note-b]]',
@@ -162,14 +397,21 @@ This is the third section of note E
const ws = new FoamWorkspace()
.set(parser.parse(noteA.uri, noteA.content))
.set(parser.parse(noteB.uri, noteB.content));
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
const res = md.render(noteBText);
expect(res).toContain('This is the text of note B which includes');
expect(res).toContain('This is the text of note A which includes');
expect(res).toContain('Cyclic link detected for wikilink');
await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-card',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
const res = md.render(noteBText);
deleteFile(noteA);
deleteFile(noteB);
expect(res).toContain('This is the text of note B which includes');
expect(res).toContain('This is the text of note A which includes');
expect(res).toContain('Cyclic link detected for wikilink');
}
);
await deleteFile(noteA);
await deleteFile(noteB);
});
});

View File

@@ -0,0 +1,92 @@
import {
WIKILINK_EMBED_REGEX,
WIKILINK_EMBED_REGEX_GROUPS,
retrieveNoteConfig,
} from './wikilink-embed';
import * as config from '../../services/config';
describe('Wikilink Note Embedding', () => {
afterEach(() => {
jest.clearAllMocks();
});
describe('Wikilink Parsing', () => {
it('should match a wikilink item including a modifier and wikilink', () => {
// no configuration
expect('![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
// one of the configurations
expect('full![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('content![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('inline![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('card![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
// any combination of configurations
expect('full-inline![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('full-card![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('content-inline![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
expect('content-card![[note-a]]').toMatch(WIKILINK_EMBED_REGEX);
});
it('should only match the wikilink if there are unrecognized keywords', () => {
const match1 = 'random-word![[note-a]]'.match(WIKILINK_EMBED_REGEX);
expect(match1[0]).toEqual('![[note-a]]');
expect(match1[1]).toEqual('![[note-a]]');
const match2 = 'foo![[note-a#section 1]]'.match(WIKILINK_EMBED_REGEX);
expect(match2[0]).toEqual('![[note-a#section 1]]');
expect(match2[1]).toEqual('![[note-a#section 1]]');
});
it('should group the wikilink into modifier and wikilink', () => {
const match1 = 'content![[note-a]]'.match(WIKILINK_EMBED_REGEX_GROUPS);
expect(match1[0]).toEqual('content![[note-a]]');
expect(match1[1]).toEqual('content');
expect(match1[2]).toEqual('note-a');
const match2 = 'full-inline![[note-a#section 1]]'.match(
WIKILINK_EMBED_REGEX_GROUPS
);
expect(match2[0]).toEqual('full-inline![[note-a#section 1]]');
expect(match2[1]).toEqual('full-inline');
expect(match2[2]).toEqual('note-a#section 1');
const match3 = '![[note-a#section 1]]'.match(WIKILINK_EMBED_REGEX_GROUPS);
expect(match3[0]).toEqual('![[note-a#section 1]]');
expect(match3[1]).toEqual(undefined);
expect(match3[2]).toEqual('note-a#section 1');
});
});
describe('Config Parsing', () => {
it('should use preview.embedNoteType if an explicit modifier is not passed in', () => {
jest
.spyOn(config, 'getFoamVsCodeConfig')
.mockReturnValueOnce('full-card');
const { noteScope, noteStyle } = retrieveNoteConfig(undefined);
expect(noteScope).toEqual('full');
expect(noteStyle).toEqual('card');
});
it('should use explicit modifier over user settings if passed in', () => {
jest
.spyOn(config, 'getFoamVsCodeConfig')
.mockReturnValueOnce('full-inline')
.mockReturnValueOnce('full-inline')
.mockReturnValueOnce('full-inline');
let { noteScope, noteStyle } = retrieveNoteConfig('content-card');
expect(noteScope).toEqual('content');
expect(noteStyle).toEqual('card');
({ noteScope, noteStyle } = retrieveNoteConfig('content'));
expect(noteScope).toEqual('content');
expect(noteStyle).toEqual('inline');
({ noteScope, noteStyle } = retrieveNoteConfig('card'));
expect(noteScope).toEqual('full');
expect(noteStyle).toEqual('card');
});
});
});

View File

@@ -4,7 +4,7 @@
import { readFileSync } from 'fs';
import { workspace as vsWorkspace } from 'vscode';
import markdownItRegex from 'markdown-it-regex';
import { isSome } from '../../utils';
import { isSome, isNone } from '../../utils';
import { FoamWorkspace } from '../../core/model/workspace';
import { Logger } from '../../core/utils/log';
import { Resource, ResourceParser } from '../../core/model/note';
@@ -14,7 +14,14 @@ import { MarkdownLink } from '../../core/services/markdown-link';
import { Position } from '../../core/model/position';
import { TextEdit } from '../../core/services/text-edit';
export const CONFIG_EMBED_NOTE_IN_CONTAINER = 'preview.embedNoteInContainer';
export const WIKILINK_EMBED_REGEX =
/((?:(?:full|content)-(?:inline|card)|full|content|inline|card)?!\[\[[^[\]]+?\]\])/;
// we need another regex because md.use(regex, replace) only permits capturing one group
// so we capture the entire possible wikilink item (ex. content-card![[note]]) using WIKILINK_EMBED_REGEX and then
// use WIKILINK_EMBED_REGEX_GROUPER to parse it into the modifier(content-card) and the wikilink(note)
export const WIKILINK_EMBED_REGEX_GROUPS =
/((?:\w+)|(?:(?:\w+)-(?:\w+)))?!\[\[([^[\]]+?)\]\]/;
export const CONFIG_EMBED_NOTE_TYPE = 'preview.embedNoteType';
const refsStack: string[] = [];
export const markdownItWikilinkEmbed = (
@@ -24,9 +31,13 @@ export const markdownItWikilinkEmbed = (
) => {
return md.use(markdownItRegex, {
name: 'embed-wikilinks',
regex: /!\[\[([^[\]]+?)\]\]/,
replace: (wikilink: string) => {
regex: WIKILINK_EMBED_REGEX,
replace: (wikilinkItem: string) => {
try {
const [_, noteEmbedModifier, wikilink] = wikilinkItem.match(
WIKILINK_EMBED_REGEX_GROUPS
);
const includedNote = workspace.find(wikilink);
if (!includedNote) {
@@ -45,27 +56,29 @@ export const markdownItWikilinkEmbed = (
return `<div class="foam-cyclic-link-warning">Cyclic link detected for wikilink: ${wikilink}</div>`;
}
let content = `Embed for [[${wikilink}]]`;
let html: string;
switch (includedNote.type) {
case 'note': {
let noteText = readFileSync(includedNote.uri.toFsPath()).toString();
const section = Resource.findSection(
includedNote,
includedNote.uri.fragment
);
if (isSome(section)) {
const rows = noteText.split('\n');
noteText = rows
.slice(section.range.start.line, section.range.end.line)
.join('\n');
}
noteText = withLinksRelativeToWorkspaceRoot(
noteText,
parser,
workspace
);
content = getFoamVsCodeConfig(CONFIG_EMBED_NOTE_IN_CONTAINER)
? `<div class="embed-container-note">${md.render(noteText)}</div>`
: noteText;
const { noteScope, noteStyle } =
retrieveNoteConfig(noteEmbedModifier);
const extractor: EmbedNoteExtractor =
noteScope === 'full'
? fullExtractor
: noteScope === 'content'
? contentExtractor
: fullExtractor;
const formatter: EmbedNoteFormatter =
noteStyle === 'card'
? cardFormatter
: noteStyle === 'inline'
? inlineFormatter
: cardFormatter;
content = extractor(includedNote, parser, workspace);
html = formatter(content, md);
break;
}
case 'attachment':
@@ -74,19 +87,20 @@ export const markdownItWikilinkEmbed = (
${md.renderInline('[[' + wikilink + ']]')}<br/>
Embed for attachments is not supported
</div>`;
html = md.render(content);
break;
case 'image':
content = `<div class="embed-container-image">${md.render(
`![](${md.normalizeLink(includedNote.uri.path)})`
)}</div>`;
html = md.render(content);
break;
}
const html = md.render(content);
refsStack.pop();
return html;
} catch (e) {
Logger.error(
`Error while including [[${wikilink}]] into the current document of the Preview panel`,
`Error while including ${wikilinkItem} into the current document of the Preview panel`,
e
);
return '';
@@ -108,6 +122,11 @@ function withLinksRelativeToWorkspaceRoot(
.map(link => {
const info = MarkdownLink.analyzeLink(link);
const resource = workspace.find(info.target);
// embedded notes that aren't created are still collected
// return null so it can be filtered in the next step
if (isNone(resource)) {
return null;
}
const pathFromRoot = vsWorkspace.asRelativePath(
toVsCodeUri(resource.uri)
);
@@ -115,6 +134,7 @@ function withLinksRelativeToWorkspaceRoot(
target: pathFromRoot,
});
})
.filter(linkEdits => !isNone(linkEdits))
.sort((a, b) => Position.compareTo(b.range.start, a.range.start));
const text = edits.reduce(
(text, edit) => TextEdit.apply(text, edit),
@@ -123,4 +143,91 @@ function withLinksRelativeToWorkspaceRoot(
return text;
}
export function retrieveNoteConfig(explicitModifier: string | undefined): {
noteScope: string;
noteStyle: string;
} {
let config = getFoamVsCodeConfig<string>(CONFIG_EMBED_NOTE_TYPE); // ex. full-inline
let [noteScope, noteStyle] = config.split('-');
// an explicit modifier will always override corresponding user setting
if (explicitModifier !== undefined) {
if (['full', 'content'].includes(explicitModifier)) {
noteScope = explicitModifier;
} else if (['card', 'inline'].includes(explicitModifier)) {
noteStyle = explicitModifier;
} else {
[noteScope, noteStyle] = explicitModifier.split('-');
}
}
return { noteScope, noteStyle };
}
/**
* A type of function that gets the desired content of the note
*/
export type EmbedNoteExtractor = (
note: Resource,
parser: ResourceParser,
workspace: FoamWorkspace
) => string;
function fullExtractor(
note: Resource,
parser: ResourceParser,
workspace: FoamWorkspace
): string {
let noteText = readFileSync(note.uri.toFsPath()).toString();
const section = Resource.findSection(note, note.uri.fragment);
if (isSome(section)) {
const rows = noteText.split('\n');
noteText = rows
.slice(section.range.start.line, section.range.end.line)
.join('\n');
}
noteText = withLinksRelativeToWorkspaceRoot(noteText, parser, workspace);
return noteText;
}
function contentExtractor(
note: Resource,
parser: ResourceParser,
workspace: FoamWorkspace
): string {
let noteText = readFileSync(note.uri.toFsPath()).toString();
let section = Resource.findSection(note, note.uri.fragment);
if (!note.uri.fragment) {
// if there's no fragment(section), the wikilink is linking to the entire note,
// in which case we need to remove the title. We could just use rows.shift()
// but should the note start with blank lines, it will only remove the first blank line
// leaving the title
// A better way is to find where the actual title starts by assuming it's at section[0]
// then we treat it as the same case as link to a section
section = note.sections.length ? note.sections[0] : null;
}
let rows = noteText.split('\n');
if (isSome(section)) {
rows = rows.slice(section.range.start.line, section.range.end.line);
}
rows.shift();
noteText = rows.join('\n');
noteText = withLinksRelativeToWorkspaceRoot(noteText, parser, workspace);
return noteText;
}
/**
* A type of function that renders note content with the desired style in html
*/
export type EmbedNoteFormatter = (content: string, md: markdownit) => string;
function cardFormatter(content: string, md: markdownit): string {
return md.render(
`<div class="embed-container-note">${md.render(content)}</div>`
);
}
function inlineFormatter(content: string, md: markdownit): string {
return md.render(content);
}
export default markdownItWikilinkEmbed;

View File

@@ -26,9 +26,9 @@ describe('Link generation in preview', () => {
markdownItRemoveLinkReferences,
].reduce((acc, extension) => extension(acc, ws), MarkdownIt());
it('generates a link to a note', () => {
it('generates a link to a note using the note title as link', () => {
expect(md.render(`[[note-a]]`)).toEqual(
`<p><a class='foam-note-link' title='${noteA.title}' href='/path/to/note-a.md' data-href='/path/to/note-a.md'>note-a</a></p>\n`
`<p><a class='foam-note-link' title='${noteA.title}' href='/path/to/note-a.md' data-href='/path/to/note-a.md'>${noteA.title}</a></p>\n`
);
});
@@ -48,13 +48,22 @@ describe('Link generation in preview', () => {
const note = `[[note-a]]
[note-a]: <note-a.md> "Note A"`;
expect(md.render(note)).toEqual(
`<p><a class='foam-note-link' title='${noteA.title}' href='/path/to/note-a.md' data-href='/path/to/note-a.md'>note-a</a>\n[note-a]: &lt;note-a.md&gt; &quot;Note A&quot;</p>\n`
`<p><a class='foam-note-link' title='${noteA.title}' href='/path/to/note-a.md' data-href='/path/to/note-a.md'>${noteA.title}</a>\n[note-a]: &lt;note-a.md&gt; &quot;Note A&quot;</p>\n`
);
});
it('generates a link to a section within the note', () => {
expect(md.render(`[[#sec]]`)).toEqual(
`<p><a class='foam-note-link' title='sec' href='#sec' data-href='#sec'>#sec</a></p>\n`
);
expect(md.render(`[[#Section Name]]`)).toEqual(
`<p><a class='foam-note-link' title='Section Name' href='#section-name' data-href='#section-name'>#Section Name</a></p>\n`
);
});
it('generates a link to a note with a specific section', () => {
expect(md.render(`[[note-b#sec2]]`)).toEqual(
`<p><a class='foam-note-link' title='My second note#sec2' href='/path2/to/note-b.md#sec2' data-href='/path2/to/note-b.md#sec2'>note-b#sec2</a></p>\n`
`<p><a class='foam-note-link' title='My second note#sec2' href='/path2/to/note-b.md#sec2' data-href='/path2/to/note-b.md#sec2'>${noteB.title}#sec2</a></p>\n`
);
});
@@ -66,7 +75,7 @@ describe('Link generation in preview', () => {
it('generates a link to a note if the note exists, but the section does not exist', () => {
expect(md.render(`[[note-b#nonexistentsec]]`)).toEqual(
`<p><a class='foam-note-link' title='My second note#nonexistentsec' href='/path2/to/note-b.md#nonexistentsec' data-href='/path2/to/note-b.md#nonexistentsec'>note-b#nonexistentsec</a></p>\n`
`<p><a class='foam-note-link' title='My second note#nonexistentsec' href='/path2/to/note-b.md#nonexistentsec' data-href='/path2/to/note-b.md#nonexistentsec'>${noteB.title}#nonexistentsec</a></p>\n`
);
});

View File

@@ -9,6 +9,7 @@ import { toVsCodeUri } from '../../utils/vsc-utils';
import { MarkdownLink } from '../../core/services/markdown-link';
import { Range } from '../../core/model/range';
import { isEmpty } from 'lodash';
import { toSlug } from '../../utils/slug';
export const markdownItWikilinkNavigation = (
md: markdownit,
@@ -26,18 +27,32 @@ export const markdownItWikilinkNavigation = (
isEmbed: false,
});
const formattedSection = section ? `#${section}` : '';
const linkSection = section ? `#${toSlug(section)}` : '';
const label = isEmpty(alias) ? `${target}${formattedSection}` : alias;
// [[#section]] links
if (target.length === 0) {
// we don't have a good way to check if the section exists within the
// open file, so we just create a regular link for it
return getResourceLink(section, linkSection, label);
}
const resource = workspace.find(target);
if (isNone(resource)) {
return getPlaceholderLink(label);
}
const link = `${vscode.workspace.asRelativePath(
const resourceLabel = isEmpty(alias)
? `${resource.title}${formattedSection}`
: alias;
const resourceLink = `/${vscode.workspace.asRelativePath(
toVsCodeUri(resource.uri)
)}${formattedSection}`;
const title = `${resource.title}${formattedSection}`;
return `<a class='foam-note-link' title='${title}' href='/${link}' data-href='/${link}'>${label}</a>`;
)}`;
return getResourceLink(
`${resource.title}${formattedSection}`,
`${resourceLink}${linkSection}`,
resourceLabel
);
} catch (e) {
Logger.error(
`Error while creating link for [[${wikilink}]] in Preview panel`,
@@ -52,4 +67,7 @@ export const markdownItWikilinkNavigation = (
const getPlaceholderLink = (content: string) =>
`<a class='foam-placeholder-link' title="Link to non-existing resource" href="javascript:void(0);">${content}</a>`;
const getResourceLink = (title: string, link: string, label: string) =>
`<a class='foam-note-link' title='${title}' href='${link}' data-href='${link}'>${label}</a>`;
export default markdownItWikilinkNavigation;

View File

@@ -24,20 +24,21 @@ interface FoamCommand<T> {
interface FindIdentifierCommandArgs {
range: vscode.Range;
target: vscode.Uri;
defaultExtension: string;
amongst: vscode.Uri[];
}
const FIND_IDENTIFIER_COMMAND: FoamCommand<FindIdentifierCommandArgs> = {
name: 'foam:compute-identifier',
execute: async ({ target, amongst, range }) => {
execute: async ({ target, amongst, range, defaultExtension }) => {
if (vscode.window.activeTextEditor) {
let identifier = FoamWorkspace.getShortestIdentifier(
target.path,
amongst.map(uri => uri.path)
);
identifier = identifier.endsWith('.md')
? identifier.slice(0, -3)
identifier = identifier.endsWith(defaultExtension)
? identifier.slice(0, defaultExtension.length * -1)
: identifier;
await vscode.window.activeTextEditor.edit(builder => {
@@ -97,7 +98,7 @@ export default async function activate(
}),
vscode.languages.registerCodeActionsProvider(
'markdown',
new IdentifierResolver(),
new IdentifierResolver(foam.workspace.defaultExtension),
{
providedCodeActionKinds: IdentifierResolver.providedCodeActionKinds,
}
@@ -193,6 +194,8 @@ export class IdentifierResolver implements vscode.CodeActionProvider {
vscode.CodeActionKind.QuickFix,
];
constructor(private defaultExtension: string) {}
provideCodeActions(
document: vscode.TextDocument,
range: vscode.Range | vscode.Selection,
@@ -207,7 +210,12 @@ export class IdentifierResolver implements vscode.CodeActionProvider {
);
for (const item of diagnostic.relatedInformation) {
res.push(
createFindIdentifierCommand(diagnostic, item.location.uri, uris)
createFindIdentifierCommand(
diagnostic,
item.location.uri,
this.defaultExtension,
uris
)
);
}
return [...acc, ...res];
@@ -232,12 +240,12 @@ const createReplaceSectionCommand = (
section: string
): vscode.CodeAction => {
const action = new vscode.CodeAction(
`Use ${section}`,
`${section}`,
vscode.CodeActionKind.QuickFix
);
action.command = {
command: REPLACE_TEXT_COMMAND.name,
title: `Use section ${section}`,
title: `Use section "${section}"`,
arguments: [
{
value: section,
@@ -257,10 +265,11 @@ const createReplaceSectionCommand = (
const createFindIdentifierCommand = (
diagnostic: vscode.Diagnostic,
target: vscode.Uri,
defaultExtension: string,
possibleTargets: vscode.Uri[]
): vscode.CodeAction => {
const action = new vscode.CodeAction(
`Use ${vscode.workspace.asRelativePath(target)}`,
`${vscode.workspace.asRelativePath(target)}`,
vscode.CodeActionKind.QuickFix
);
action.command = {
@@ -270,6 +279,7 @@ const createFindIdentifierCommand = (
{
target: target,
amongst: possibleTargets,
defaultExtension: defaultExtension,
range: new vscode.Range(
diagnostic.range.start.line,
diagnostic.range.start.character + 2,

View File

@@ -475,30 +475,7 @@ async function askUserForFilepathConfirmation(
});
}
/**
* Common chars that is better to avoid in file names.
* Inspired by:
* https://www.mtu.edu/umc/services/websites/writing/characters-avoid/
* https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
* Even if some might be allowed in Win or Linux, to keep things more compatible and less error prone
* we don't allow them
* Also see https://github.com/foambubble/foam/issues/1042
*/
const UNALLOWED_CHARS = '/\\#%&{}<>?*$!\'":@+`|=';
/**
* Uses the title to generate a file path.
* It sanitizes the title to remove special characters and spaces.
*
* @param resolver the resolver to use
* @returns the string path of the new note
*/
export const getPathFromTitle = async (resolver: Resolver) => {
let defaultName = await resolver.resolveFromName('FOAM_TITLE');
UNALLOWED_CHARS.split('').forEach(char => {
defaultName = defaultName.split(char).join('');
});
const defaultFilepath = URI.file(`${defaultName}.md`);
return defaultFilepath;
const defaultName = await resolver.resolveFromName('FOAM_TITLE_SAFE');
return URI.file(`${defaultName}.md`);
};

View File

@@ -102,6 +102,23 @@ describe('variable-resolver, variable resolution', () => {
expect(await resolver.resolveAll(variables)).toEqual(expected);
});
it('should resolve FOAM_TITLE_SAFE', async () => {
const foamTitle = 'My/note#title';
const variables = [
new Variable('FOAM_TITLE'),
new Variable('FOAM_TITLE_SAFE'),
];
const expected = new Map<string, string>();
expected.set('FOAM_TITLE', foamTitle);
expected.set('FOAM_TITLE_SAFE', 'My-note-title');
const givenValues = new Map<string, string>();
givenValues.set('FOAM_TITLE', foamTitle);
const resolver = new Resolver(givenValues, new Date());
expect(await resolver.resolveAll(variables)).toEqual(expected);
});
it('should resolve FOAM_DATE_* properties with current day by default', async () => {
const variables = [
new Variable('FOAM_DATE_YEAR'),

View File

@@ -10,6 +10,7 @@ import {
const knownFoamVariables = new Set([
'FOAM_TITLE',
'FOAM_TITLE_SAFE',
'FOAM_SLUG',
'FOAM_SELECTED_TEXT',
'FOAM_DATE_YEAR',
@@ -132,6 +133,9 @@ export class Resolver implements VariableResolver {
case 'FOAM_TITLE':
value = resolveFoamTitle();
break;
case 'FOAM_TITLE_SAFE':
value = resolveFoamTitleSafe(this);
break;
case 'FOAM_SLUG':
value = toSlug(await this.resolve(new Variable('FOAM_TITLE')));
break;
@@ -242,3 +246,29 @@ async function resolveFoamTitle() {
function resolveFoamSelectedText() {
return findSelectionContent()?.content ?? '';
}
/**
* Common chars that is better to avoid in file names.
* Inspired by:
* https://www.mtu.edu/umc/services/websites/writing/characters-avoid/
* https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
* Even if some might be allowed in Win or Linux, to keep things more compatible and less error prone
* we don't allow them
* Also see https://github.com/foambubble/foam/issues/1042
*/
const UNALLOWED_CHARS = '/\\#%&{}<>?*$!\'":@+`|=';
/**
* Uses the title to generate a file path.
* It sanitizes the title to remove special characters and spaces.
*
* @param resolver the resolver to use
* @returns the string path of the new note
*/
export const resolveFoamTitleSafe = async (resolver: Resolver) => {
let safeTitle = await resolver.resolveFromName('FOAM_TITLE');
UNALLOWED_CHARS.split('').forEach(char => {
safeTitle = safeTitle.split(char).join('-');
});
return safeTitle;
};

View File

@@ -0,0 +1,32 @@
import { getNotesExtensions } from './settings';
import { withModifiedFoamConfiguration } from './test/test-utils-vscode';
describe('Default note settings', () => {
it('should default to .md', async () => {
const config = getNotesExtensions();
expect(config.defaultExtension).toEqual('.md');
expect(config.notesExtensions).toEqual(['.md']);
});
it('should always include the default note extension in the list of notes extensions', async () => {
withModifiedFoamConfiguration(
'files.defaultNoteExtension',
'mdxx',
async () => {
const { notesExtensions } = getNotesExtensions();
expect(notesExtensions).toEqual(['.mdxx']);
withModifiedFoamConfiguration(
'files.notesExtensions',
'md markdown',
async () => {
const { notesExtensions } = getNotesExtensions();
expect(notesExtensions).toEqual(
expect.arrayContaining(['.mdxx', '.md', '.markdown'])
);
}
);
}
);
});
});

View File

@@ -1,5 +1,43 @@
import { workspace, GlobPattern } from 'vscode';
import { uniq } from 'lodash';
import { LogLevel } from './core/utils/log';
import { getFoamVsCodeConfig } from './services/config';
/**
* Gets the notes extensions and default extension from the config.
*
* @returns {notesExtensions: string[], defaultExtension: string}
*/
export function getNotesExtensions() {
const notesExtensionsFromSetting = getFoamVsCodeConfig(
'files.notesExtensions',
''
)
.split(' ')
.filter(ext => ext.trim() !== '')
.map(ext => '.' + ext.trim());
const defaultExtension =
'.' +
(getFoamVsCodeConfig('files.defaultNoteExtension', 'md') ?? 'md').trim();
// we make sure that the default extension is always included in the list of extensions
const notesExtensions = uniq(
notesExtensionsFromSetting.concat(defaultExtension)
);
return { notesExtensions, defaultExtension };
}
/**
* Gets the attachment extensions from the config.
*
* @returns string[]
*/
export function getAttachmentsExtensions() {
return getFoamVsCodeConfig('files.attachmentExtensions', '')
.split(' ')
.map(ext => '.' + ext.trim());
}
export function getWikilinkDefinitionSetting():
| 'withExtensions'

View File

@@ -34,12 +34,17 @@ async function main() {
// Passed to --extensionTestsPath
const extensionTestsPath = path.join(__dirname, 'suite');
const testWorkspace = path.join(
extensionDevelopmentPath,
'.test-workspace'
);
// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [
path.join(extensionDevelopmentPath, '.test-workspace'),
testWorkspace,
'--disable-extensions',
'--disable-workspace-trust',
],

View File

@@ -15,6 +15,8 @@
process.env.FORCE_COLOR = '1';
process.env.NODE_ENV = 'test';
import rf from 'rimraf';
// eslint-disable-next-line import/no-extraneous-dependencies
import { runCLI } from '@jest/core';
import { cleanWorkspace } from './test-utils-vscode';
@@ -42,6 +44,12 @@ export function run(): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
await cleanWorkspace();
const testWorkspace = path.join(__dirname, '..', '..', '.test-workspace');
// clean test workspace
rf.sync(path.join(testWorkspace, '*'));
rf.sync(path.join(testWorkspace, '.vscode'));
rf.sync(path.join(testWorkspace, '.foam'));
try {
const { results } = await runCLI(
{

View File

@@ -1,7 +1,7 @@
const NodeEnvironment = require('jest-environment-node');
const { TestEnvironment } = require('jest-environment-node');
const vscode = require('vscode');
class VscodeEnvironment extends NodeEnvironment {
class VscodeEnvironment extends TestEnvironment {
async setup() {
await super.setup();
this.global.vscode = vscode;

View File

@@ -108,10 +108,10 @@ export const runCommand = async <T>(command: string, args: T = undefined) =>
* @param fn the function to execute
*/
export const withModifiedConfiguration = async (key, value, fn: () => void) => {
const old = vscode.workspace.getConfiguration().get(key);
const old = vscode.workspace.getConfiguration().inspect(key);
await vscode.workspace.getConfiguration().update(key, value);
await fn();
await vscode.workspace.getConfiguration().update(key, old);
await vscode.workspace.getConfiguration().update(key, old.workspaceValue);
};
/**

View File

@@ -9,7 +9,7 @@ export const toVsCodePosition = (p: FoamPosition): Position =>
export const toVsCodeRange = (r: FoamRange): Range =>
new Range(r.start.line, r.start.character, r.end.line, r.end.character);
export const toVsCodeUri = (u: FoamURI): Uri => Uri.parse(u.toString());
export const toVsCodeUri = (u: FoamURI): Uri => Uri.from(u);
export const fromVsCodeUri = (u: Uri): FoamURI => FoamURI.parse(u.toString());

View File

@@ -5,7 +5,7 @@
👀*This is an early stage project under rapid development. For updates join the [Foam community Discord](https://foambubble.github.io/join-discord/g)! 💬*
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-112-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-117-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
[![Visual Studio Marketplace Installs](https://img.shields.io/visual-studio-marketplace/i/foam.foam-vscode?label=VS%20Code%20Installs)](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode)
@@ -343,6 +343,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="http://www.jim-graham.net/"><img src="https://avatars.githubusercontent.com/u/430293?v=4?s=60" width="60px;" alt="Jim Graham"/><br /><sub><b>Jim Graham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jimgraham" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://t.me/littlepoint"><img src="https://avatars.githubusercontent.com/u/7611700?v=4?s=60" width="60px;" alt="Zhizhen He"/><br /><sub><b>Zhizhen He</b></sub></a><br /><a href="#tool-hezhizhen" title="Tools">🔧</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://amnesiak.org/me"><img src="https://avatars.githubusercontent.com/u/952059?v=4?s=60" width="60px;" alt="Tony Cheneau"/><br /><sub><b>Tony Cheneau</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=tcheneau" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/nicholas-l"><img src="https://avatars.githubusercontent.com/u/12977174?v=4?s=60" width="60px;" alt="Nicholas Latham"/><br /><sub><b>Nicholas Latham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=nicholas-l" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://thara.dev"><img src="https://avatars.githubusercontent.com/u/1532891?v=4?s=60" width="60px;" alt="Tomochika Hara"/><br /><sub><b>Tomochika Hara</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=thara" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dcarosone"><img src="https://avatars.githubusercontent.com/u/11495017?v=4?s=60" width="60px;" alt="Daniel Carosone"/><br /><sub><b>Daniel Carosone</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=dcarosone" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/MABruni"><img src="https://avatars.githubusercontent.com/u/100445384?v=4?s=60" width="60px;" alt="Miguel Angel Bruni Montero"/><br /><sub><b>Miguel Angel Bruni Montero</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=MABruni" title="Code">💻</a></td>
</tr>
</tbody>
</table>

1081
yarn.lock

File diff suppressed because it is too large Load Diff