mirror of
https://github.com/foambubble/foam.git
synced 2026-01-11 06:58:11 -05:00
Compare commits
109 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
503b486179 | ||
|
|
a36d39acf8 | ||
|
|
fb92790a0a | ||
|
|
dcb951004a | ||
|
|
3b5906a1cf | ||
|
|
dc541dea2a | ||
|
|
eb908cb689 | ||
|
|
967ff18d8d | ||
|
|
89298b9652 | ||
|
|
e1694f298b | ||
|
|
61961f0c1d | ||
|
|
2822bfaa9e | ||
|
|
9af4e814ac | ||
|
|
f8f2ecbec8 | ||
|
|
6d4db373bf | ||
|
|
9149546445 | ||
|
|
4893d55ed3 | ||
|
|
53caa94013 | ||
|
|
eda46ac006 | ||
|
|
37837a314d | ||
|
|
fc084c736e | ||
|
|
ca5229f557 | ||
|
|
f96282828c | ||
|
|
c863586cd0 | ||
|
|
a6c0cc603f | ||
|
|
8ed2d17793 | ||
|
|
08aae069fe | ||
|
|
09c1426926 | ||
|
|
4694cfae8d | ||
|
|
9ca36c9d81 | ||
|
|
65367b53b4 | ||
|
|
43a2984047 | ||
|
|
b0800fd30e | ||
|
|
5cbc722929 | ||
|
|
f57b8ec9b6 | ||
|
|
0c7b1458f5 | ||
|
|
8c31b563cc | ||
|
|
ca7bfdff1d | ||
|
|
1fe786c5c2 | ||
|
|
649bd6440a | ||
|
|
7a562aa0aa | ||
|
|
0bab17c130 | ||
|
|
8121223e30 | ||
|
|
793664ac59 | ||
|
|
4c5430d2b1 | ||
|
|
ebef851f5a | ||
|
|
253ee94b1c | ||
|
|
9ffd465a32 | ||
|
|
ff3dacdbbf | ||
|
|
0a6350464b | ||
|
|
fe0228bdcc | ||
|
|
471260bdd3 | ||
|
|
a22f1b46dc | ||
|
|
318641ae04 | ||
|
|
12a4fd98c3 | ||
|
|
a93360eb1b | ||
|
|
0938de2694 | ||
|
|
a120f368c3 | ||
|
|
c028689012 | ||
|
|
27665154db | ||
|
|
659621e75d | ||
|
|
3ed6c5306c | ||
|
|
ffe7a32886 | ||
|
|
7b99804022 | ||
|
|
d24d4b1e83 | ||
|
|
d1a145545e | ||
|
|
46f3753425 | ||
|
|
4e0f6a5eeb | ||
|
|
6b79a5cad0 | ||
|
|
6756c43ab0 | ||
|
|
bcecb5d9c7 | ||
|
|
71ddc3c4bc | ||
|
|
7a10c45ed8 | ||
|
|
c669e5436b | ||
|
|
afbb40fe9b | ||
|
|
c7cdb509ce | ||
|
|
7b96936926 | ||
|
|
1d223683f1 | ||
|
|
94bf3ea469 | ||
|
|
de9224b5c7 | ||
|
|
6c0064390a | ||
|
|
2903acb34e | ||
|
|
d55b592264 | ||
|
|
e525051617 | ||
|
|
9a49c9ff66 | ||
|
|
b0727307b0 | ||
|
|
a004e61b2a | ||
|
|
1a7e633edc | ||
|
|
fb78e2baff | ||
|
|
9d143394dc | ||
|
|
249e3dd924 | ||
|
|
3398ab08ac | ||
|
|
2a197cfee5 | ||
|
|
e95aa05059 | ||
|
|
dc8df6fd1e | ||
|
|
babcbb1ec1 | ||
|
|
82e46b22ff | ||
|
|
1999b04ea2 | ||
|
|
ebc9c5761e | ||
|
|
f37f2e20a2 | ||
|
|
f11a779132 | ||
|
|
20694278a6 | ||
|
|
6a849d220b | ||
|
|
6001dc0214 | ||
|
|
93cedcc490 | ||
|
|
cad0c38965 | ||
|
|
bdb95a0832 | ||
|
|
e0580d39bf | ||
|
|
279b3b48f1 |
@@ -1139,6 +1139,51 @@
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "Tenormis",
|
||||
"name": "Tenormis",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/61572102?v=4",
|
||||
"profile": "https://github.com/Tenormis",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "djplaner",
|
||||
"name": "David Jones",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/225052?v=4",
|
||||
"profile": "http://djon.es/blog",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "s-jacob-powell",
|
||||
"name": "S. Jacob Powell",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/109111499?v=4",
|
||||
"profile": "https://github.com/s-jacob-powell",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "figdavi",
|
||||
"name": "Davi Figueiredo",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/99026991?v=4",
|
||||
"profile": "https://github.com/figdavi",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "ChThH",
|
||||
"name": "CT Hall",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/9499483?v=4",
|
||||
"profile": "https://github.com/ChThH",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 7,
|
||||
|
||||
61
.claude/commands/research-issue.md
Normal file
61
.claude/commands/research-issue.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Research Issue Command
|
||||
|
||||
Research a GitHub issue by analyzing the issue details and codebase to generate a comprehensive task analysis file.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
/research-issue <issue-number>
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- `issue-number` (required): The GitHub issue number to research
|
||||
|
||||
## Description
|
||||
|
||||
This command performs comprehensive research on a GitHub issue by:
|
||||
|
||||
1. **Fetching Issue Details**: Uses `gh issue view` to get issue title, description, labels, comments, and related information
|
||||
2. **Codebase Analysis**: Searches the codebase for relevant files, patterns, and components mentioned in the issue
|
||||
3. **Root Cause Analysis**: Identifies possible technical causes based on the issue description and codebase findings
|
||||
4. **Solution Planning**: Proposes two solution approaches ranked by preference
|
||||
5. **Documentation**: Creates a structured task file in `.agent/tasks/<issue-id>-<sanitized-title>.md`
|
||||
|
||||
If there is already a `.agent/tasks/<issue-id>-<sanitized-title>.md` file, use it for context and update it accordingly.
|
||||
If at any time during these steps you need clarifying information from me, please ask.
|
||||
|
||||
## Output Format
|
||||
|
||||
Creates a markdown file with:
|
||||
|
||||
- Issue summary and key details
|
||||
- Research findings from codebase analysis
|
||||
- Identified possible root causes
|
||||
- Two ranked solution approaches with pros/cons
|
||||
- Technical considerations and dependencies
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
/research-issue 1234
|
||||
/research-issue 567
|
||||
```
|
||||
|
||||
## Implementation
|
||||
|
||||
The command will:
|
||||
|
||||
1. Validate the issue number and check if it exists
|
||||
2. Fetch issue details using GitHub CLI
|
||||
3. Search codebase for relevant patterns, files, and components
|
||||
4. Analyze findings to identify root causes
|
||||
5. Generate structured markdown file with research results
|
||||
6. Save to `.agent/tasks/` directory with standardized naming
|
||||
|
||||
## Error Handling
|
||||
|
||||
- Invalid issue numbers
|
||||
- GitHub CLI authentication issues
|
||||
- Network connectivity problems
|
||||
- File system write permissions
|
||||
10
.devcontainer/devcontainer.json
Normal file
10
.devcontainer/devcontainer.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "Foam Dev Container",
|
||||
"image": "mcr.microsoft.com/devcontainers/typescript-node:0-18",
|
||||
"postCreateCommand": "yarn install",
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
|
||||
}
|
||||
}
|
||||
}
|
||||
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -73,4 +73,4 @@ jobs:
|
||||
- name: Run Tests
|
||||
uses: GabrielBB/xvfb-action@v1.4
|
||||
with:
|
||||
run: yarn test --stream
|
||||
run: yarn test
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,3 +11,4 @@ docs/_site
|
||||
docs/.sass-cache
|
||||
docs/.jekyll-metadata
|
||||
.test-workspace
|
||||
.agent/tasks
|
||||
|
||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -24,9 +24,8 @@
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnSaveMode": "file",
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||
"jest.autoRun": "off",
|
||||
"jest.rootPath": "packages/foam-vscode",
|
||||
"jest.jestCommandLine": "yarn jest",
|
||||
"jest.jestCommandLine": "yarn test:unit",
|
||||
"gitdoc.enabled": false,
|
||||
"search.mode": "reuseEditor",
|
||||
"[typescript]": {
|
||||
|
||||
263
CLAUDE.md
Normal file
263
CLAUDE.md
Normal file
@@ -0,0 +1,263 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Collaboration Principles
|
||||
|
||||
**Be honest and objective**: Evaluate all suggestions, ideas, and feedback on their technical merits. Don't be overly complimentary or sycophantic. If something doesn't make sense, doesn't align with best practices, or could be improved, say so directly and constructively. Technical accuracy and project quality take precedence over being agreeable.
|
||||
|
||||
## Project overview
|
||||
|
||||
Foam is a personal knowledge management and sharing system, built on Visual Studio Code and GitHub. It allows users to organize research, keep re-discoverable notes, write long-form content, and optionally publish it to the web. The main goals are to help users create relationships between thoughts and information, supporting practices like building a "Second Brain" or a "Zettelkasten". Foam is free, open-source, and extensible, giving users ownership and control over their information. The target audience includes individuals interested in personal knowledge management, note-taking, and content creation, particularly those familiar with VS Code and GitHub.
|
||||
|
||||
## Quick Commands
|
||||
|
||||
All the following commands are to be executed from the `packages/foam-vscode` directory
|
||||
|
||||
### Development
|
||||
|
||||
- `yarn install` - Install dependencies
|
||||
- `yarn build` - Build all packages
|
||||
- `yarn watch` - Watch mode for development
|
||||
- `yarn clean` - Clean build outputs
|
||||
- `yarn reset` - Full clean, install, and build
|
||||
|
||||
### Testing
|
||||
|
||||
- `yarn test` - Run all tests (unit + integration)
|
||||
- `yarn test:unit` - Run unit tests (\*.test.ts files and the .spec.ts files marked a vscode-mock friendly)
|
||||
- `yarn test:e2e` - Run only integration tests (\*.spec.ts files)
|
||||
- `yarn lint` - Run linting
|
||||
- `yarn test-reset-workspace` to clean test workspace
|
||||
|
||||
Unit tests run in Node.js environment using Jest
|
||||
Integration tests require VS Code extension host
|
||||
When running tests, do not provide additional parameters, they are ignored by the custom runner script. You cannot run just a test, you have to run the whole suite.
|
||||
|
||||
Unit tests are named `*.test.ts` and integration tests are `*.spec.ts`. These test files live alongside the code in the `src` directory. An integration test is one that has a direct or indirect dependency on `vscode` module.
|
||||
There is a mock `vscode` module that can be used to run most integration tests without starting VS Code. Tests that can use this mock are start with the line `/* @unit-ready */`.
|
||||
|
||||
- If you are interested in a test inside a `*.test.ts` file, run `yarn test:unit` or inside a `*.spec.ts` file that starts with `/* @unit-ready */` run `yarn test:unit`
|
||||
- If you are interested in a test inside a `*.spec.ts` file that does not include `/* @unit-ready */` run `yarn test`
|
||||
|
||||
While in development we mostly want to use `yarn test:unit`.
|
||||
When multiple tests are failing, look at all of them, but only focus on fixing the first one. Once that is fixed, run the test suite again and repeat the process.
|
||||
|
||||
When writing tests keep mocking to a bare minimum. Code should be written in a way that is easily testable and if I/O is necessary, it should be done in appropriate temporary directories.
|
||||
Never mock anything that is inside `packages/foam-vscode/src/core/`.
|
||||
|
||||
Use the utility functions from `test-utils.ts` and `test-utils-vscode.ts` and `test-datastore.ts`.
|
||||
|
||||
To improve readability of the tests, set up the test and tear it down within the test case (as opposed to use other functions like `beforeEach` unless it's much better to do it that way)
|
||||
|
||||
Never fix a test by adjusting the expectation if the expectation is correct, test must be fixed by addressing the issue with the code.
|
||||
|
||||
## Repository Structure
|
||||
|
||||
This is a monorepo using Yarn workspaces with the main VS Code extension in `packages/foam-vscode/`.
|
||||
|
||||
### Key Directories
|
||||
|
||||
- `packages/foam-vscode/src/core/` - Platform-agnostic business logic (NO vscode dependencies)
|
||||
- `packages/foam-vscode/src/features/` - VS Code-specific features and UI
|
||||
- `packages/foam-vscode/src/services/` - service implementations, might have VS Code dependency, but we try keep that to a minimum
|
||||
- `packages/foam-vscode/src/test/` - Test utilities and mocks
|
||||
- `docs/` - Documentation and user guides
|
||||
|
||||
### File Naming Patterns
|
||||
|
||||
Test files follow `*.test.ts` for unit tests and `*.spec.ts` for integration tests, living alongside the code in `src`. An integration test is one that has a direct or indirect dependency on `vscode` package.
|
||||
|
||||
### Important Constraint
|
||||
|
||||
Code in `packages/foam-vscode/src/core/` MUST NOT depend on the `vscode` library or any files outside the core directory. This maintains platform independence.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### Core Abstractions
|
||||
|
||||
**FoamWorkspace** - Central repository managing all resources (notes, attachments)
|
||||
|
||||
- Uses reversed trie for efficient resource lookup
|
||||
- Event-driven updates (onDidAdd, onDidUpdate, onDidDelete)
|
||||
- Handles identifier resolution for short-form linking
|
||||
|
||||
**FoamGraph** - Manages relationship graph between resources
|
||||
|
||||
- Tracks links and backlinks between resources
|
||||
- Real-time updates when workspace changes
|
||||
- Handles placeholder resources for broken links
|
||||
|
||||
**ResourceProvider Pattern** - Pluggable architecture for different file types
|
||||
|
||||
- `MarkdownProvider` for .md files
|
||||
- `AttachmentProvider` for other file types
|
||||
- Extensible for future resource types
|
||||
|
||||
**DataStore Interface** - Abstract file system operations
|
||||
|
||||
- Platform-agnostic file access with configurable filtering
|
||||
- Supports both local and remote file systems
|
||||
|
||||
### Feature Integration Pattern
|
||||
|
||||
Features are registered as functions receiving:
|
||||
|
||||
```typescript
|
||||
(context: ExtensionContext, foamPromise: Promise<Foam>) => void
|
||||
```
|
||||
|
||||
This allows features to:
|
||||
|
||||
- Register VS Code commands, providers, and event handlers
|
||||
- Access the Foam workspace when ready
|
||||
- Extend markdown-it for preview rendering
|
||||
|
||||
### Testing Conventions
|
||||
|
||||
- `*.test.ts` - Unit tests using Jest
|
||||
- `*.spec.ts` - Integration tests requiring VS Code extension host
|
||||
- Tests live alongside source code in `src/`
|
||||
- Test cases should be phrased in terms of aspects of the feature being tested (expected behaviors), as they serve both as validation of the code as well as documentation of what the expected behavior for the code is in different situations. They should include the happy paths and edge cases.
|
||||
|
||||
## Development Workflow
|
||||
|
||||
We build production code together. I handle implementation details while you guide architecture and catch complexity early.
|
||||
When working on an issue, check if a `.agent/tasks/<issue-id>-<sanitized-title>.md` exists. If not, suggest whether we should start by doing a research on it (using the `/research-issue <issue-id>`) command.
|
||||
Whenever we work together on a task, feel free to challenge my assumptions and ideas and be critical if useful.
|
||||
|
||||
## Core Workflow: Research → Plan → Implement → Validate
|
||||
|
||||
**Start every feature with:** "Let me research the codebase and create a plan before implementing."
|
||||
|
||||
1. **Research** - Understand existing patterns and architecture
|
||||
2. **Plan** - Propose approach and verify with you
|
||||
3. **Implement** - Build with tests and error handling
|
||||
4. **Validate** - ALWAYS run formatters, linters, and tests after implementation
|
||||
|
||||
- Whenever working on a feature or issue, let's always come up with a plan first, then save it to a file called `/.agent/current-plan.md`, before getting started with code changes. Update this file as the work progresses.
|
||||
- Let's use pure functions where possible to improve readability and testing.
|
||||
|
||||
### Adding New Features
|
||||
|
||||
1. Create feature in `src/features/` directory
|
||||
2. Register feature in `src/features/index.ts`
|
||||
3. Add tests (both unit and integration as needed)
|
||||
4. Update configuration in `package.json` if needed
|
||||
|
||||
### Working on an issue
|
||||
|
||||
1. Get the issue information from github
|
||||
2. Define a step by step plan for addressing the issue
|
||||
3. Create tests for the feature
|
||||
4. Starting from the first test case, implement the feature so the test passes
|
||||
|
||||
### Core Logic Changes
|
||||
|
||||
1. Modify code in `src/core/` (ensure no vscode dependencies)
|
||||
2. Add comprehensive unit tests
|
||||
3. Update integration tests in features that use the core logic
|
||||
|
||||
## Configuration
|
||||
|
||||
The extension uses VS Code's configuration system with the `foam.*` namespace.
|
||||
You can find all the settings in `/packages/foam-vscode/package.json`
|
||||
|
||||
## Common Development Tasks
|
||||
|
||||
### Extending Core Functionality
|
||||
|
||||
When adding to `src/core/`:
|
||||
|
||||
- Keep platform-agnostic (no vscode imports)
|
||||
- Add comprehensive unit tests
|
||||
- Consider impact on graph and workspace state
|
||||
- Update relevant providers if needed
|
||||
|
||||
## Dependencies
|
||||
|
||||
- **Runtime**: VS Code API, markdown parsing, file watching
|
||||
- **Development**: TypeScript, Jest, ESLint, esbuild
|
||||
- **Key Libraries**: remark (markdown parsing), lru-cache, lodash
|
||||
|
||||
The extension supports both Node.js and browser environments via separate build targets.
|
||||
|
||||
## Documentation Guidelines
|
||||
|
||||
### User Documentation (`docs/user/`)
|
||||
|
||||
Documentation in `docs/user/` must be written for non-technical users. The goal is to help novice users quickly start using features, not to explain technical implementation details.
|
||||
|
||||
**Writing Guidelines:**
|
||||
|
||||
- **Target audience**: Assume users are new to Foam and may not be technical
|
||||
- **Be concise**: Keep it short and to the point - every sentence must convey useful information
|
||||
- **Avoid repetition**: Don't repeat the same concept in different words
|
||||
- **Focus on "how to use"**: Show users what they can do and how to do it, not how it works internally
|
||||
- **Balance brevity with clarity**: Users won't read verbose documentation, but they need enough information to succeed
|
||||
- **Use examples**: Show practical use cases rather than abstract descriptions
|
||||
- **Start with the most common use case**: Lead with what most users will want to do first
|
||||
|
||||
# GitHub CLI Integration
|
||||
|
||||
To interact with the github repo we will be using the `gh` command.
|
||||
ALWAYS ask before performing a write operation on Github.
|
||||
|
||||
## Common Commands for Claude Code Integration
|
||||
|
||||
### Issues
|
||||
|
||||
```bash
|
||||
# List all issues
|
||||
gh issue list
|
||||
|
||||
# Filter issues by milestone
|
||||
gh issue list --milestone "v1.0.0"
|
||||
|
||||
# Filter issues by assignee
|
||||
gh issue list --assignee @me
|
||||
gh issue list --assignee username
|
||||
|
||||
# Filter issues by label
|
||||
gh issue list --label "bug"
|
||||
gh issue list --label "enhancement,priority-high"
|
||||
|
||||
# Filter issues by state
|
||||
gh issue list --state open
|
||||
gh issue list --state closed
|
||||
gh issue list --state all
|
||||
|
||||
# Combine filters
|
||||
gh issue list --milestone "v1.0.0" --label "bug" --assignee @me
|
||||
|
||||
# View specific issue
|
||||
gh issue view 123
|
||||
|
||||
# Create issue
|
||||
gh issue create --title "Bug fix" --body "Description"
|
||||
|
||||
# Add comment to issue
|
||||
gh issue comment 123 --body "Update comment"
|
||||
```
|
||||
|
||||
### Pull Requests
|
||||
|
||||
```bash
|
||||
# List all PRs
|
||||
gh pr list
|
||||
|
||||
# Filter PRs the same way as for filters (for example, here is by milestone)
|
||||
gh pr list --milestone "v1.0.0"
|
||||
|
||||
# View PR details
|
||||
gh pr view 456
|
||||
|
||||
# Create PR
|
||||
gh pr create --title "Feature" --body "Description"
|
||||
|
||||
# Check out PR locally
|
||||
gh pr checkout 456
|
||||
|
||||
# Add review comment
|
||||
gh pr comment 456 --body "LGTM"
|
||||
```
|
||||
@@ -1,29 +0,0 @@
|
||||
# Achieving Greater Privacy and Security
|
||||
|
||||
Foam, at its heart and committed to in its [Principles](https://foambubble.github.io/foam/principles), allows the user to control their content in a flexible and non-prescriptive manner. This extends to user preferences, or requirements depending on application and context, around both privacy and security. One way that these use cases can be met is through the use of open-source and not-for-profit mechanisms in the user's workflow to provide a functional equivalence.
|
||||
|
||||
Here are a few suggestions on increasing privacy and security when using Foam.
|
||||
## VS Codium: The Open Source build of VS Code
|
||||
|
||||
Foam is built upon VS Code, itself a Microsoft product built on top of an open source project.
|
||||
|
||||
As can be found [here](https://github.com/Microsoft/vscode/issues/60#issuecomment-161792005) the **VS Code product itself is not fully open source**. This means that its inner workings are not fully transparent, facilitating the collection and distribution of your data, as specified in its [Privacy Statement](https://devblogs.microsoft.com/visualstudio/privacy/).
|
||||
|
||||
If you prefer a fully open source editor based on the same core of VS Code (and for most intents and purposes equivalent to it), you can try [VSCodium](https://github.com/VSCodium).
|
||||
In its own introduction it is described as, "Binary releases of VS Code without MS branding/telemetry/licensing". Installation packages are easily available across Windows, Unix and Linux (or you can build it from source!).
|
||||
Access to the VS Code marketplace of add-ons remains in place, including the Foam extension.
|
||||
|
||||
The change you will notice in using VS Code versus VS Codium - simply speaking, none. It is, in just about every way you will think of, the same IDE, just without the Microsoft proprietary licence and telemetry. Your Foam experience will remain as smooth and productive as before the change.
|
||||
|
||||
## Version Control and Replication
|
||||
|
||||
In Foam's [Getting Started](https://foambubble.github.io/foam/#getting-started) section, the set up describes how to set up your notes with a GitHub repository in using the template provided. Doing so provides the user with the ability to see commits made and therefore versions of their notes, allows the user to work across devices or collaborate effectively with other users, and makes publishing to GitHub pages easy.
|
||||
It's important at the same time to point out the closed-source nature of GitHub, being owned by Microsoft.
|
||||
|
||||
One alternative approach could be to use [GitLab](https://gitlab.com/), an open source alternative to GitHub. Whilst it improves on the aspect of transparency, it does also collect usage details and sends your content across the internet.
|
||||
And of course data is still stored in clear in the cloud, making it susceptible to hacks of the service.
|
||||
|
||||
A more private approach would manage replication between devices and users with a serverless mechanism like [Syncthing](https://syncthing.net). Its continuous synchronisation means that changes in files are seen almost instantly and offers the choice of using only local network connections or securely using public relays when a local network connection is unavailable. This means that having two connected devices online will have them synchronised, but it is worth noting that the continuous synchronisation could result in corruption if two users worked on the same file simultaneously and it doesn't offer the same kind of version control that git does (though versioning support can be found and is described [here](https://docs.syncthing.net/users/versioning.html)). It is also not advisable to attempt to use a continuous synchronisation tool to sync local git repositories as the risk of corruption on the git files is high (see [here](https://forum.syncthing.net/t/can-syncthing-reliably-sync-local-git-repos-not-github/8404/18)).
|
||||
|
||||
If you need the version control and collaboration, but do not want to compromise on your privacy, the best course of action is to host the open source GitLab server software yourself. The steps (well described [here](https://www.techrepublic.com/article/how-to-set-up-a-gitlab-server-and-host-your-own-git-repositories/)) are not especially complex by any means and can be used exclusively on the local network, if required, offering a rich experience of "built-in version control, issue tracking, code review, CI/CD, and more", according to its website, [GitLab / GitLab Community Edition · GitLab](https://gitlab.com/rluna-gitlab/gitlab-ce).
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
# Big Vision
|
||||
|
||||
[[todo]]
|
||||
|
||||
- What methodologies do we want to support?
|
||||
- Zettelkasten?
|
||||
- GTD? (Get Things Done)
|
||||
- Digital gardening?
|
||||
- Blogging/publishing
|
||||
- Others?
|
||||
- Be an educational tool as much as a tool to implement these methodologies
|
||||
- What use cases are we working towards?
|
||||
-[[todo]] User round table
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[todo]: dev/todo.md "Todo"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -5,7 +5,7 @@ tags: todo, good-first-task
|
||||
# Contribution Guide
|
||||
|
||||
Foam is open to contributions of any kind, including but not limited to code, documentation, ideas, and feedback.
|
||||
This guide aims to help guide new and seasoned contributors getting around the Foam codebase. For a comprehensive guide about contributing to open-source projects in general, [see here](https://sqldbawithabeard.com/2019/11/29/how-to-fork-a-github-repository-and-contribute-to-an-open-source-project/).
|
||||
This guide aims to help guide new and seasoned contributors getting around the Foam codebase. For a comprehensive guide about contributing to open-source projects in general, [see here](https://blog.robsewell.com/blog/how-to-fork-a-github-repository-and-contribute-to-an-open-source-project/).
|
||||
|
||||
## Getting Up To Speed
|
||||
|
||||
@@ -14,6 +14,8 @@ Before you start contributing we recommend that you read the following links:
|
||||
- [[principles]] - This document describes the guiding principles behind Foam.
|
||||
- [[code-of-conduct]] - Rules we hope every contributor aims to follow, allowing everyone to participate in our community!
|
||||
|
||||
To get yourself familiar with the codebase you can also browse [this repo](https://app.komment.ai/wiki/github/foambubble/foam)
|
||||
|
||||
## Diving In
|
||||
|
||||
We understand that diving in an unfamiliar codebase may seem scary,
|
||||
@@ -24,7 +26,8 @@ Finally, the easiest way to help, is to use it and provide feedback by [submitti
|
||||
|
||||
## Contributing
|
||||
|
||||
If you're interested in contributing, this short guide will help you get things set up locally (assuming [node.js >= v16](https://nodejs.org/) and [yarn](https://yarnpkg.com/) are already installed on your system).
|
||||
If you're interested in contributing, this short guide will help you get things set up locally (assuming [node.js >= v18](https://nodejs.org/) and [yarn](https://yarnpkg.com/) are already installed on your system).
|
||||
You can also use the provided [[devcontainers]] to avoid installing dependencies locally. With the Dev Containers extension installed, open the repository in VS Code and run **Dev Containers: Reopen in Container**.
|
||||
|
||||
1. Fork the project to your Github account by clicking the "Fork" button on the top right hand corner of the project's [home repository page](https://github.com/foambubble/foam).
|
||||
2. Clone your newly forked repo locally:
|
||||
@@ -84,7 +87,7 @@ This guide assumes you read the previous instructions and you're set up to work
|
||||
|
||||
1. Now we'll use the launch configuration defined at [`.vscode/launch.json`](https://github.com/foambubble/foam/blob/main/.vscode/launch.json) to start a new extension host of VS Code. Open the "Run and Debug" Activity (the icon with the bug on the far left) and select "Run VSCode Extension" in the pop-up menu. Now hit F5 or click the green arrow "play" button to fire up a new copy of VS Code with your extension installed.
|
||||
|
||||
2. In the new extension host of VS Code that launched, open a Foam workspace (e.g. your personal one, or a test-specific one created from [foam-template](https://github.com/foambubble/foam-template)). This is strictly not necessary, but the extension won't auto-run unless it's in a workspace with a `.vscode/foam.json` file.
|
||||
2. In the new extension host of VS Code that launched, open a Foam workspace (e.g. your personal one, or a test-specific one created from [foam-template](https://github.com/foambubble/foam-template)).
|
||||
|
||||
3. Test a command to make sure it's working as expected. Open the Command Palette (Ctrl/Cmd + Shift + P) and select "Foam: Update Markdown Reference List". If you see no errors, it's good to go!
|
||||
|
||||
@@ -109,6 +112,7 @@ Feel free to modify and submit a PR if this guide is out-of-date or contains err
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[principles]: ../principles.md "Principles"
|
||||
[code-of-conduct]: code-of-conduct.md "Code of Conduct"
|
||||
[devcontainers]: devcontainers.md "Using Dev Containers"
|
||||
[recipes]: ../user/recipes/recipes.md "Recipes"
|
||||
[recommended-extensions]: ../user/getting-started/recommended-extensions.md "Recommended Extensions"
|
||||
[//end]: # "Autogenerated link references"
|
||||
|
||||
13
docs/dev/devcontainers.md
Normal file
13
docs/dev/devcontainers.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Using Dev Containers
|
||||
|
||||
Foam provides a [devcontainer](https://devcontainer.ai/) configuration to make it easy to contribute without installing Node and Yarn locally.
|
||||
|
||||
## Quick start
|
||||
|
||||
1. Install [VS Code](https://code.visualstudio.com/) and the [Dev Containers](https://aka.ms/vscode-remote/download/extension) extension.
|
||||
2. Open the Foam repository in VS Code.
|
||||
3. Run **Dev Containers: Reopen in Container** from the command palette.
|
||||
|
||||
This will build a Docker image with Node 18 and install dependencies using `yarn install`. Once ready you can run the usual build and test commands from the integrated terminal.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Foam File Format
|
||||
|
||||
This file is an example of a valid Foam file. Essentially it's just a markdown file with a bit of additional support for MediaWiki-style `[[wikilinks]]`.
|
||||
This file is an example of a valid Foam file. Essentially it's just a markdown file with a bit of additional support for MediaWiki-style `[[wikilinks]]` and note embeds.
|
||||
|
||||
Here are a few specific constraints, mainly because our tooling is a bit fragmented. Most of these should be eventually lifted, and our requirement should just be "Markdown with `[[wikilinks]]`:
|
||||
|
||||
@@ -10,7 +10,8 @@ Here are a few specific constraints, mainly because our tooling is a bit fragmen
|
||||
- This is a temporary limitation and will be lifted in future versions.
|
||||
- At least `.mdx` will be supported, but ideally we'll support any file that you can map to `Markdown` language mode in VS Code
|
||||
- **In addition to normal Markdown Links syntax you can use `[[MediaWiki]]` links.** See [[wikilinks]] for more details.
|
||||
- **You can embed other notes using `![[note]]` syntax.** This supports various modifiers like `content![[note]]` or `full-card![[note]]` to control how content is displayed.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[wikilinks]: ../user/features/wikilinks.md "Wikilinks"
|
||||
[//end]: # "Autogenerated link references"
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[wikilinks]: ../user/features/wikilinks.md 'Wikilinks'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
|
||||
@@ -1,329 +0,0 @@
|
||||
# Templates v2 Proposal <!-- omit in TOC -->
|
||||
|
||||
The current capabilities of templates is limited in some important ways. This document aims to propose a design that addresses these shortcomings.
|
||||
|
||||
**IMPORTANT: This design is merely a proposal of a design that could be implemented. It DOES NOT represent a commitment by `Foam` developers to implement the features outlined in this document. This document is merely a mechanism to facilitate discussion of a possible future direction for `Foam`.**
|
||||
|
||||
- [Introduction](#introduction)
|
||||
- [Limitations of current templating](#limitations-of-current-templating)
|
||||
- [Too much friction to create a new note](#too-much-friction-to-create-a-new-note)
|
||||
- [Manual note creation (Mouse + Keyboard)](#manual-note-creation-mouse--keyboard)
|
||||
- [Manual note creation (Keyboard)](#manual-note-creation-keyboard)
|
||||
- [Foam missing note creation](#foam-missing-note-creation)
|
||||
- [`Markdown Notes: New Note` (Keyboard)](#markdown-notes-new-note-keyboard)
|
||||
- [Foam template note creation (Keyboard)](#foam-template-note-creation-keyboard)
|
||||
- [Templating of daily notes](#templating-of-daily-notes)
|
||||
- [Templating of filepaths](#templating-of-filepaths)
|
||||
- [Goal / Philosophy](#goal--philosophy)
|
||||
- [Proposal](#proposal)
|
||||
- [Summary](#summary)
|
||||
- [Add a `${title}` and `${titleSlug}` template variables](#add-a-title-and-titleslug-template-variables)
|
||||
- [Add a `Foam: Create New Note` command and hotkey](#add-a-foam-create-new-note-command-and-hotkey)
|
||||
- [Case 1: `.foam/templates/new-note.md` doesn't exist](#case-1-foamtemplatesnew-notemd-doesnt-exist)
|
||||
- [Case 2: `.foam/templates/new-note.md` exists](#case-2-foamtemplatesnew-notemd-exists)
|
||||
- [Change missing wikilinks to use the default template](#change-missing-wikilinks-to-use-the-default-template)
|
||||
- [Add a metadata section to templates](#add-a-metadata-section-to-templates)
|
||||
- [Example](#example)
|
||||
- [Add a replacement for `dateFormat`](#add-a-replacement-for-dateformat)
|
||||
- [Add support for daily note templates](#add-support-for-daily-note-templates)
|
||||
- [Eliminate all `foam.openDailyNote` settings](#eliminate-all-foamopendailynote-settings)
|
||||
- [Summary: resulting behaviour](#summary-resulting-behaviour)
|
||||
- [`Foam: Create New Note`](#foam-create-new-note)
|
||||
- [`Foam: Open Daily Note`](#foam-open-daily-note)
|
||||
- [Navigating to missing wikilinks](#navigating-to-missing-wikilinks)
|
||||
- [`Foam: Create Note From Template`](#foam-create-note-from-template)
|
||||
- [Extensions](#extensions)
|
||||
- [More variables in templates](#more-variables-in-templates)
|
||||
- [`defaultFilepath`](#defaultfilepath)
|
||||
- [Arbitrary hotkey -> template mappings?](#arbitrary-hotkey---template-mappings)
|
||||
|
||||
## Introduction
|
||||
|
||||
Creating of new notes in Foam is too cumbersome and slow. Despite their power, Foam templates can currently only be used in very limited scenarios.
|
||||
|
||||
This proposal aims to address these issues by streamlining note creation and by allowing templates to be used everywhere.
|
||||
|
||||
## Limitations of current templating
|
||||
|
||||
### Too much friction to create a new note
|
||||
|
||||
Creating new notes should an incredibly streamlined operation. There should be no friction to creating new notes.
|
||||
|
||||
Unfortunately, all of the current methods for creating notes are cumbersome.
|
||||
|
||||
#### Manual note creation (Mouse + Keyboard)
|
||||
|
||||
1. Navigate to the directory where you want the note
|
||||
2. Click the new file button
|
||||
3. Provide a filename
|
||||
4. Manually enter the template contents you want
|
||||
|
||||
#### Manual note creation (Keyboard)
|
||||
|
||||
1. Navigate to the directory where you want the note
|
||||
2. `⌘N` to create a new file
|
||||
3. `⌘S` to save the file and give it a filename
|
||||
4. Manually enter the template contents you want
|
||||
|
||||
#### Foam missing note creation
|
||||
|
||||
1. Open an existing note in the directory where you want the note
|
||||
2. Use the wikilinks syntax to create a link to the title of the note you want to have
|
||||
3. Use `Ctrl+Click`/`F12` to create the new file
|
||||
4. Manually enter the template contents you want
|
||||
|
||||
#### `Markdown Notes: New Note` (Keyboard)
|
||||
|
||||
1. Navigate to the directory where you want the note
|
||||
2. `Shift+⌘P` to open the command pallette
|
||||
3. Type `New Note` until it appears in the list. Press `Enter/Return` to select it.
|
||||
4. Enter a title for the note
|
||||
5. Manually enter the template contents you want
|
||||
|
||||
#### Foam template note creation (Keyboard)
|
||||
|
||||
1. `Shift+⌘P` to open the command pallette
|
||||
2. Type `Create New Note From Template` until it appears in the list. Press `Enter/Return` to select it.
|
||||
3. Use the arrow keys (or type the template name) to select the template. Press `Enter/Return` to select it.
|
||||
4. Modify the filepath to match the desired directory + filename. Press `Enter/Return` to select it.
|
||||
|
||||
All of these steps are far too cumbersome. And only the last one allows the use of templates.
|
||||
|
||||
### Templating of daily notes
|
||||
|
||||
Currently `Open Daily Note` opens an otherwise empty note, with a title defined by the `foam.openDailyNote.titleFormat` setting.
|
||||
Daily notes should be able to be fully templated as well.
|
||||
|
||||
### Templating of filepaths
|
||||
|
||||
As discussed in ["Template the filepath in `openDailyNote`"](https://github.com/foambubble/foam/issues/523), it would be useful to be able to specify the default filepaths of templates. For example, many people include timestamps in their filepaths.
|
||||
|
||||
## Goal / Philosophy
|
||||
|
||||
In a sentence: **Creating a new note should be a single button press and should use templates.**
|
||||
|
||||
## Proposal
|
||||
|
||||
1. Add a new `Foam: Create New Note` that is the streamlined counterpart to the more flexible `Foam: Create New Note From Template`
|
||||
2. Use templates everywhere
|
||||
3. Add metadata into the actual templates themselves in order to template the filepaths themselves.
|
||||
|
||||
### Summary
|
||||
|
||||
This can be done through a series of changes to the way that templates are implemented:
|
||||
|
||||
1. Add a `${title}` and `${titleSlug}` template variables
|
||||
2. Add a `Foam: Create New Note` command and hotkey
|
||||
3. Change missing wikilinks to use the default template
|
||||
4. Add a metadata section to templates
|
||||
5. Add a replacement for `dateFormat`
|
||||
6. Add support for daily note templates
|
||||
7. Eliminate all `foam.openDailyNote` settings
|
||||
|
||||
I've broken it out into these steps to show that the overall proposal can be implemented piecemeal in independent PRs that build on one another.
|
||||
|
||||
### Add a `${title}` and `${titleSlug}` template variables
|
||||
|
||||
When you use `Markdown Notes: New Note`, and give it a title, the title is formatted as a filename and also used as the title in the resulting note.
|
||||
|
||||
**Example:**
|
||||
|
||||
Given the title `Living in a dream world` to `Markdown Notes: New Note`, the filename is `living-in-a-dream-world.md` and the file contents are:
|
||||
|
||||
```markdown
|
||||
# Living in a dream world
|
||||
```
|
||||
|
||||
When creating a note from a template in Foam, you should be able to use a `${title}` variable. If the template uses the `${title}` variable, the user will be prompted for a title when they create a note from a template.
|
||||
|
||||
Example:
|
||||
|
||||
Given this `.foam/templates/my_template.md` template that uses the `${title}` variable:
|
||||
|
||||
```markdown
|
||||
# ${title}
|
||||
```
|
||||
|
||||
When a user asks for a new note using this template (eg. `Foam: Create New Note From Template`), VSCode will first ask the user for a title then provide it to the template, producing:
|
||||
|
||||
```markdown
|
||||
# Living in a dream world
|
||||
```
|
||||
|
||||
There will also be a `${titleSlug}` variable made available, which will be the "slugified" version of the title (eg. `living-in-a-dream-world`). This will be useful in later steps where we want to template the filepath of a template.
|
||||
|
||||
### Add a `Foam: Create New Note` command and hotkey
|
||||
|
||||
Instead of using `Markdown Notes: New Note`, Foam itself will have a `Create New Note` command that creates notes using templates.
|
||||
|
||||
This would open use the template found at `.foam/templates/new-note.md` to create the new note.
|
||||
|
||||
`Foam: Create New Note` will offer the fastest workflow for creating a note when you don't need customization, while `Foam: Create New Note From Template` will remain to serve a fully customizable (but slower) workflow.
|
||||
|
||||
#### Case 1: `.foam/templates/new-note.md` doesn't exist
|
||||
|
||||
If `.foam/templates/new-note.md` doesn't exist, it behaves the same as `Markdown Notes: New Note`:
|
||||
|
||||
* it would ask for a title and create the note in the current directory. It would open a note with the note containing the title.
|
||||
|
||||
**Note:** this would use an implicit default template, making use of the `${title}` variable.
|
||||
|
||||
#### Case 2: `.foam/templates/new-note.md` exists
|
||||
|
||||
If `.foam/templates/new-note.md` exists:
|
||||
|
||||
* it asks for the note title and creates the note in the current directory
|
||||
|
||||
**Progress:** At this point, we have a faster way to create new notes from templates.
|
||||
|
||||
### Change missing wikilinks to use the default template
|
||||
|
||||
Clicking on a dangling/missing wikilink should be equivalent to calling `Foam: Create New Note` with the contents of the link as the title.
|
||||
That way, creating a note by navigating to a missing note uses the default template.
|
||||
|
||||
### Add a metadata section to templates
|
||||
|
||||
* The `Foam: New Note` command creates a new note in the current directory. This is a sensible default that makes it quick, but lacks flexibility.
|
||||
* The `Foam: Create New Note From Template` asks the user to confirm/customize the filepath. This is more flexible but slower since there are more steps involved.
|
||||
|
||||
Both commands use templates. It would be nice if we could template the filepaths as well as the template contents (See ["Template the filepath in `openDailyNote`"](https://github.com/foambubble/foam/issues/523) for a more in-depth discussion the benefits of filepath templating).
|
||||
|
||||
In order to template the filepath, there needs to be a place where metadata like this can be specified.
|
||||
I think this metadata should be stored alongside the templates themselves. That way, it can make use of all the same template variable available to the templates themselves.
|
||||
|
||||
Conceptually, adding metadata to the templates is similar to Markdown frontmatter, though the choice of exact syntax for adding this metadata will have to be done with care since the templates can contain arbitrary contents including frontmatter.
|
||||
|
||||
#### Example
|
||||
|
||||
A workable syntax is still to be determined.
|
||||
While this syntax probably doesn't work as a solution, for this example I will demonstrate the concept using a second frontmatter block:
|
||||
|
||||
```markdown
|
||||
<!-- The below front-matter block is for foam-specific template settings -->
|
||||
<!-- It is removed when the user creates a new note using this template -->
|
||||
---
|
||||
<!-- The default filepath to use when using this template -->
|
||||
<!-- Relative paths are relative to the workspace, absolute paths are absolute -->
|
||||
<!-- Note that you can include VSCode snippet variables to template the path -->
|
||||
filepath: `journal/${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}_${titleSlug}.md`
|
||||
---
|
||||
|
||||
<!-- The actual contents of the template begin after the `---` thematic break immediately below this line-->
|
||||
---
|
||||
---
|
||||
created: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}T${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}
|
||||
tags: []
|
||||
---
|
||||
|
||||
# ${title}
|
||||
```
|
||||
|
||||
In this example, using this template improves the UX:
|
||||
|
||||
In `Foam: Create New Note` workflow, having `filepath` metadata within `.foam/templates/new-note.md` allows for control over the filepath without having to introduce any more UX steps to create a new note. It's still just a hotkey away and a title.
|
||||
|
||||
As we'll see, when it comes to allowing daily notes to be templated, we don't even need to use `${title}` in our template, in which case we don't we don't even need to prompt for a title.
|
||||
|
||||
In the `Create New Note From Template` workflow, during the step where we allow the user to customize the filepath, it will already templated according to the `filepath` in the template's metadata. This means that the user has to make fewer changes to the path, especially in cases where they want to include things like datetimes in the filenames. This makes it faster (eg. don't have to remember what day it is, and don't have to type it) and less error-prone (eg. when they accidentally type the wrong date).
|
||||
|
||||
### Add a replacement for `dateFormat`
|
||||
|
||||
`foam.openDailyNote.filenameFormat` uses `dateFormat()` to put the current timestamp into the daily notes filename. This is much more flexible than what is available in VSCode Snippet variables. Before daily notes are switched over to use templates, we will have to come up with another mechanism/syntax to allow for calls to `dateFormat()` within template files.
|
||||
|
||||
This would be especially useful in the migration of users to the new daily notes templates. For example, if `.foam/templates/daily-note.md` is unset, then we could generate an implicit template for use by `Foam: Open Daily Note`. Very roughly something like:
|
||||
|
||||
```markdown
|
||||
<!-- The below front-matter block is for foam-specific template settings -->
|
||||
<!-- It is removed when the user creates a new note using this template -->
|
||||
---
|
||||
<!-- The default filepath to use when using this template -->
|
||||
<!-- Relative paths are relative to the workspace, absolute paths are absolute -->
|
||||
<!-- Note that you can include VSCode snippet variables to template the path -->
|
||||
filepath: `${foam.openDailyNote.directory}/${foam.openDailyNote.filenameFormat}.${foam.openDailyNote.fileExtension}`
|
||||
---
|
||||
|
||||
<!-- The actual contents of the template begin after the `---` thematic break immediately below this line-->
|
||||
---
|
||||
# ${foam.openDailyNote.titleFormat}
|
||||
```
|
||||
|
||||
### Add support for daily note templates
|
||||
|
||||
With the above features implemented, making daily notes use templates is simple.
|
||||
|
||||
We define a `.foam/templates/daily-note.md` filepath that the `Foam: Open Daily Note` command will always use to find its daily note template.
|
||||
If `.foam/templates/daily-note.md` does not exist, it falls back to a default, implicitly defined daily notes template (which follows the default behaviour of the current `foam.openDailyNote` settings).
|
||||
|
||||
Both `Foam: Open Daily Note` and `Foam: Create New Note` can share all of the implementation code, with the only differences being the hotkeys used and the template filepath used.
|
||||
|
||||
Example daily note template (again using the example syntax of the foam-specific frontmatter block):
|
||||
|
||||
```markdown
|
||||
<!-- The below front-matter block is for foam-specific template settings -->
|
||||
<!-- It is removed when the user creates a new note using this template -->
|
||||
---
|
||||
<!-- The default filepath to use when using this template -->
|
||||
<!-- Relative paths are relative to the workspace, absolute paths are absolute -->
|
||||
<!-- Note that you can include VSCode snippet variables to template the path -->
|
||||
filepath: `journal/${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}.md`
|
||||
---
|
||||
|
||||
<!-- The actual contents of the template begin after the `---` thematic break immediately below this line-->
|
||||
---
|
||||
# ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}
|
||||
```
|
||||
|
||||
Since there is no use of the `${title}` variable, opening the daily note behaves exactly as it does today and automatically opens the note with no further user interaction.
|
||||
|
||||
### Eliminate all `foam.openDailyNote` settings
|
||||
|
||||
Now that all of the functionality of the `foam.openDailyNote` settings have been obviated, these settings can be removed:
|
||||
|
||||
* `foam.openDailyNote.directory`, `foam.openDailyNote.filenameFormat`, and `foam.openDailyNote.fileExtension` can be specified in the `filepath` metadata of the daily note template.
|
||||
* `foam.openDailyNote.titleFormat` has been replaced by the ability to fully template the daily note, including the title.
|
||||
|
||||
## Summary: resulting behaviour
|
||||
|
||||
### `Foam: Create New Note`
|
||||
|
||||
A new command optimized for speedy creation of new notes. This will become the default way to create new notes. In its fastest form, it simply opens the new note with no further user interaction.
|
||||
|
||||
### `Foam: Open Daily Note`
|
||||
|
||||
Simplified since it no longer has its custom settings, and re-uses all the same implementation code as `Foam: Create New Note`.
|
||||
Templates can now be used with daily notes.
|
||||
|
||||
### Navigating to missing wikilinks
|
||||
|
||||
Now creates the new notes using the default template. Re-uses all the same implementation code as `Foam: Create New Note`
|
||||
Now uses the contents of the wikilink as the `${title}` parameter for the template.
|
||||
|
||||
### `Foam: Create Note From Template`
|
||||
|
||||
Almost the exact same as it is today. However, with `${title}` and `filepath` templating, users will have less changes to make in the filepath confirmation step.
|
||||
It's the slower but more powerful version of `Foam: Create New Note`, allowing you to pick any template, as well as customize the filepath.
|
||||
|
||||
## Extensions
|
||||
|
||||
In addition to the ideas of this proposal, there are ways we could imagine extending it. These are all "out of scope" for this design, but thinking about them could be useful to guide our thinking about this design.
|
||||
|
||||
### More variables in templates
|
||||
|
||||
`${title}` is necessary in this case to replace the functionality of `Markdown Notes: New Note`.
|
||||
However, one could imagine that this pattern of "Ask the user for a value for missing variable values" could be useful in other situations too.
|
||||
Perhaps users could even define their own (namespaced) template variables, and Foam would ask them for values to use for each when creating a note using a template that used those variables.
|
||||
|
||||
### `defaultFilepath`
|
||||
|
||||
By using `defaultFilepath` instead of `filepath` in the metadata section, you could have more control over the note creation without having to fall back to the full `Create New Note From Template` workflow.
|
||||
|
||||
* `filepath` will not ask the user for the file path, simply use the value provided (as described above)
|
||||
* `defaultFilepath` will ask the user for the file path, pre-populating the file path using `defaultFilepath`
|
||||
|
||||
The first allows "one-click" note creation, the second more customization.
|
||||
This might not be necessary, or this might not be the right way to solve the problem. We'll see.
|
||||
|
||||
### Arbitrary hotkey -> template mappings?
|
||||
|
||||
`Foam: Open Daily Note` and `Foam: Create New Note` only differ by their hotkey and their default template setting.
|
||||
Is there a reason/opportunity to abstract this further and allow for users to define custom `hotkey -> template` mappings?
|
||||
130
docs/dev/testing.md
Normal file
130
docs/dev/testing.md
Normal file
@@ -0,0 +1,130 @@
|
||||
# Testing in Foam VS Code Extension
|
||||
|
||||
This document explains the testing strategy and conventions used in the Foam VS Code extension.
|
||||
|
||||
## Test File Types
|
||||
|
||||
We use two distinct types of test files, each serving different purposes:
|
||||
|
||||
### `.test.ts` Files - Pure Unit Tests
|
||||
|
||||
- **Purpose**: Test business logic and algorithms in complete isolation
|
||||
- **Dependencies**: No VS Code APIs dependencies
|
||||
- **Environment**: Pure Jest with Node.js
|
||||
- **Speed**: Very fast execution
|
||||
- **Location**: Throughout the codebase alongside source files
|
||||
|
||||
### `.spec.ts` Files - Integration Tests with VS Code APIs
|
||||
|
||||
- **Purpose**: Test features that integrate with VS Code APIs and user workflows
|
||||
- **Dependencies**: Will likely depend on VS Code APIs (`vscode` module), otherwise avoid incurring the performance hit
|
||||
- **Environment**: Can run in TWO environments:
|
||||
- **Mock Environment**: Jest with VS Code API mocks (fast)
|
||||
- **Real VS Code**: Full VS Code extension host (slow but comprehensive)
|
||||
- **Speed**: Depends on environment (see performance section below)
|
||||
- **Location**: Primarily in `src/features/` and service layers
|
||||
|
||||
## Key Principle: Environment Flexibility for `.spec.ts` Files
|
||||
|
||||
**`.spec.ts` files use VS Code APIs**, but they can run in different environments:
|
||||
|
||||
- **Mock Environment**: Uses our VS Code API mocks for speed
|
||||
- **Real VS Code**: Uses actual VS Code extension host for full integration testing
|
||||
|
||||
This dual-environment capability allows us to:
|
||||
|
||||
- Run specs quickly during development (mock environment)
|
||||
- Verify full integration during CI/CD (real VS Code environment)
|
||||
- Gradually migrate specs to mock-compatible implementations
|
||||
|
||||
## Performance Comparison
|
||||
|
||||
| Test Type | Environment | Typical Duration | VS Code APIs |
|
||||
| --------------------- | ---------------------- | ---------------- | ---------------- |
|
||||
| **`.test.ts`** | Pure Jest | fastest | **No** |
|
||||
| **`.spec.ts` (mock)** | Jest + VS Code Mocks | fast | **Yes** (mocked) |
|
||||
| **`.spec.ts` (real)** | VS Code Extension Host | sloooooow. | **Yes** (real) |
|
||||
|
||||
## Running Tests
|
||||
|
||||
### Available Commands
|
||||
|
||||
- **`yarn test:unit`**: Runs `.test.ts` files (no VS Code dependencies) + `@unit-ready` marked `.spec.ts` files using mocks
|
||||
- **`yarn test:unit-without-specs`**: Runs only `.test.ts` files
|
||||
- **`yarn test:e2e`**: Runs all `.spec.ts` files in full VS Code extension host
|
||||
- **`yarn test`**: Runs both unit and e2e test suites sequentially
|
||||
|
||||
## Mock Environment Migration
|
||||
|
||||
We're gradually enabling `.spec.ts` files to run in our fast mock environment while maintaining their ability to run in real VS Code.
|
||||
|
||||
### The `@unit-ready` Annotation
|
||||
|
||||
Spec files marked with `/* @unit-ready */` can run in both environments:
|
||||
|
||||
```typescript
|
||||
/* @unit-ready */
|
||||
import * as vscode from 'vscode';
|
||||
// ... test uses VS Code APIs but works with our mocks
|
||||
```
|
||||
|
||||
### Common Migration Fixes
|
||||
|
||||
**Configuration defaults**: Our mocks don't load package.json defaults
|
||||
|
||||
```typescript
|
||||
// Before
|
||||
const format = getFoamVsCodeConfig('openDailyNote.filenameFormat');
|
||||
|
||||
// After (defensive)
|
||||
const format = getFoamVsCodeConfig(
|
||||
'openDailyNote.filenameFormat',
|
||||
'yyyy-mm-dd'
|
||||
);
|
||||
```
|
||||
|
||||
**File system operations**: Ensure proper async handling
|
||||
|
||||
```typescript
|
||||
// Mock file operations are immediate but still async
|
||||
await vscode.workspace.fs.writeFile(uri, content);
|
||||
```
|
||||
|
||||
### When NOT to Migrate
|
||||
|
||||
Some specs should remain real-VS-Code-only:
|
||||
|
||||
- Tests verifying complex VS Code UI interactions
|
||||
- Tests requiring real file system watching with timing
|
||||
- Tests validating extension packaging or activation
|
||||
- Tests that depend on VS Code's complex internal state management
|
||||
|
||||
## Mock System Capabilities
|
||||
|
||||
Our `vscode-mock.ts` provides comprehensive VS Code API mocking:
|
||||
|
||||
## Contributing Guidelines
|
||||
|
||||
When adding new tests:
|
||||
|
||||
1. **Choose the right type**:
|
||||
|
||||
- Use `.test.ts` for pure business logic with no VS Code dependencies
|
||||
- Use `.spec.ts` for anything that needs VS Code APIs
|
||||
|
||||
2. **Consider mock compatibility**:
|
||||
|
||||
- When writing `.spec.ts` files, consider if they could run in mock environment
|
||||
- Add `/* @unit-ready */` if the test works with our mocks
|
||||
|
||||
3. **Follow naming conventions**:
|
||||
|
||||
- Test files should be co-located with source files when possible
|
||||
- Use descriptive test names that explain the expected behavior
|
||||
|
||||
4. **Performance awareness**:
|
||||
- Prefer unit tests for business logic (fastest)
|
||||
- Use mock-compatible specs for VS Code integration (fast)
|
||||
- Reserve real VS Code specs for complex integration scenarios (comprehensive)
|
||||
|
||||
This testing strategy gives us the best of both worlds: fast feedback during development and comprehensive integration verification when needed.
|
||||
135
docs/index.md
135
docs/index.md
@@ -1,10 +1,25 @@
|
||||
# Foam
|
||||
# What is Foam?
|
||||
|
||||
**Foam** is a personal knowledge management and sharing system inspired by [Roam Research](https://roamresearch.com/), built on [Visual Studio Code](https://code.visualstudio.com/) and [GitHub](https://github.com/).
|
||||
Foam is a personal knowledge management system built on [Visual Studio Code](https://code.visualstudio.com/) and [GitHub](https://github.com/). It helps you organize research, create discoverable notes, and publish your knowledge.
|
||||
|
||||
You can use **Foam** for organising your research, keeping re-discoverable notes, writing long-form content and, optionally, publishing it to the web.
|
||||
## Key Features
|
||||
|
||||
**Foam** is free, open source, and extremely extensible to suit your personal workflow. You own the information you create with Foam, and you're free to share it, and collaborate on it with anyone you want.
|
||||
- **Wikilinks** - Connect thoughts with `[[double bracket]]` syntax
|
||||
- **Embeds** - Include content from other notes with `![[note]]` syntax
|
||||
- **Backlinks** - Automatically discover connections between notes
|
||||
- **Graph visualization** - See your knowledge network visually
|
||||
- **Daily notes** - Capture timestamped thoughts
|
||||
- **Templates** - Standardize note creation
|
||||
- **Tags** - Organize and filter content
|
||||
|
||||
## Why Choose Foam?
|
||||
|
||||
- **Free and open source** - No subscriptions or vendor lock-in
|
||||
- **Own your data** - Notes stored as standard Markdown files
|
||||
- **VS Code integration** - Leverage powerful editing and extensions
|
||||
- **Git-based** - Version control and collaboration built-in
|
||||
|
||||
Foam is like a bathtub: _What you get out of it depends on what you put into it._
|
||||
|
||||
<p class="announcement">
|
||||
<b>New!</b> Join <a href="https://foambubble.github.io/join-discord/w" target="_blank">Foam community Discord</a> for users and contributors!
|
||||
@@ -17,88 +32,79 @@ You can use **Foam** for organising your research, keeping re-discoverable notes
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Foam](#foam)
|
||||
- [What is Foam?](#what-is-foam)
|
||||
- [Key Features](#key-features)
|
||||
- [Why Choose Foam?](#why-choose-foam)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [How do I use Foam?](#how-do-i-use-foam)
|
||||
- [What's in a Foam?](#whats-in-a-foam)
|
||||
- [Getting started](#getting-started)
|
||||
- [Features](#features)
|
||||
- [Call To Adventure](#call-to-adventure)
|
||||
- [Contributing](#contributing)
|
||||
- [Thanks and attribution](#thanks-and-attribution)
|
||||
- [License](#license)
|
||||
|
||||
## How do I use Foam?
|
||||
|
||||
**Foam** is a tool that supports creating relationships between thoughts and information to help you think better.
|
||||
Foam helps you create relationships between thoughts and information through:
|
||||
|
||||
Whether you want to build a [Second Brain](https://www.buildingasecondbrain.com/) or a [Zettelkasten](https://zettelkasten.de/posts/overview/), write a book, or just get better at long-term learning, **Foam** can help you organise your thoughts if you follow these simple rules:
|
||||
1. **Atomic notes** - Write focused markdown documents on single topics
|
||||
2. **Wikilinks** - Connect ideas with `[[double bracket]]` syntax
|
||||
3. **Backlinks** - Discover unexpected connections between notes
|
||||
4. **Graph visualization** - See your knowledge network visually
|
||||
|
||||
1. Create a single **Foam** workspace for all your knowledge and research following the [Getting started](#getting-started) guide.
|
||||
2. Write your thoughts in markdown documents (I like to call them **Bubbles**, but that might be more than a little twee). These documents should be atomic: Put things that belong together into a single document, and limit its content to that single topic. ([source](https://zettelkasten.de/posts/overview/#principles))
|
||||
3. Use Foam's shortcuts and autocompletions to link your thoughts together with `[[wikilinks]]`, and navigate between them to explore your knowledge graph.
|
||||
4. Get an overview of your **Foam** workspace using a [[graph-visualization]] (⚠️ WIP), and discover relationships between your thoughts with the use of [[backlinking]].
|
||||
|
||||
Foam is a like a bathtub: _What you get out of it depends on what you put into it._
|
||||
Success with Foam depends on consistent note-taking and linking habits.
|
||||
|
||||
## What's in a Foam?
|
||||
|
||||
Like the soapy suds it's named after, **Foam** is mostly air.
|
||||
Foam combines existing tools:
|
||||
|
||||
1. The editing experience of **Foam** is powered by VS Code, enhanced by workspace settings that glue together [[recommended-extensions]] and preferences optimised for writing and navigating information.
|
||||
2. To back up, collaborate on and share your content between devices, Foam pairs well with [GitHub](http://github.com/).
|
||||
3. To publish your content, you can set it up to publish to [GitHub Pages](https://pages.github.com/), or to any website hosting platform like [Netlify](http://netlify.com/) or [Vercel](https://vercel.com).
|
||||
1. **VS Code** - Enhanced with [[recommended-extensions]] optimized for knowledge management
|
||||
2. **GitHub** - Version control, backup, and collaboration
|
||||
3. **Static site generators** - Publish to GitHub Pages, Netlify, or Vercel
|
||||
|
||||
> **Fun fact**: This documentation was researched, written and published using **Foam**.
|
||||
> This documentation was created using Foam.
|
||||
|
||||
## Getting started
|
||||
|
||||
> ⚠️ Foam is still in preview. Expect the experience to be a little rough.
|
||||
**Requirements:** GitHub account and Visual Studio Code
|
||||
|
||||
These instructions assume you have a GitHub account, and you have Visual Studio Code installed.
|
||||
|
||||
1. Use the [foam-template project](https://github.com/foambubble/foam-template) to generate a new repository. If you're logged into GitHub, you can just hit this button:
|
||||
1. **Create repository** - Use the [foam-template](https://github.com/foambubble/foam-template) to generate a new repository
|
||||
|
||||
<a class="github-button" href="https://github.com/foambubble/foam-template/generate" data-icon="octicon-repo-template" data-size="large" aria-label="Use this template foambubble/foam-template on GitHub">Use this template</a>
|
||||
|
||||
_If you want to keep your thoughts to yourself, remember to set the repository private, or if you don't want to use GitHub to host your workspace at all, choose [**Download as ZIP**](https://github.com/foambubble/foam-template/archive/main.zip) instead of **Use this template**._
|
||||
2. **Clone and open** - Clone locally and open the folder in VS Code
|
||||
3. **Install extensions** - Click "Install all" when prompted for recommended extensions
|
||||
4. **Configure** - Edit `.vscode/settings.json` for your preferences
|
||||
|
||||
2. [Clone the repository locally](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) and open it in VS Code.
|
||||
**Next steps:**
|
||||
|
||||
_Open the repository as a folder using the `File > Open...` menu item. In VS Code, "open workspace" refers to [multi-root workspaces](https://code.visualstudio.com/docs/editor/multi-root-workspaces)._
|
||||
|
||||
3. When prompted to install recommended extensions, click **Install all** (or **Show Recommendations** if you want to review and install them one by one)
|
||||
|
||||
After setting up the repository, open `.vscode/settings.json` and edit, add or remove any settings you'd like for your Foam workspace.
|
||||
|
||||
- _If using a [multi-root workspace](https://code.visualstudio.com/docs/editor/multi-root-workspaces) as noted above, make sure that your **Foam** directory is first in the list. There are some settings that will need to be migrated from `.vscode/settings.json` to your `.code-workspace` file._
|
||||
|
||||
To learn more about how to use **Foam**, read the [[recipes]].
|
||||
|
||||
Getting stuck in the setup? Read the [[frequently-asked-questions]].
|
||||
|
||||
Check our [issues on GitHub](http://github.com/foambubble/foam/issues) if you get stuck on something, and create a new one if something doesn't seem right!
|
||||
- Explore the [[recipes]] for usage patterns
|
||||
- Check [[frequently-asked-questions]] if you need help
|
||||
- Report issues on [GitHub](http://github.com/foambubble/foam/issues)
|
||||
|
||||
## Features
|
||||
|
||||
**Foam** doesn't have features in the traditional sense. Out of the box, you have access to all features of VS Code and all the [[recommended-extensions]] you choose to install, but it's up to you to discover what you can do with it!
|
||||
Foam leverages VS Code and [[recommended-extensions]] to provide:
|
||||
|
||||
- **Wikilinks** with autocomplete and navigation
|
||||
- **Backlinks** panel showing connections
|
||||
- **Graph visualization** of your knowledge network
|
||||
- **Daily notes** with templates and snippets
|
||||
- **Tag system** for organization
|
||||
- **Publishing** to static sites
|
||||
|
||||

|
||||
|
||||
Head over to [[recipes]] for some useful patterns and ideas!
|
||||
Explore [[recipes]] for usage patterns and workflows.
|
||||
|
||||
## Call To Adventure
|
||||
## Contributing
|
||||
|
||||
The goal of **Foam** is to be your personal companion on your quest for knowledge.
|
||||
Foam is an evolving project and we welcome contributions:
|
||||
|
||||
It's currently about "10% ready" relative to all the features I've thought of, but I've only thought of ~1% of the features it could have, and I'm excited to learn from others.
|
||||
|
||||
I am using it as my personal thinking tool. By making it public, I hope to learn from others not only how to improve Foam, but also to improve how I learn and manage information.
|
||||
|
||||
If that sounds like something you're interested in, I'd love to have you along on the journey.
|
||||
|
||||
- Read about our [[principles]] to understand Foam's philosophy and direction
|
||||
- Read the [[contribution-guide]] guide to learn how to participate.
|
||||
- Feel free to open [GitHub issues](https://github.com/foambubble/foam/issues) to give me feedback and ideas for new features.
|
||||
- Read our [[principles]] to understand Foam's philosophy
|
||||
- Follow the [[contribution-guide]] to get involved
|
||||
- Share feedback via [GitHub issues](https://github.com/foambubble/foam/issues)
|
||||
|
||||
## Thanks and attribution
|
||||
|
||||
@@ -266,6 +272,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://schaver.com/"><img src="https://avatars.githubusercontent.com/u/7584?v=4?s=60" width="60px;" alt="Mark Schaver"/><br /><sub><b>Mark Schaver</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=markschaver" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/n8layman"><img src="https://avatars.githubusercontent.com/u/25353944?v=4?s=60" width="60px;" alt="Nathan Layman"/><br /><sub><b>Nathan Layman</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=n8layman" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/emmanuel-ferdman"><img src="https://avatars.githubusercontent.com/u/35470921?v=4?s=60" width="60px;" alt="Emmanuel Ferdman"/><br /><sub><b>Emmanuel Ferdman</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=emmanuel-ferdman" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Tenormis"><img src="https://avatars.githubusercontent.com/u/61572102?v=4?s=60" width="60px;" alt="Tenormis"/><br /><sub><b>Tenormis</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=Tenormis" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://djon.es/blog"><img src="https://avatars.githubusercontent.com/u/225052?v=4?s=60" width="60px;" alt="David Jones"/><br /><sub><b>David Jones</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=djplaner" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/s-jacob-powell"><img src="https://avatars.githubusercontent.com/u/109111499?v=4?s=60" width="60px;" alt="S. Jacob Powell"/><br /><sub><b>S. Jacob Powell</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=s-jacob-powell" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/figdavi"><img src="https://avatars.githubusercontent.com/u/99026991?v=4?s=60" width="60px;" alt="Davi Figueiredo"/><br /><sub><b>Davi Figueiredo</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=figdavi" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ChThH"><img src="https://avatars.githubusercontent.com/u/9499483?v=4?s=60" width="60px;" alt="CT Hall"/><br /><sub><b>CT Hall</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=ChThH" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -275,20 +288,18 @@ If that sounds like something you're interested in, I'd love to have you along o
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
||||
|
||||
**Foam** was inspired by [Roam Research](https://roamresearch.com/) and the [Zettelkasten methodology](https://zettelkasten.de/posts/overview)
|
||||
Foam was inspired by [Roam Research](https://roamresearch.com/) and [Zettelkasten methodology](https://zettelkasten.de/posts/overview).
|
||||
|
||||
**Foam** wouldn't be possible without [Visual Studio Code](https://code.visualstudio.com/) and [GitHub](https://github.com/), and relies heavily on our fantastic open source [[recommended-extensions]] and all their contributors!
|
||||
Foam builds on [Visual Studio Code](https://code.visualstudio.com/), [GitHub](https://github.com/), and our [[recommended-extensions]].
|
||||
|
||||
## License
|
||||
|
||||
Foam is licensed under the [MIT license](LICENSE.txt).
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[graph-visualization]: user/features/graph-visualization.md "Graph Visualization"
|
||||
[backlinking]: user/features/backlinking.md "Backlinking"
|
||||
[recommended-extensions]: user/getting-started/recommended-extensions.md "Recommended Extensions"
|
||||
[recipes]: user/recipes/recipes.md "Recipes"
|
||||
[frequently-asked-questions]: user/frequently-asked-questions.md "Frequently Asked Questions"
|
||||
[principles]: principles.md "Principles"
|
||||
[contribution-guide]: dev/contribution-guide.md "Contribution Guide"
|
||||
[//end]: # "Autogenerated link references"
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[recommended-extensions]: user/getting-started/recommended-extensions.md 'Recommended Extensions'
|
||||
[recipes]: user/recipes/recipes.md 'Recipes'
|
||||
[frequently-asked-questions]: user/frequently-asked-questions.md 'Frequently Asked Questions'
|
||||
[principles]: principles.md 'Principles'
|
||||
[contribution-guide]: dev/contribution-guide.md 'Contribution Guide'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
# Reading list
|
||||
|
||||
- [Zettelkasten article, recommended by tchayen](https://github.com/alefore/weblog/blob/master/zettelkasten.md)
|
||||
- [Suping up VS Code as a Markdown editor](https://kortina.nyc/essays/suping-up-vs-code-as-a-markdown-notebook/)
|
||||
- [VSCode Extensions Packs](https://code.visualstudio.com/blogs/2017/03/07/extension-pack-roundup) [[todo]] Evaluate for deployment
|
||||
- [Dark mode](https://css-tricks.com/dark-modes-with-css/)
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[todo]: dev/todo.md "Todo"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -1,19 +0,0 @@
|
||||
# Terminology
|
||||
|
||||
It would be good to have some shared terminology to talk about Foam concepts. Some in-group terminology is acceptable, but we shouldn't be obtuse just to be exclusive.
|
||||
|
||||
Here's some ideas, these are open for discussion.
|
||||
|
||||
## Foam, the software project
|
||||
|
||||
The set of tools and ideas collected in this organisation.
|
||||
|
||||
## (Your) Foam
|
||||
|
||||
The directory/repository where you keep all your notes.
|
||||
|
||||
Also happens to sound quite a lot like Home. Funny, that.
|
||||
|
||||
## Bubble
|
||||
|
||||
Individual Foam note, written in Markdown.
|
||||
6
docs/user/__foam todo.md
Normal file
6
docs/user/__foam todo.md
Normal file
@@ -0,0 +1,6 @@
|
||||
- command to bootstrap workspace
|
||||
- make all extensions ON for attachments by default
|
||||
- improve settings description
|
||||
- add deprecation to daily note settings in package.json
|
||||
- JS filteres and hooks
|
||||
- plugins (compatibility with Obsidian?)
|
||||
@@ -1,16 +1,66 @@
|
||||
# Backlinking
|
||||
# Backlinks
|
||||
|
||||
When using [[wikilinks]], you can find all notes that link to a specific note in the **Connections Explorer**
|
||||
Backlinks are one of Foam's most powerful features for knowledge discovery. They automatically show you which notes reference your current note, creating a web of interconnected knowledge that reveals surprising relationships between your ideas.
|
||||
|
||||
- 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 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.
|
||||
_[📹 Watch: Understanding and using backlinks in Foam]_
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[wikilinks]: wikilinks.md "Wikilinks"
|
||||
[make-backlinks-more-prominent]: ../recipes/make-backlinks-more-prominent.md "Make Backlinks More Prominent"
|
||||
[materialized-backlinks]: ../../dev/proposals/materialized-backlinks.md "Materialized Backlinks (stub)"
|
||||
[roadmap]: ../../dev/proposals/roadmap.md "Roadmap"
|
||||
[//end]: # "Autogenerated link references"
|
||||
## What Are Backlinks?
|
||||
|
||||
A backlink is a connection from another note that points to the note you're currently viewing. While you create forward links intentionally with `[[wikilinks]]`, backlinks are discovered automatically by Foam.
|
||||
|
||||
### Forward Links vs. Backlinks
|
||||
|
||||
**Forward Links** (what you create):
|
||||
|
||||
```markdown
|
||||
# Machine Learning Note
|
||||
|
||||
I'm studying [[Neural Networks]] and [[Deep Learning]] concepts.
|
||||
```
|
||||
|
||||
**Backlinks** (what Foam discovers):
|
||||
If you're viewing the "Neural Networks" note, Foam shows you that "Machine Learning Note" links to it, even though you didn't explicitly create that reverse connection.
|
||||
|
||||
This bidirectional linking creates a richer knowledge network than traditional hierarchical folders.
|
||||
|
||||
## Accessing Backlinks - Connections Panel
|
||||
|
||||
The Connections panel shows both forward links and backlinks:
|
||||
|
||||
1. **Open Command Palette** (`Ctrl+Shift+P` / `Cmd+Shift+P`)
|
||||
2. **Type "connections"** and select "Explorer: Focus on Connections"
|
||||
3. **Use the filter buttons** to show only backlinks, forward links, or all connections
|
||||
|
||||
_[📹 Watch: Finding and opening the backlinks panel]_
|
||||
|
||||
## Using Backlinks for Knowledge Discovery
|
||||
|
||||
### 1. Finding Unexpected Connections
|
||||
|
||||
Backlinks often reveal relationships you didn't consciously create:
|
||||
|
||||
**Example:** While reviewing a "Productivity" note, backlinks might show connections from:
|
||||
|
||||
- A cooking recipe (time management for meal prep)
|
||||
- A fitness routine (efficient workout planning)
|
||||
- A work project (team productivity strategies)
|
||||
|
||||
These diverse connections can spark new insights and cross-domain learning.
|
||||
|
||||
### 2. Identifying Important Concepts
|
||||
|
||||
Notes with many backlinks are often central to your thinking:
|
||||
|
||||
- **Hub concepts** that connect many ideas
|
||||
- **Frequently referenced** resources or definitions
|
||||
- **Bridge topics** that span multiple domains
|
||||
|
||||
### 3. Building Context Around Ideas
|
||||
|
||||
Backlinks provide context for how you use concepts across different areas:
|
||||
|
||||
- How you apply the same principle in various projects
|
||||
- Evolution of your thinking about a topic over time
|
||||
- Different perspectives you've encountered on the same idea
|
||||
|
||||
_[📹 Watch: Using backlinks for knowledge discovery and research]_
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
# 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.
|
||||
|
||||

|
||||
|
||||
## 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]]`.
|
||||
@@ -1,23 +1,48 @@
|
||||
# Daily Notes
|
||||
|
||||
Daily notes allow you to quickly create and access a new notes file for each day. This is a surprisingly effective and increasingly common strategy to organize notes and manage events.
|
||||
Daily notes allow you to quickly create and access a note file for each day.
|
||||
|
||||
View today's note file by running the `Foam: Open Daily Note` command, by using the shortcut `alt+d` (note: shortcuts can be [overridden](https://code.visualstudio.com/docs/getstarted/keybindings)), or by using [#snippets](#Snippets). The name, location, and title of daily notes files are [#configurable](#Configuration).
|
||||
## Creating Daily Notes
|
||||
|
||||
## Roam-style Automatic Daily Notes
|
||||
- **Command:** `Ctrl+Shift+P` → "Foam: Open Daily Note"
|
||||
- **Shortcut:** `Alt+D`
|
||||
- **Snippets:** Type `/today`, `/yesterday`, `/tomorrow` in any note
|
||||
|
||||
You can automatically open today's note on startup by setting the `Foam › Open Daily Note: On Startup` setting to `true`.
|
||||
## Automatic Daily Notes
|
||||
|
||||
Open daily note automatically on VS Code startup:
|
||||
|
||||
```json
|
||||
{
|
||||
"foam.openDailyNote.onStartup": true
|
||||
}
|
||||
```
|
||||
|
||||
## Daily Note Templates
|
||||
|
||||
Daily notes can also make use of [[Note Templates]], by defining a special `.foam/templates/daily-note.md` template.
|
||||
Create `.foam/templates/daily-note.md` to customize the structure:
|
||||
|
||||
## Snippets
|
||||
```markdown
|
||||
---
|
||||
type: daily-note
|
||||
---
|
||||
|
||||
Create a link to a recent daily note using [snippets](https://code.visualstudio.com/docs/editor/userdefinedsnippets). Type `/today` and press `enter` to link to today's note. You can also write:
|
||||
# Daily Note - $FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE
|
||||
|
||||
## Tasks
|
||||
|
||||
- [ ]
|
||||
|
||||
## Notes
|
||||
```
|
||||
|
||||
## Date Snippets
|
||||
|
||||
Create links to recent daily notes using snippets:
|
||||
|
||||
| Snippet | Date |
|
||||
| ------------ | ------------- |
|
||||
| `/today` | today |
|
||||
| `/tomorrow` | tomorrow |
|
||||
| `/yesterday` | yesterday |
|
||||
| `/monday` | next Monday |
|
||||
@@ -29,31 +54,13 @@ Create a link to a recent daily note using [snippets](https://code.visualstudio.
|
||||
|
||||
## Configuration
|
||||
|
||||
By default, Daily Notes will be created in a file called `yyyy-mm-dd.md` in the workspace's `journals` folder, with the heading `yyyy-mm-dd`.
|
||||
By default, daily notes are created as `yyyy-mm-dd.md` in the workspace's `journals` folder.
|
||||
|
||||
These settings can be overridden in your workspace or global `.vscode/settings.json` file, using the [**dateformat** date masking syntax](https://github.com/felixge/node-dateformat#mask-options):
|
||||
To customize your daily note location and format you can create a `.foam/templates/daily-note.md` template. See [[templates]] for more information.
|
||||
|
||||
It's possible to customize the path and heading of your daily notes, by following the [dateformat masking syntax](https://github.com/felixge/node-dateformat#mask-options).
|
||||
The following properties can be used:
|
||||
There are also some settings to customize the behavior of daily notes, but they are deprecated and will be removed. Please use the `daily-note.md` template.
|
||||
|
||||
```json
|
||||
"foam.openDailyNote.directory": "journal",
|
||||
"foam.openDailyNote.filenameFormat": "'daily-note'-yyyy-mm-dd",
|
||||
"foam.openDailyNote.fileExtension": "mdx",
|
||||
"foam.openDailyNote.titleFormat": "'Journal Entry, ' dddd, mmmm d",
|
||||
```
|
||||
|
||||
The above configuration would create a file `journal/daily-note-2020-07-25.mdx`, with the heading `Journal Entry, Sunday, July 25`.
|
||||
|
||||
> NOTE: It is possible to set the filepath of a daily note according to the date using the special [[note-properties]] configurable for [[Note Templates]]. Specifically, see [[note-templates#Example of date-based|Example of date-based filepath]]. Using the template property will override any setting configured through `.vscode/settings.json`.
|
||||
|
||||
## Extend Functionality (Weekly, Monthly, Quarterly Notes)
|
||||
|
||||
Please see [[note-macros]]
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[Note Templates]: note-templates.md "Note Templates"
|
||||
[note-properties]: note-properties.md "Note Properties"
|
||||
[note-templates#Example of date-based|Example of date-based filepath]: note-templates.md "Note Templates"
|
||||
[note-macros]: ../recipes/note-macros.md "Custom Note Macros"
|
||||
[note-templates]: note-templates.md "Note Templates"
|
||||
[//end]: # "Autogenerated link references"
|
||||
|
||||
134
docs/user/features/embeds.md
Normal file
134
docs/user/features/embeds.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# Note Embeds
|
||||
|
||||
Embeds allow you to include content from other notes directly into your current note. This is powerful for creating dynamic content that updates automatically when the source note changes.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
Use the embed syntax with an exclamation mark before the wikilink:
|
||||
|
||||
```markdown
|
||||
![[note-name]]
|
||||
```
|
||||
|
||||
This will embed the entire content of `note-name` into your current note.
|
||||
|
||||
## Embedding Sections
|
||||
|
||||
You can embed specific sections of a note by referencing the heading:
|
||||
|
||||
```markdown
|
||||
![[note-name#Section Title]]
|
||||
```
|
||||
|
||||
## Embed Types
|
||||
|
||||
Foam supports different embedding scopes and styles that can be configured globally or overridden per embed.
|
||||
|
||||
### Scope Modifiers
|
||||
|
||||
- **`full`** - Include the entire note or section, including the title/heading
|
||||
- **`content`** - Include everything except the title/heading
|
||||
|
||||
Examples:
|
||||
|
||||
```markdown
|
||||
full![[my-note]] # Include title + content
|
||||
content![[my-note]] # Content only, no title
|
||||
```
|
||||
|
||||
### Style Modifiers
|
||||
|
||||
- **`card`** - Display the embedded content in a bordered container
|
||||
- **`inline`** - Display the content seamlessly as part of the current note
|
||||
|
||||
Examples:
|
||||
|
||||
```markdown
|
||||
card![[my-note]] # Bordered container
|
||||
inline![[my-note]] # Seamless integration
|
||||
```
|
||||
|
||||
### Combined Modifiers
|
||||
|
||||
You can combine scope and style modifiers:
|
||||
|
||||
```markdown
|
||||
full-card![[my-note]] # Title + content in bordered container
|
||||
content-inline![[my-note]] # Content only, seamlessly integrated
|
||||
full-inline![[my-note]] # Title + content, seamlessly integrated
|
||||
content-card![[my-note]] # Content only in bordered container
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Set your default embed behavior in VS Code settings:
|
||||
|
||||
```json
|
||||
{
|
||||
"foam.preview.embedNoteType": "full-card"
|
||||
}
|
||||
```
|
||||
|
||||
Available options:
|
||||
|
||||
- `full-card` (default)
|
||||
- `full-inline`
|
||||
- `content-card`
|
||||
- `content-inline`
|
||||
|
||||
## Image Sizing
|
||||
|
||||
Resize images to make your documents more readable:
|
||||
|
||||
```markdown
|
||||
![[image.png|300]] # 300 pixels wide
|
||||
![[image.png|50%]] # Half the container width
|
||||
```
|
||||
|
||||
### Common Use Cases
|
||||
|
||||
**Make large screenshots readable:**
|
||||
```markdown
|
||||
![[screenshot.png|600]]
|
||||
```
|
||||
|
||||
**Create responsive images:**
|
||||
```markdown
|
||||
![[diagram.png|70%]]
|
||||
```
|
||||
|
||||
**Size by width and height:**
|
||||
```markdown
|
||||
![[image.png|300x200]]
|
||||
```
|
||||
|
||||
### Alignment
|
||||
|
||||
Center, left, or right align images:
|
||||
|
||||
```markdown
|
||||
![[image.png|300|center]]
|
||||
![[image.png|300|left]]
|
||||
![[image.png|300|right]]
|
||||
```
|
||||
|
||||
### Alt Text
|
||||
|
||||
Add descriptions for accessibility:
|
||||
|
||||
```markdown
|
||||
![[chart.png|400|Monthly sales chart]]
|
||||
```
|
||||
|
||||
### Units
|
||||
|
||||
- `300` or `300px` - pixels (default)
|
||||
- `50%` - percentage of container
|
||||
- `20em` - relative to font size
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
- Check image path: `![[path/to/image.png|300]]`
|
||||
- No spaces around pipes: `|300|` not `| 300 |`
|
||||
- Images only resize in preview mode, not edit mode
|
||||
- Use lowercase alignment: `center` not `Center`
|
||||
@@ -1,10 +1,23 @@
|
||||
# Graph Visualization
|
||||
|
||||
Foam comes with a graph visualization of your notes.
|
||||
The graph view is one of Foam's most powerful features. It transforms your collection of notes into a visual network, revealing connections between ideas that might not be obvious when reading individual notes. This guide will teach you how to use the graph view to explore, understand, and expand your knowledge base.
|
||||
|
||||
To see the graph execute the `Foam: Show Graph` command.
|
||||
|
||||
Your files, such as notes and documents, are shown as the nodes of the graph along with the tags defined in your notes. The edges of the graph represent either a link between two files or a file that contains a certain tag. A node in the graph will grow in size with the number of connections it has, representing stronger or more defined concepts and topics.
|
||||
|
||||
### The `Show Graph` command
|
||||
|
||||
1. **Press `Ctrl+Shift+P` / `Cmd+Shift+P`**
|
||||
2. **Type "Foam: Show Graph"**
|
||||
3. **Press Enter**
|
||||
|
||||
You can set up a custom keyboard shortcut:
|
||||
|
||||
1. **Go to File > Preferences > Keyboard Shortcuts**
|
||||
2. **Search for "Foam: Show Graph"**
|
||||
3. **Assign your preferred shortcut**
|
||||
|
||||
## Graph Navigation
|
||||
|
||||
With the Foam graph visualization you can:
|
||||
@@ -38,6 +51,7 @@ A sample configuration object is provided below, you can provide as many or as l
|
||||
"foam.graph.style": {
|
||||
"background": "#202020",
|
||||
"fontSize": 12,
|
||||
"fontFamily": "Sans-Serif",
|
||||
"lineColor": "#277da1",
|
||||
"lineWidth": 0.2,
|
||||
"particleWidth": 1.0,
|
||||
@@ -50,6 +64,7 @@ A sample configuration object is provided below, you can provide as many or as l
|
||||
|
||||
- `background` background color of the graph, adjust to increase contrast
|
||||
- `fontSize` size of the title font for each node
|
||||
- `fontFamily` font of the title font for each node
|
||||
- `lineColor` color of the edges between nodes in the graph
|
||||
- `lineWidth` thickness of the edges between nodes
|
||||
- `particleWidth` size of the particle animation showing link direction when highlighting a node
|
||||
@@ -99,8 +114,19 @@ Will result in the following graph:
|
||||
|
||||

|
||||
|
||||
## What's Next?
|
||||
|
||||
With graph view mastery, you're ready to explore advanced Foam features:
|
||||
|
||||
1. **[[wikilinks]]** - Understand bidirectional connections
|
||||
2. **[[templates]]** - Use templates effectively to standardize your note creation
|
||||
3. **[[tags]]** - Organize your notes with tags
|
||||
4. **[[daily-notes]]** - Set up daily notes to establish capture routines
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[note-properties]: note-properties.md "Note Properties"
|
||||
[wikilinks]: wikilinks.md "Wikilinks"
|
||||
[tags]: tags.md "Tags"
|
||||
[templates]: templates.md "Note Templates"
|
||||
[daily-notes]: daily-notes.md "Daily Notes"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -1,23 +0,0 @@
|
||||
# Including notes in a note
|
||||
|
||||
In some situations it might be useful to include the content of another note in your current note. Foam supports this displaying within the vscode environment. Note, this does not work out-of-the-box for your publishing solutions.
|
||||
|
||||
## Including a note
|
||||
|
||||
Including a note can be done by adding an `!` before a wikilink definition. For example `![[wikilink]]`.
|
||||
|
||||
## Custom styling
|
||||
|
||||
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'
|
||||
[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'
|
||||
@@ -1,89 +1,65 @@
|
||||
# Link Reference Definitions
|
||||
|
||||
When you use `[[wikilinks]]`, the [foam-vscode](https://github.com/foambubble/foam/tree/main/packages/foam-vscode) extension can automatically generate [Markdown Link Reference Definitions](https://spec.commonmark.org/0.29/#link-reference-definitions) at the bottom of the file. This is not needed to navigate your workspace with foam-vscode, but is useful for files to remain compatible with various Markdown tools (e.g. parsers, static site generators, VS code plugins etc), which don't support `[[wikilinks]]`.
|
||||
Link reference definitions make your notes compatible with standard Markdown processors by converting wikilinks to standard Markdown references.
|
||||
|
||||
## Example
|
||||
Foam doesn't need references in order to work, but this feature is aimed at supporting other tools you might want to integrate with.
|
||||
|
||||
The following example:
|
||||
## What Are Link Reference Definitions?
|
||||
|
||||
```md
|
||||
- [[wikilinks]]
|
||||
- [[github-pages]]
|
||||
Foam can automatically add reference definitions to the bottom of your notes:
|
||||
|
||||
**Your note:**
|
||||
|
||||
```markdown
|
||||
# Machine Learning
|
||||
|
||||
Related to [[Data Science]] and [[Statistics]].
|
||||
```
|
||||
|
||||
...generates the following link reference definitions to the bottom of the file:
|
||||
**With reference definitions:**
|
||||
|
||||
```md
|
||||
[wikilinks]: wikilinks 'Wikilinks'
|
||||
[github-pages]: github-pages 'GitHub Pages'
|
||||
```markdown
|
||||
# Machine Learning
|
||||
|
||||
Related to [[Data Science]] and [[Statistics]].
|
||||
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[Data Science]: data-science.md 'Data Science'
|
||||
[Statistics]: statistics.md 'Statistics'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
```
|
||||
|
||||
You can open the [raw markdown](https://foambubble.github.io/foam/features/link-reference-definitions.md) to see them at the bottom of this file
|
||||
You can open the [raw markdown](https://foambubble.github.io/foam/user/features/link-reference-definitions.md) to see them at the bottom of this file
|
||||
## Enabling Reference Definitions
|
||||
|
||||
## Specification
|
||||
|
||||
The three components of a link reference definition are `[link-label]: link-target "Link Title"`
|
||||
|
||||
- **link label:** The link text to match in the surrounding markdown document. This matches the inner bracket of the double-bracketed `[[wikilink]]` notation
|
||||
- **link destination** The target of the matched link
|
||||
- By default we generate links without extension. This can be overridden, see [Configuration](#configuration) below
|
||||
- **"Link Title"** Optional title for link (The Foam template has a snippet of JavaScript to replace this on the website at runtime)
|
||||
|
||||
## Configuration
|
||||
|
||||
You can choose to generate link reference definitions with or without file extensions, depending on the target, or to disable the generation altogether. As a rule of thumb:
|
||||
|
||||
- Links with file extensions work better with standard markdown-based tools, such as GitHub web UI.
|
||||
- Links without file extensions work better with certain web publishing tools that treat links as literal urls and don't transform them automatically, such as the standard GitHub pages installation.
|
||||
|
||||
By default, Foam generates links without file extensions for legacy reasons, but this may change in future versions.
|
||||
|
||||
You can override this setting in your Foam workspace's `settings.json`:
|
||||
|
||||
- `"foam.edit.linkReferenceDefinitions": "withoutExtensions"` (default)
|
||||
- `"foam.edit.linkReferenceDefinitions": "withExtensions"`
|
||||
- `"foam.edit.linkReferenceDefinitions": "off"`
|
||||
|
||||
### Ignoring files
|
||||
|
||||
Sometimes, you may want to ignore certain files or folders, so that Foam doesn't generate link reference definitions to them.
|
||||
|
||||
There are three options for excluding files from your Foam project:
|
||||
|
||||
1. `files.exclude` (from VSCode) will prevent the folder from showing in the file explorer.
|
||||
|
||||
> "Configure glob patterns for excluding files and folders. For example, the file explorer decides which files and folders to show or hide based on this setting. Refer to the Search: Exclude setting to define search-specific excludes."
|
||||
|
||||
2. `files.watcherExclude` (from VSCode) prevents VSCode from constantly monitoring files for changes.
|
||||
|
||||
> "Configure paths or glob patterns to exclude from file watching. Paths or basic glob patterns that are relative (for example `build/output` or `*.js`) will be resolved to an absolute path using the currently opened workspace. Complex glob patterns must match on absolute paths (i.e. prefix with `**/` or the full path and suffix with `/**` to match files within a path) to match properly (for example `**/build/output/**` or `/Users/name/workspaces/project/build/output/**`). When you experience the file watcher process consuming a lot of CPU, make sure to exclude large folders that are of less interest (such as build output folders)."
|
||||
|
||||
3. `foam.files.ignore` (from Foam) ignores files from being added to the Foam graph.
|
||||
|
||||
> "Specifies the list of globs that will be ignored by Foam (e.g. they will not be considered when creating the graph). To ignore the all the content of a given folder, use `<folderName>/**/*`" (requires reloading VSCode to take effect).
|
||||
|
||||
For instance, if you're using a local instance of [Jekyll](https://jekyllrb.com/), you may find that it writes copies of each `.md` file into a `_site` directory, which may lead to Foam generating references to them instead of the original source notes.
|
||||
|
||||
You can ignore the `_site` directory by adding any of the following settings to your `.vscode/settings.json` file:
|
||||
Configure in your settings:
|
||||
|
||||
```json
|
||||
"files.exclude": {
|
||||
"**/_site": true
|
||||
},
|
||||
"files.watcherExclude": {
|
||||
"**/_site": true
|
||||
},
|
||||
"foam.files.ignore": [
|
||||
"_site/**/*"
|
||||
]
|
||||
{
|
||||
"foam.edit.linkReferenceDefinitions": "withExtensions"
|
||||
}
|
||||
```
|
||||
|
||||
After changing the setting in your workspace, you can run the [[workspace-janitor]] command to convert all existing definitions.
|
||||
**Options:**
|
||||
|
||||
- `"off"` - Disabled (default)
|
||||
- `"withoutExtensions"` - References without extension
|
||||
- `"withExtensions"` - References with extension
|
||||
|
||||
If you are using your notes only within Foam, you can keep definitions `off` (also to reduce clutter), otherwise pick your setting based on what is required by your use case.
|
||||
|
||||
## How It Works
|
||||
|
||||
1. Scans your note for wikilinks
|
||||
2. Generates reference definitions when you save
|
||||
3. Updates definitions when links change
|
||||
4. Maintains the auto-generated section
|
||||
|
||||
## Benefits
|
||||
|
||||
- **Standard Markdown compatibility** - Works with any Markdown processor
|
||||
- **Publishing platforms** - Compatible with GitHub Pages, Jekyll, etc.
|
||||
- **Future-proofing** - Not locked into Foam-specific format
|
||||
- **Team collaboration** - Others can read notes without Foam
|
||||
|
||||
|
||||
See [[link-reference-definition-improvements]] for further discussion on current problems and potential solutions.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[workspace-janitor]: ../tools/workspace-janitor.md "Janitor"
|
||||
[link-reference-definition-improvements]: ../../dev/proposals/link-reference-definition-improvements.md "Link Reference Definition Improvements"
|
||||
[//end]: # "Autogenerated link references"
|
||||
|
||||
@@ -29,8 +29,8 @@ Some properties have special meaning for Foam:
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `title` | will assign the name to the note that you will see in the graph, regardless of the filename or the first heading (also see how to [[write-notes-in-foam]]) |
|
||||
| `type` | can be used to style notes differently in the graph (also see [[graph-visualization]]). The default type for a document is `note` unless otherwise specified with this property. |
|
||||
| `title` | will assign the name to the note that you will see in the graph, regardless of the filename or the first heading (also see how to [[note-taking-in-foam]]) |
|
||||
| `type` | can be used to style notes differently in the graph (also see [[graph-view]]). The default type for a document is `note` unless otherwise specified with this property. |
|
||||
| `tags` | can be used to add tags to a note (see [[tags]]) |
|
||||
| `alias` | can be used to add aliases to the note. an alias will show up in the link autocompletion |
|
||||
|
||||
@@ -47,7 +47,7 @@ alias: alias1, alias2
|
||||
|
||||
## Foam Template Properties
|
||||
|
||||
There also exists properties that are even more specific to Foam templates, see [[note-templates#Metadata]] for more info.
|
||||
There also exists properties that are even more specific to Foam templates, see [[templates#Metadata]] for more info.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[write-notes-in-foam]: ../getting-started/write-notes-in-foam.md "Writing Notes"
|
||||
|
||||
@@ -1,221 +0,0 @@
|
||||
# Note Templates
|
||||
|
||||
Foam supports note templates which let you customize the starting content of your notes instead of always starting from an empty note.
|
||||
|
||||
Note templates are `.md` files located in the special `.foam/templates` directory of your workspace.
|
||||
|
||||
## Quickstart
|
||||
|
||||
Create a template:
|
||||
|
||||
- Run the `Foam: Create New Template` command from the command palette
|
||||
- OR manually create a regular `.md` file in the `.foam/templates` directory
|
||||
|
||||

|
||||
|
||||
_Theme: Ayu Light_
|
||||
|
||||
To create a note from a template:
|
||||
|
||||
- Run the `Foam: Create New Note From Template` command and follow the instructions. Don't worry if you've not created a template yet! You'll be prompted to create a new template if none exist.
|
||||
- OR run the `Foam: Create New Note` command, which uses the special default template (`.foam/templates/new-note.md`, if it exists)
|
||||
|
||||

|
||||
|
||||
_Theme: Ayu Light_
|
||||
|
||||
## Special templates
|
||||
|
||||
### Default template
|
||||
|
||||
The `.foam/templates/new-note.md` template is special in that it is the template that will be used by the `Foam: Create New Note` command.
|
||||
Customize this template to contain content that you want included every time you create a note. To begin it is _recommended_ to define the YAML Front-Matter of the template similar to the following:
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: basic-note
|
||||
---
|
||||
```
|
||||
|
||||
### Default daily note template
|
||||
|
||||
The `.foam/templates/daily-note.md` template is special in that it is the template that will be used when creating daily notes (e.g. by using `Foam: Open Daily Note`).
|
||||
Customize this template to contain content that you want included every time you create a daily note. To begin it is _recommended_ to define the YAML Front-Matter of the template similar to the following:
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: daily-note
|
||||
---
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
Templates can use all the variables available in [VS Code Snippets](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables).
|
||||
|
||||
In addition, you can also use variables provided by Foam:
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `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. |
|
||||
|
||||
### `FOAM_DATE_*` variables
|
||||
|
||||
Foam defines its own set of datetime variables that have a similar behaviour as [VS Code's datetime snippet variables](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables).
|
||||
|
||||
For example, `FOAM_DATE_YEAR` has the same behaviour as VS Code's `CURRENT_YEAR`, `FOAM_DATE_SECONDS_UNIX` has the same behaviour as `CURRENT_SECONDS_UNIX`, etc.
|
||||
|
||||
By default, prefer using the `FOAM_DATE_` versions. The datetime used to compute the values will be the same for both `FOAM_DATE_` and VS Code's variables, with the exception of the creation notes using the daily note template.
|
||||
|
||||
For more nitty-gritty details about the supported date formats, [see here](https://github.com/foambubble/foam/blob/main/packages/foam-vscode/src/services/variable-resolver.ts).
|
||||
|
||||
#### Relative daily notes
|
||||
|
||||
When referring to daily notes, you can use the relative snippets (`/+1d`, `/tomorrow`, etc.). In these cases, the new notes will be created with the daily note template, but the datetime used should be the relative datetime, not the current datetime.
|
||||
By using the `FOAM_DATE_` versions of the variables, the correct relative date will populate the variables, instead of the current datetime.
|
||||
|
||||
For example, given this daily note template (`.foam/templates/daily-note.md`):
|
||||
|
||||
```markdown
|
||||
# $FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE
|
||||
|
||||
## Here's what I'm going to do today
|
||||
|
||||
- Thing 1
|
||||
- Thing 2
|
||||
```
|
||||
|
||||
When the `/tomorrow` snippet is used, `FOAM_DATE_` variables will be populated with tomorrow's date, as expected.
|
||||
If instead you were to use the VS Code versions of these variables, they would be populated with today's date, not tomorrow's, causing unexpected behaviour.
|
||||
|
||||
When creating notes in any other scenario, the `FOAM_DATE_` values are computed using the same datetime as the VS Code ones, so the `FOAM_DATE_` versions can be used in all scenarios by default.
|
||||
|
||||
## Metadata
|
||||
|
||||
Templates can also contain metadata about the templates themselves. The metadata is defined in YAML "Frontmatter" blocks within the templates.
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `filepath` | The filepath to use when creating the new note. If the filepath is a relative filepath, it is relative to the current workspace. |
|
||||
| `name` | A human readable name to show in the template picker. |
|
||||
| `description` | A human readable description to show in the template picker. |
|
||||
|
||||
Foam-specific variables (e.g. `$FOAM_TITLE`) can be used within template metadata. However, VS Code snippet variables are ([currently](https://github.com/foambubble/foam/pull/655)) not supported.
|
||||
|
||||
### `filepath` attribute
|
||||
|
||||
The `filepath` metadata attribute allows you to define a relative or absolute filepath to use when creating a note using the template. If the filepath is a relative filepath, it is relative to the current workspace.
|
||||
|
||||
#### Example of **relative** `filepath`
|
||||
|
||||
For example, `filepath` can be used to customize `.foam/templates/new-note.md`, overriding the default `Foam: Create New Note` behaviour of opening the file in the same directory as the active file:
|
||||
|
||||
```yaml
|
||||
---
|
||||
# This will create the note in the "journal" subdirectory of the current workspace,
|
||||
# regardless of which file is the active file.
|
||||
foam_template:
|
||||
filepath: 'journal/$FOAM_TITLE.md'
|
||||
---
|
||||
```
|
||||
|
||||
#### Example of **absolute** `filepath`
|
||||
|
||||
`filepath` can be an absolute filepath, so that the notes get created in the same location, regardless of which file or workspace the editor currently has open.
|
||||
The format of an absolute filepath may vary depending on the filesystem used.
|
||||
|
||||
```yaml
|
||||
---
|
||||
foam_template:
|
||||
# Unix / MacOS filesystems
|
||||
filepath: '/Users/john.smith/foam/journal/$FOAM_TITLE.md'
|
||||
|
||||
# Windows filesystems
|
||||
filepath: 'C:\Users\john.smith\Documents\foam\journal\$FOAM_TITLE.md'
|
||||
---
|
||||
```
|
||||
|
||||
#### Example of **date-based** `filepath`
|
||||
|
||||
It is possible to vary the `filepath` value based on the current date using the `FOAM_DATE_*` variables. This is especially useful for the [[daily-notes]] template if you wish to organize by years, months, etc. Below is an example of a daily-note template metadata section that will create new daily notes under the `journal/YEAR/MONTH-MONTH_NAME/` filepath. For example, when a note is created on November 15, 2022, a new file will be created at `C:\Users\foam_user\foam_notes\journal\2022\11-Nov\2022-11-15-daily-note.md`. This method also respects the creation of daily notes relative to the current date (i.e. `/+1d`).
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: daily-note
|
||||
foam_template:
|
||||
description: Daily Note for $FOAM_TITLE
|
||||
filepath: "C:\\Users\\foam_user\\foam_notes\\journal\\$FOAM_DATE_YEAR\\$FOAM_DATE_MONTH-$FOAM_DATE_MONTH_NAME_SHORT\\$FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE-daily-note.md"
|
||||
---
|
||||
|
||||
# $FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE Daily Notes
|
||||
```
|
||||
|
||||
> Note: this method **requires** the use of absolute file paths, and in this example is using Windows path conventions. This method will also override any filename formatting defined in `.vscode/settings.json`
|
||||
|
||||
### `name` and `description` attributes
|
||||
|
||||
These attributes provide a human readable name and description to be shown in the template picker (e.g. When a user uses the `Foam: Create New Note From Template` command):
|
||||
|
||||

|
||||
|
||||
### Adding template metadata to an existing YAML Frontmatter block
|
||||
|
||||
If your template already has a YAML Frontmatter block, you can add the Foam template metadata to it.
|
||||
|
||||
#### Limitations
|
||||
|
||||
Foam only supports adding the template metadata to _YAML_ Frontmatter blocks. If the existing Frontmatter block uses some other format (e.g. JSON), you will have to add the template metadata to its own YAML Frontmatter block.
|
||||
|
||||
Further, the template metadata must be provided as a [YAML block mapping](https://yaml.org/spec/1.2/spec.html#id2798057), with the attributes placed on the lines immediately following the `foam_template` line:
|
||||
|
||||
```yaml
|
||||
---
|
||||
existing_frontmatter: "Existing Frontmatter block"
|
||||
foam_template: # this is a YAML "Block" mapping ("Flow" mappings aren't supported)
|
||||
name: My Note Template # Attributes must be on the lines immediately following `foam_template`
|
||||
description: This is my note template
|
||||
filepath: `journal/$FOAM_TITLE.md`
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
Due to the technical limitations of parsing the complex YAML format, unless the metadata is provided this specific form, Foam is unable to correctly remove the template metadata before creating the resulting note.
|
||||
|
||||
If this limitation proves inconvenient to you, please let us know. We may be able to extend our parsing capabilities to cover your use case. In the meantime, you can add the template metadata without this limitation by providing it in its own YAML Frontmatter block.
|
||||
|
||||
### Adding template metadata to its own YAML Frontmatter block
|
||||
|
||||
You can add the template metadata to its own YAML Frontmatter block at the start of the template:
|
||||
|
||||
```yaml
|
||||
---
|
||||
foam_template:
|
||||
name: My Note Template
|
||||
description: This is my note template
|
||||
filepath: 'journal/$FOAM_TITLE.md'
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
If the note already has a Frontmatter block, a Foam-specific Frontmatter block can be added to the start of the template. The Foam-specific Frontmatter block must always be placed at the very beginning of the file, and only whitespace can separate the two Frontmatter blocks.
|
||||
|
||||
```yaml
|
||||
---
|
||||
foam_template:
|
||||
name: My Note Template
|
||||
description: This is my note template
|
||||
filepath: 'journal/$FOAM_TITLE.md'
|
||||
---
|
||||
|
||||
---
|
||||
existing_frontmatter: 'Existing Frontmatter block'
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[daily-notes]: daily-notes.md "Daily Notes"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -1,62 +1,81 @@
|
||||
---
|
||||
tags: my-tag1 my-tag2 my-tag3/notes
|
||||
---
|
||||
|
||||
# Tags
|
||||
|
||||
You can add tags to your notes to categorize or link notes together.
|
||||
Tags provide flexible categorization and organization for your notes beyond wikilinks and folders.
|
||||
|
||||
## Creating a tag
|
||||
## Creating Tags
|
||||
|
||||
There are two ways of creating a tag:
|
||||
### Inline Tags
|
||||
|
||||
- Adding a `#tag` anywhere in the text of the note, for example: #my-tag1
|
||||
- Using the `tags: tag1, tag2` yaml frontmatter [[note-properties|note property]]. Notice `my-tag1` and `my-tag2` tags which are added to this document this way.
|
||||
Add tags directly in note content:
|
||||
|
||||
Tags can also be hierarchical, so you can have `#parent/child` such as #my-tag3/info.
|
||||
```markdown
|
||||
# Machine Learning Fundamentals
|
||||
|
||||
### Tag completion
|
||||
This covers basic algorithms and applications.
|
||||
|
||||
Typing the `#` character will launch VS Code's "Intellisense." This provider will show a list of possible tags that match the character. If you are editing in the frontmatter [[note-properties|note property]], you can invoke tag completion on the `tags:` line by either typing the `#` character, or using the ["trigger suggest"](https://code.visualstudio.com/docs/editor/intellisense) keybinding (usually `ctrl+space`). If the `#` is used in the frontmatter, it will be removed when the tag is inserted.
|
||||
|
||||
## Using *Tag Explorer*
|
||||
|
||||
It's possible to navigate tags via the Tag Explorer panel. Expand the Tag Explorer view in the left side bar which will list all the tags found in current Foam environment. Then, each level of tags can be expanded until the options to search by tag and a list of all files containing a particular tag are shown.
|
||||
|
||||
Tags can also be visualized in the Foam Graph Explorer. See [[graph-visualization]] for more info including how to change the color of nodes representing tags.
|
||||
|
||||
## Styling tags
|
||||
|
||||
It is possible to customize the way that tags look in the Markdown Preview panel that renders your Foam notes. This requires some knowledge of the CSS language, which is used to customize the styles of web technologies such as VSCode. A cursory introduction to CSS can be [found here](https://www.freecodecamp.org/news/get-started-with-css-in-5-minutes-e0804813fc3e/).
|
||||
|
||||
1. Create a CSS file within your Foam project, for example in `.foam/css/custom-tag-style.css` or [.vscode/custom-tag-style.css](../../.vscode/custom-tag-style.css)
|
||||
2. Add CSS code that targets the `.foam-tag` class
|
||||
3. Add a rule for each [CSS property](https://www.w3schools.com/cssref/index.php) you would like applied to your tags.
|
||||
4. Open the `.vscode/settings.json` file (or the Settings browser with `ctrl+,`)
|
||||
5. Add the path to your new stylesheet to the `markdown.styles` setting.
|
||||
|
||||
> Note: the file path for the stylesheet will be relative to the currently open folder in the workspace when changing this setting for the current workspace. If changing this setting for the user, then the file path will be relative to your global [VSCode settings](https://code.visualstudio.com/docs/getstarted/settings).
|
||||
|
||||
The end result will be a CSS file that looks similar to the content below. Now you can make your tags standout in your note previews.
|
||||
|
||||
```css
|
||||
.foam-tag{
|
||||
color:#ffffff;
|
||||
background-color: #000000;
|
||||
}
|
||||
#machine-learning #data-science #algorithms #beginner
|
||||
```
|
||||
|
||||

|
||||
### Front Matter Tags
|
||||
|
||||
## Using backlinks in place of tags
|
||||
Add tags in YAML front matter:
|
||||
|
||||
Given the power of backlinks, some people prefer to use them as tags.
|
||||
For example you can tag your notes about books with [[book]].
|
||||
```markdown
|
||||
---
|
||||
tags: [machine-learning, data-science, algorithms, beginner]
|
||||
---
|
||||
```
|
||||
|
||||
[note-properties|note property]: note-properties.md "Note Properties"
|
||||
[graph-visualization]: graph-visualization.md "Graph Visualization"
|
||||
### Hierarchical Tags
|
||||
|
||||
Create tag hierarchies using forward slashes:
|
||||
|
||||
```markdown
|
||||
#programming/languages/python
|
||||
#programming/frameworks/react
|
||||
#work/projects/website-redesign
|
||||
#personal/health/exercise
|
||||
```
|
||||
|
||||
|
||||
## Autocompletion
|
||||
|
||||
Typing `#` shows existing tags. In front matter, use `Ctrl+Space` for tag suggestions.
|
||||
|
||||
## Tag Explorer
|
||||
|
||||
Use the Tag Explorer panel in VS Code's sidebar to:
|
||||
|
||||
- Browse hierarchical tag structure
|
||||
- Filter by tag names
|
||||
- Click tags to see all associated notes
|
||||
- View tag usage counts
|
||||
|
||||
Tags also appear in the [[graph-view]] with customizable colors.
|
||||
|
||||
## Custom Tag Styling
|
||||
|
||||
Customize tag appearance in markdown preview by adding CSS:
|
||||
|
||||
1. Create `.foam/css/custom-tag-style.css`
|
||||
2. Add CSS targeting `.foam-tag` class:
|
||||
```css
|
||||
.foam-tag {
|
||||
color: #ffffff;
|
||||
background-color: #000000;
|
||||
}
|
||||
```
|
||||
3. Update `.vscode/settings.json`:
|
||||
```json
|
||||
{
|
||||
"markdown.styles": [".foam/css/custom-tag-style.css"]
|
||||
}
|
||||
```
|
||||
|
||||
## Tags vs Backlinks
|
||||
|
||||
Some users prefer [[book]] backlinks instead of #book tags for categorization. Both approaches work - choose what fits your workflow.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[note-properties|note property]: note-properties.md "Note Properties"
|
||||
[graph-visualization]: graph-visualization.md "Graph Visualization"
|
||||
[//end]: # "Autogenerated link references"
|
||||
[graph-view]: graph-view.md "Graph Visualization"
|
||||
[//end]: # "Autogenerated link references"
|
||||
|
||||
402
docs/user/features/templates.md
Normal file
402
docs/user/features/templates.md
Normal file
@@ -0,0 +1,402 @@
|
||||
# Note Templates
|
||||
|
||||
Foam supports note templates which let you customize the starting content of your notes instead of always starting from an empty note.
|
||||
|
||||
Foam supports two types of templates:
|
||||
|
||||
- **Markdown templates** (`.md` files) - Simple templates with predefined content and variables
|
||||
- **JavaScript templates** (`.js` files) - Smart templates that can adapt based on context and make intelligent decisions
|
||||
|
||||
Both types of templates are located in the special `.foam/templates` directory of your workspace.
|
||||
|
||||
## Quickstart
|
||||
|
||||
### Creating templates
|
||||
|
||||
**For simple templates:**
|
||||
|
||||
- Run the `Foam: Create New Template` command from the command palette
|
||||
- OR manually create a regular `.md` file in the `.foam/templates` directory
|
||||
|
||||
**For smart templates:**
|
||||
|
||||
- Create a `.js` file in the `.foam/templates` directory (see [JavaScript Templates](#javascript-templates) section below)
|
||||
|
||||

|
||||
|
||||
### Using templates
|
||||
|
||||
To create a note from a template:
|
||||
|
||||
- Run the `Foam: Create New Note From Template` command and follow the instructions. Don't worry if you've not created a template yet! You'll be prompted to create a new simple template if none exist.
|
||||
- OR run the `Foam: Create New Note` command, which uses the special default template (`.foam/templates/new-note.md` or `.foam/templates/new-note.js`, if it exists)
|
||||
|
||||

|
||||
|
||||
## Special templates
|
||||
|
||||
### Default template
|
||||
|
||||
The default template is used by the `Foam: Create New Note` command. Foam will look for these templates in order:
|
||||
|
||||
1. `.foam/templates/new-note.js` (JavaScript template)
|
||||
2. `.foam/templates/new-note.md` (Markdown template)
|
||||
|
||||
Customize this template to contain content that you want included every time you create a note.
|
||||
|
||||
### Default daily note template
|
||||
|
||||
The daily note template is used when creating daily notes (e.g. by using `Foam: Open Daily Note`). Foam will look for these templates in order:
|
||||
|
||||
1. `.foam/templates/daily-note.js` (JavaScript template)
|
||||
2. `.foam/templates/daily-note.md` (Markdown template)
|
||||
|
||||
For a simple markdown template, it is _recommended_ to define the YAML Front-Matter similar to the following:
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: daily-note
|
||||
---
|
||||
```
|
||||
|
||||
## JavaScript Templates
|
||||
|
||||
JavaScript templates are a powerful way to create smart, context-aware note templates that can adapt based on the situation. Unlike static Markdown templates, JavaScript templates can make intelligent decisions about what content to include.
|
||||
|
||||
**Use JavaScript templates when you want to:**
|
||||
|
||||
- Create different note structures based on the day of the week, time, or date
|
||||
- Adapt templates based on where the note is being created from
|
||||
- Automatically find and link related notes in your workspace
|
||||
- Generate content based on existing notes or workspace structure
|
||||
- Implement complex logic that static templates cannot handle
|
||||
|
||||
### Basic JavaScript template structure
|
||||
|
||||
A JavaScript template is a `.js` file that exports a function returning note content, and optionally location:
|
||||
|
||||
```javascript
|
||||
// .foam/templates/daily-note.js
|
||||
async function createNote({ trigger, foam, resolver, foamDate }) {
|
||||
const today = dayjs();
|
||||
// or you could use foamDate for day specific notes, see FOAM_DATE_* variables
|
||||
// const day = dayjs(foamDate)
|
||||
const formattedDay = today.format('YYYY-MM-DD');
|
||||
|
||||
// if you need a variable you can use the resolver
|
||||
// const title = await resolver.resolveFromName('FOAM_TITLE');
|
||||
|
||||
console.log(
|
||||
'Creating note for today: ' + formattedDay,
|
||||
JSON.stringify(trigger)
|
||||
);
|
||||
|
||||
let content = `# Daily Note - ${formattedDay}
|
||||
|
||||
## Today's focus
|
||||
-
|
||||
|
||||
## Notes
|
||||
-
|
||||
`;
|
||||
|
||||
switch (today.day()) {
|
||||
case 1: // Monday
|
||||
content = `# Week Planning - ${formattedDay}
|
||||
|
||||
## This week's goals
|
||||
- [ ] Goal 1
|
||||
- [ ] Goal 2
|
||||
|
||||
## Focus areas
|
||||
-
|
||||
`;
|
||||
break;
|
||||
case 5: // Friday
|
||||
content = `# Week Review - ${formattedDay}
|
||||
|
||||
## What went well
|
||||
-
|
||||
|
||||
## What could be improved
|
||||
-
|
||||
|
||||
## Next week's priorities
|
||||
-
|
||||
`;
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
content,
|
||||
filepath: `/weekly-planning/${formattedDay}.md`,
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
**Smart meeting notes:**
|
||||
|
||||
```javascript
|
||||
async function createNote({ trigger, foam, resolver }) {
|
||||
const title = (await resolver.resolveFromName('FOAM_TITLE')) || 'Meeting';
|
||||
const today = dayjs();
|
||||
// Detect meeting type from title
|
||||
const isStandup = title.toLowerCase().includes('standup');
|
||||
const isReview = title.toLowerCase().includes('review');
|
||||
|
||||
let template = `# ${title} - ${today.format('YYYY-MM-DD')}
|
||||
|
||||
`;
|
||||
|
||||
if (isStandup) {
|
||||
template += `## What I did yesterday
|
||||
-
|
||||
|
||||
## What I'm doing today
|
||||
-
|
||||
|
||||
## Blockers
|
||||
-
|
||||
`;
|
||||
} else if (isReview) {
|
||||
template += `## What went well
|
||||
-
|
||||
|
||||
## What could be improved
|
||||
-
|
||||
|
||||
## Action items
|
||||
- [ ]
|
||||
`;
|
||||
} else {
|
||||
template += `## Agenda
|
||||
-
|
||||
|
||||
## Notes
|
||||
-
|
||||
|
||||
## Action items
|
||||
- [ ]
|
||||
`;
|
||||
}
|
||||
|
||||
return {
|
||||
content: template,
|
||||
filepath: `/meetings/${title}.md`,
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### Template result format
|
||||
|
||||
JavaScript templates must return an object with:
|
||||
|
||||
- `content` (required): The note content as a string
|
||||
- `filepath` (required): Custom file path for the note
|
||||
- NOTE: the path must be within the workspace.
|
||||
- A relative path will be resolved based on the `onRelativePath` command configuration.
|
||||
- An absolute path will be taken as is, if it falls within the workspace. Otherwise it will be considered to be from the workspace root
|
||||
|
||||
```javascript
|
||||
return {
|
||||
content: '# My Note\n\nContent here...',
|
||||
filepath: 'custom-folder/my-note.md',
|
||||
};
|
||||
```
|
||||
|
||||
### Security and limitations
|
||||
|
||||
JavaScript templates run in a best-effort secured environment:
|
||||
|
||||
- ✅ Can only run from trusted VS Code workspaces
|
||||
- ✅ Can access Foam workspace and utilities
|
||||
- ✅ Can use standard JavaScript features
|
||||
- ✅ Have a 30-second execution timeout
|
||||
- ❌ Cannot access the file system directly
|
||||
- ❌ Cannot make network requests
|
||||
- ❌ Cannot access Node.js modules
|
||||
|
||||
This increases the chances that templates stay safe while still being powerful enough for complex logic.
|
||||
|
||||
STILL - PLEASE BE AWARE YOU ARE EXECUTING CODE ON YOUR MACHINE. THIS SANDBOX IS NOT MEANT TO BE THE ULTIMATE SECURITY SOLUTION.
|
||||
|
||||
**YOU MUST TRUST THE REPO CONTRIBUTORS**
|
||||
|
||||
## Markdown templates
|
||||
|
||||
Markdown templates are a simple way to create notes
|
||||
|
||||
**Use Markdown templates when you want to:**
|
||||
|
||||
- Create simple, consistent note structures
|
||||
- Use basic variables and placeholders
|
||||
- Keep templates easy to read and modify
|
||||
|
||||
### Variables
|
||||
|
||||
Markdown templates can use all the variables available in [VS Code Snippets](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables).
|
||||
|
||||
In addition, you can also use variables provided by Foam:
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `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_CURRENT_DIR` | The current editor's directory path. Resolves to the directory of the currently active file, or falls back to workspace root if no editor is active. Useful for creating notes in the current directory context. |
|
||||
| `FOAM_DATE_*` | `FOAM_DATE_YEAR`, `FOAM_DATE_MONTH`, `FOAM_DATE_WEEK`, `FOAM_DATE_DAY_ISO` 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. |
|
||||
|
||||
### `FOAM_DATE_*` variables
|
||||
|
||||
Foam defines its own set of datetime variables that have a similar behaviour as [VS Code's datetime snippet variables](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables).
|
||||
|
||||
Supported variables include:
|
||||
|
||||
- `FOAM_DATE_YEAR`: 4-digit year (e.g. 2025)
|
||||
- `FOAM_DATE_MONTH`: 2-digit month (e.g. 09)
|
||||
- `FOAM_DATE_WEEK`: ISO 8601 week number (e.g. 37)
|
||||
- `FOAM_DATE_DAY_ISO`: ISO 8601 weekday number (1-7, where Monday=1, Sunday=7)
|
||||
- `FOAM_DATE_DATE`: 2-digit day of month (e.g. 15)
|
||||
- `FOAM_DATE_DAY_NAME`: Full weekday name (e.g. Monday)
|
||||
- `FOAM_DATE_DAY_NAME_SHORT`: Short weekday name (e.g. Mon)
|
||||
- `FOAM_DATE_HOUR`, `FOAM_DATE_MINUTE`, `FOAM_DATE_SECOND`, `FOAM_DATE_SECONDS_UNIX`, etc.
|
||||
|
||||
For example, `FOAM_DATE_YEAR` has the same behaviour as VS Code's `CURRENT_YEAR`, `FOAM_DATE_SECONDS_UNIX` has the same behaviour as `CURRENT_SECONDS_UNIX`, etc. `FOAM_DATE_DAY_ISO` returns the ISO weekday number (Monday=1, Sunday=7), which is useful for ISO week date formats like `2025-W37-5`.
|
||||
|
||||
By default, prefer using the `FOAM_DATE_` versions. The datetime used to compute the values will be the same for both `FOAM_DATE_` and VS Code's variables, with the exception of the creation notes using the daily note template.
|
||||
|
||||
For more nitty-gritty details about the supported date formats, [see here](https://github.com/foambubble/foam/blob/main/packages/foam-vscode/src/services/variable-resolver.ts).
|
||||
|
||||
#### Relative daily notes
|
||||
|
||||
When referring to daily notes, you can use the relative snippets (`/+1d`, `/tomorrow`, etc.). In these cases, the new notes will be created with the daily note template, but the datetime used should be the relative datetime, not the current datetime.
|
||||
By using the `FOAM_DATE_` versions of the variables, the correct relative date will populate the variables, instead of the current datetime.
|
||||
|
||||
For example, given this daily note template (`.foam/templates/daily-note.md`):
|
||||
|
||||
```markdown
|
||||
# $FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE
|
||||
|
||||
## Here's what I'm going to do today
|
||||
|
||||
- Thing 1
|
||||
- Thing 2
|
||||
```
|
||||
|
||||
When the `/tomorrow` snippet is used, `FOAM_DATE_` variables will be populated with tomorrow's date, as expected.
|
||||
If instead you were to use the VS Code versions of these variables, they would be populated with today's date, not tomorrow's, causing unexpected behaviour.
|
||||
|
||||
When creating notes in any other scenario, the `FOAM_DATE_` values are computed using the same datetime as the VS Code ones, so the `FOAM_DATE_` versions can be used in all scenarios by default.
|
||||
|
||||
### Metadata
|
||||
|
||||
**Markdown templates** can also contain metadata about the templates themselves. The metadata is defined in YAML "Frontmatter" blocks within the templates.
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `filepath` | The filepath to use when creating the new note. If the filepath is a relative filepath, it is relative to the current workspace. |
|
||||
| `name` | A human readable name to show in the template picker. |
|
||||
| `description` | A human readable description to show in the template picker. |
|
||||
|
||||
Foam-specific variables (e.g. `$FOAM_TITLE`) can be used within template metadata. However, VS Code snippet variables are ([currently](https://github.com/foambubble/foam/pull/655)) not supported.
|
||||
|
||||
#### `filepath` attribute
|
||||
|
||||
It is possible to vary the `filepath` value based on the current date using the `FOAM_DATE_*` variables. This is especially useful for the [[daily-notes]] template if you wish to organize by years, months, etc. Below is an example of a daily-note template metadata section that will create new daily notes under the `journal/YEAR/MONTH-MONTH_NAME/` filepath. For example, when a note is created on November 15, 2022, a new file will be created at `C:\Users\foam_user\foam_notes\journal\2022\11-Nov\2022-11-15-daily-note.md`. This method also respects the creation of daily notes relative to the current date (i.e. `/+1d`).
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: daily-note
|
||||
foam_template:
|
||||
description: Daily Note
|
||||
filepath: '/journal/$FOAM_DATE_YEAR/$FOAM_DATE_MONTH-$FOAM_DATE_MONTH_NAME_SHORT/$FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE-daily-note.md'
|
||||
---
|
||||
|
||||
# $FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE Daily Notes
|
||||
```
|
||||
|
||||
##### Creating notes in the current directory
|
||||
|
||||
To create notes in the same directory as your currently active file, use the `FOAM_CURRENT_DIR` variable in your template's `filepath`:
|
||||
|
||||
```markdown
|
||||
---
|
||||
foam_template:
|
||||
name: Current Directory Note
|
||||
filepath: '$FOAM_CURRENT_DIR/$FOAM_SLUG.md'
|
||||
---
|
||||
|
||||
# $FOAM_TITLE
|
||||
|
||||
$FOAM_SELECTED_TEXT
|
||||
```
|
||||
|
||||
**Best practices for filepath patterns:**
|
||||
|
||||
- **Explicit current directory:** `$FOAM_CURRENT_DIR/$FOAM_SLUG.md` - Creates notes in the current editor's directory
|
||||
- **Workspace root:** `/$FOAM_SLUG.md` - Always creates notes in workspace root
|
||||
- **Subdirectories:** `$FOAM_CURRENT_DIR/meetings/$FOAM_SLUG.md` - Creates notes in subdirectories relative to current location
|
||||
|
||||
The `FOAM_CURRENT_DIR` approach is recommended over relative paths (like `./file.md`) because it makes the template's behavior explicit and doesn't depend on configuration settings.
|
||||
|
||||
#### `name` and `description` attributes
|
||||
|
||||
These attributes provide a human readable name and description to be shown in the template picker (e.g. When a user uses the `Foam: Create New Note From Template` command):
|
||||
|
||||

|
||||
|
||||
#### Adding template metadata to an existing YAML Frontmatter block
|
||||
|
||||
If your template already has a YAML Frontmatter block, you can add the Foam template metadata to it.
|
||||
|
||||
Foam only supports adding the template metadata to _YAML_ Frontmatter blocks. If the existing Frontmatter block uses some other format (e.g. JSON), you will have to add the template metadata to its own YAML Frontmatter block.
|
||||
|
||||
Further, the template metadata must be provided as a [YAML block mapping](https://yaml.org/spec/1.2/spec.html#id2798057), with the attributes placed on the lines immediately following the `foam_template` line:
|
||||
|
||||
```yaml
|
||||
---
|
||||
existing_frontmatter: "Existing Frontmatter block"
|
||||
foam_template: # this is a YAML "Block" mapping ("Flow" mappings aren't supported)
|
||||
name: My Note Template # Attributes must be on the lines immediately following `foam_template`
|
||||
description: This is my note template
|
||||
filepath: `journal/$FOAM_TITLE.md`
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
#### Adding template metadata to its own YAML Frontmatter block
|
||||
|
||||
You can add the template metadata to its own YAML Frontmatter block at the start of the template:
|
||||
|
||||
```yaml
|
||||
---
|
||||
foam_template:
|
||||
name: My Note Template
|
||||
description: This is my note template
|
||||
filepath: 'journal/$FOAM_TITLE.md'
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
If the note already has a Frontmatter block, a Foam-specific Frontmatter block can be added to the start of the template. The Foam-specific Frontmatter block must always be placed at the very beginning of the file, and only whitespace can separate the two Frontmatter blocks.
|
||||
|
||||
```yaml
|
||||
---
|
||||
foam_template:
|
||||
name: My Note Template
|
||||
description: This is my note template
|
||||
filepath: 'journal/$FOAM_TITLE.md'
|
||||
---
|
||||
|
||||
---
|
||||
existing_frontmatter: 'Existing Frontmatter block'
|
||||
---
|
||||
This is the rest of the template
|
||||
```
|
||||
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[daily-notes]: daily-notes.md 'Daily Notes'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
@@ -1,44 +1,45 @@
|
||||
# Wikilinks
|
||||
|
||||
Wikilinks are the internal links that connect the files in your knowledge base. (Also called `[[MediaWiki]]` links).
|
||||
Wikilinks are internal links that connect files in your knowledge base using `[[double bracket]]` syntax.
|
||||
|
||||
## Creating and navigating wikilinks
|
||||
## Creating Wikilinks
|
||||
|
||||
To create a wikilink, type `[[` and then start typing the name of another note in your repo. Once the desired note is selected press the `tab` key to autocomplete it. For example: [[graph-visualization]].
|
||||
1. **Type `[[`** and start typing a note name
|
||||
2. **Select from autocomplete** and press `Tab`
|
||||
3. **Navigate** with `Ctrl+Click` (`Cmd+Click` on Mac) or `F12`
|
||||
4. **Create new notes** by clicking on non-existent wikilinks
|
||||
|
||||
`Cmd` + `Click` ( `Ctrl` + `Click` on Windows ) on wikilink to navigate to that note (`F12` also works while your cursor is on the wikilink). If the file doesn't exist it will be created in your workspace based on your default [[note-templates]] settings.
|
||||
Example: [[graph-view]]
|
||||
|
||||
## Placeholders
|
||||
|
||||
You can also create a [[placeholder]]. <!--NOTE: this placeholder link should NOT have an associated file. This is to demonstrate the concept-->
|
||||
A placeholder is a wikilink that doesn't have a target file and a link to a placeholder is styled differently so you can easily tell them apart.
|
||||
They can still be helpful to highlight connections.
|
||||
Wikilinks to non-existent files create [[placeholder]] links, styled differently to show they need files created. They're useful for planning your knowledge structure.
|
||||
|
||||
Open the graph with `Foam: Show Graph` command, and look at the placeholder node.
|
||||
View placeholders in the graph with `Foam: Show Graph` command or in the `Placeholders` panel.
|
||||
|
||||
Remember, with `CTRL/CMD+click` on a wikilink you can navigate to the note, or create it (if the link is a placeholder).
|
||||
## Section Links
|
||||
|
||||
## Support for sections
|
||||
Link to specific sections using `[[note-name#Section Title]]` syntax. Foam provides autocomplete for section titles.
|
||||
|
||||
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.
|
||||
Examples:
|
||||
|
||||
## Markdown compatibility
|
||||
- External file: `[link text](other-file.md#section-name)`
|
||||
- Same document: `[link text](#section-name)`
|
||||
|
||||
The [Foam for VSCode](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode) extension automatically generates [[link-reference-definitions]] at the bottom of the file to make wikilinks compatible with other Markdown tools and parsers.
|
||||
## Markdown Compatibility
|
||||
|
||||
## Read more
|
||||
Foam can automatically generates [[link-reference-definitions]] at the bottom of files to make wikilinks compatible with standard Markdown processors.
|
||||
|
||||
- [[foam-file-format]]
|
||||
- [[note-templates]]
|
||||
- See [[link-reference-definition-improvements]] for further discussion on current problems and potential solutions.
|
||||
## Related
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[graph-visualization]: graph-visualization.md "Graph Visualization"
|
||||
[note-templates]: note-templates.md "Note Templates"
|
||||
[link-reference-definitions]: link-reference-definitions.md "Link Reference Definitions"
|
||||
[foam-file-format]: ../../dev/foam-file-format.md "Foam File Format"
|
||||
[link-reference-definition-improvements]: ../../dev/proposals/link-reference-definition-improvements.md "Link Reference Definition Improvements"
|
||||
[//end]: # "Autogenerated link references"
|
||||
- [[foam-file-format]] - Technical details
|
||||
- [[templates]] - Creating new notes
|
||||
- [[link-reference-definition-improvements]] - Current limitations
|
||||
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[graph-visualization]: graph-visualization.md 'Graph Visualization'
|
||||
[link-reference-definitions]: link-reference-definitions.md 'Link Reference Definitions'
|
||||
[foam-file-format]: ../../dev/foam-file-format.md 'Foam File Format'
|
||||
[note-templates]: note-templates.md 'Note Templates'
|
||||
[link-reference-definition-improvements]: ../../dev/proposals/link-reference-definition-improvements.md 'Link Reference Definition Improvements'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
# Creating New Notes
|
||||
|
||||
- Write out a new `[[wikilink]]` and `Cmd` + `Click` to create a new file and enter it.
|
||||
- For keyboard navigation, use the 'Follow Definition' key `F12` (or [remap the 'editor.action.revealDefinition' key binding](https://code.visualstudio.com/docs/getstarted/keybindings) to something more ergonomic)
|
||||
- `Cmd` + `Shift` + `P` (`Ctrl` + `Shift` + `P` for Windows), execute `Foam: Create Note` and enter a **Title Case Name** to create `Title Case Name.md`
|
||||
- Add a keyboard binding to make creating new notes easier. See [[commands]] for more info on this.
|
||||
- The [[note-templates]] used by this command can be customized.
|
||||
- You shouldn't worry too much about categorizing your notes. You can always [[search-for-notes]], and explore them using the [[graph-visualization]].
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[commands]: ../features/commands.md "Foam Commands"
|
||||
[note-templates]: ../features/note-templates.md "Note Templates"
|
||||
[search-for-notes]: ../recipes/search-for-notes.md "Search for Notes"
|
||||
[graph-visualization]: ../features/graph-visualization.md "Graph Visualization"
|
||||
[//end]: # "Autogenerated link references"
|
||||
209
docs/user/getting-started/first-workspace.md
Normal file
209
docs/user/getting-started/first-workspace.md
Normal file
@@ -0,0 +1,209 @@
|
||||
# Creating Your First Workspace
|
||||
|
||||
A Foam workspace is where all your notes, ideas, and knowledge live. Think of it as your digital garden where thoughts can grow and connect. This guide will help you set up a workspace that's organized, scalable, and tailored to your thinking style.
|
||||
|
||||
## Understanding Workspaces
|
||||
|
||||
A Foam workspace is simply a folder containing **Markdown files** (`.md`) - your actual notes.
|
||||
|
||||
Optionally it can contain:
|
||||
|
||||
- **Configuration files** - VS Code settings and Foam preferences
|
||||
- **Assets** - images, attachments, and other media
|
||||
- **Templates** - reusable note structures
|
||||
|
||||
### Single vs. Multiple Workspaces
|
||||
|
||||
**Recommended: Single Workspace**
|
||||
|
||||
- Keep all your knowledge in one place
|
||||
- Better link discovery and graph visualization
|
||||
- Easier to maintain and backup
|
||||
- Follows the "unified knowledge base" principle
|
||||
|
||||
**Deprecated: Multiple Workspaces** (deprecated - advanced users only)
|
||||
|
||||
- Separate professional and personal knowledge
|
||||
- Isolate sensitive information
|
||||
- Different workflows for different projects
|
||||
|
||||
Multiple workspaces are to be considered deprecated at this point, and might become unsupported in the future.
|
||||
You can simulate a complex workspace by using file/folder links.
|
||||
|
||||
## Method 1: Using the Foam Template (Recommended)
|
||||
|
||||
The easiest way to start is with our pre-configured template:
|
||||
|
||||
### Step 1: Create from Template
|
||||
|
||||
1. **Visit** [github.com/foambubble/foam-template](https://github.com/foambubble/foam-template)
|
||||
2. **Click "Use this template"** (you'll need a GitHub account)
|
||||
3. **Name your repository** (e.g., "john-knowledge-base", "my-second-brain")
|
||||
4. **Choose visibility:**
|
||||
- **Private** - for personal notes (recommended)
|
||||
- **Public** - if you want to share your knowledge openly
|
||||
|
||||
### Step 2: Clone Locally
|
||||
|
||||
```bash
|
||||
git clone https://github.com/yourusername/your-repo-name.git
|
||||
cd your-repo-name
|
||||
```
|
||||
|
||||
### Step 3: Open in VS Code
|
||||
|
||||
1. **Launch VS Code**
|
||||
2. **File > Open Folder**
|
||||
3. **Select your cloned repository folder**
|
||||
|
||||
## Method 2: Start from Scratch
|
||||
|
||||
For a minimal setup:
|
||||
|
||||
1. **Create a new folder** on your computer
|
||||
2. **Open the folder** in VS Code (`File > Open Folder`)
|
||||
|
||||
That's all, you can start working with your markdown files and Foam will take care of the rest.
|
||||
|
||||
## Ideas for your knowledge base
|
||||
|
||||
### 1. Customize Your Settings
|
||||
|
||||
Review and adjust `.vscode/settings.json` based on your preferences:
|
||||
|
||||
- **Daily notes location** - where your daily notes are stored
|
||||
- **Image handling** - how pasted images are organized
|
||||
- **Link format** - with or without file extensions
|
||||
|
||||
### 2. Set Up Your Inbox
|
||||
|
||||
Create `inbox.md` as your default capture location:
|
||||
|
||||
```markdown
|
||||
# Inbox
|
||||
|
||||
Quick notes and ideas go here before being organized.
|
||||
|
||||
## Today's Captures
|
||||
|
||||
-
|
||||
|
||||
## To Process
|
||||
|
||||
-
|
||||
|
||||
## Ideas
|
||||
|
||||
-
|
||||
```
|
||||
|
||||
### 3. Create Core Structure Notes
|
||||
|
||||
## Workspace Organization Strategies
|
||||
|
||||
Establish your main organizational notes.
|
||||
You can use any methodology, Foam is not opinionated.
|
||||
|
||||
The only recommendation is to get started, you can improve later.
|
||||
|
||||
The two main methods adopted by users are [PARA](https://fortelabs.com/blog/para/) and [Zettelkasten](https://zettelkasten.de/overview/).
|
||||
|
||||
### The PARA Method
|
||||
|
||||
Organize around four categories:
|
||||
|
||||
- **Projects** - Things with deadlines
|
||||
- **Areas** - Ongoing responsibilities
|
||||
- **Resources** - Future reference materials
|
||||
- **Archive** - Inactive items
|
||||
|
||||
### Zettelkasten Approach
|
||||
|
||||
Number-based system for atomic ideas:
|
||||
|
||||
- **Permanent notes** - `202501251030-idea-title.md`
|
||||
- **Literature notes** - `book-author-year.md`
|
||||
- **Index notes** - `index-topic.md`
|
||||
|
||||
### 4. Configure Daily Notes
|
||||
|
||||
Daily notes are perfect for:
|
||||
|
||||
- Daily planning and reflection
|
||||
- Meeting notes
|
||||
- Journal entries
|
||||
- Quick captures
|
||||
|
||||
Test your daily notes setup:
|
||||
|
||||
1. **Press `Ctrl+Shift+P` / `Cmd+Shift+P`**
|
||||
2. **Type "Foam: Open Daily Note"**
|
||||
3. **Verify the note is created in the right location**
|
||||
|
||||
Alternatively you can press `Alt+D` to open today's daily note, or `Alt+H` to open another day's daily note.
|
||||
Use the `.foam/templates/daily-note.md` to customize your daily note.
|
||||
|
||||
## Best Practices for New Workspaces
|
||||
|
||||
### 1. Start Small
|
||||
|
||||
- Begin with just a few notes
|
||||
- Don't over-organize initially
|
||||
- Let structure emerge naturally
|
||||
|
||||
### 2. Use Templates
|
||||
|
||||
- Create templates for common note types
|
||||
- Maintain consistency across similar notes
|
||||
- Save time on repetitive formatting
|
||||
|
||||
### 3. Link Early and Often
|
||||
|
||||
- Use `[[wikilinks]]` liberally
|
||||
- Don't worry about creating "perfect" links
|
||||
- Foam handles broken links gracefully
|
||||
|
||||
### 4. Regular Reviews
|
||||
|
||||
- Weekly workspace cleanup
|
||||
- Archive completed projects
|
||||
- Identify missing connections
|
||||
|
||||
## Syncing and Backup
|
||||
|
||||
Foam works on simple files, you can add whatever backup method you prefer on top of it.
|
||||
|
||||
### Git
|
||||
|
||||
Your workspace is a Git repository:
|
||||
|
||||
```bash
|
||||
git add .
|
||||
git commit -m "Add new notes and ideas"
|
||||
git push origin main
|
||||
```
|
||||
|
||||
You can also use other VS Code extensions to manage the git synching if that's helpful.
|
||||
|
||||
### Alternative Sync Methods
|
||||
|
||||
- **Cloud storage** - Dropbox, OneDrive, Google Drive
|
||||
- **Local backup** - Time Machine, File History
|
||||
- **Manual export** - Regular ZIP backups
|
||||
|
||||
## What's Next?
|
||||
|
||||
With your workspace set up, you're ready to:
|
||||
|
||||
1. **[Learn note-taking fundamentals](note-taking)** - Master Markdown and writing effective notes
|
||||
2. **[Explore navigation](navigation.md)** - Connect your thoughts with wikilinks
|
||||
3. **[Discover the graph view](graph-view.md)** - Visualize your knowledge network
|
||||
4. **[Set up templates](templates)** - Standardize your note creation process
|
||||
|
||||
## Getting Help
|
||||
|
||||
If you encounter setup issues:
|
||||
|
||||
- Check the [Installation Guide](installation.md) for prerequisites
|
||||
- Visit the [FAQ](../faq.md) for common workspace problems
|
||||
- Join the [Foam Community Discord](https://foambubble.github.io/join-discord/w)
|
||||
@@ -1,47 +1,256 @@
|
||||
# Getting started with VS Code
|
||||
# Using Foam with VS Code Features
|
||||
|
||||
VS Code is a powerful text editor, hidden behind a simple interface.
|
||||
Foam builds on Visual Studio Code's powerful editing capabilities, integrating seamlessly with VS Code's native features to create a comprehensive knowledge management experience. This guide explores how to leverage VS Code's built-in functionality alongside Foam.
|
||||
|
||||
### Keyboard shortcuts
|
||||
|
||||
VS Code supports various **keyboard shortcuts**, the most important for us are:
|
||||
|
||||
| Shortcut | Action |
|
||||
| ------------- | ---------------------------- |
|
||||
| `cmd+N` | create a new file |
|
||||
| `cmd+S` | save the current file |
|
||||
| `cmd+O` | open a file |
|
||||
| `cmd+P` | use quickpick to open a file |
|
||||
| `cmd+shift+P` | invoke a command (see below) |
|
||||
| Shortcut | Action |
|
||||
| ------------- | ----------------------------- |
|
||||
| `cmd+N` | create a new file |
|
||||
| `cmd+S` | save the current file |
|
||||
| `cmd+O` | open a file |
|
||||
| `cmd+P` | use quickpick to open a file |
|
||||
| `alt+D` | open the daily note for today |
|
||||
| `alt+H` | open the daily note for a day |
|
||||
| `cmd+shift+P` | invoke a command (see below) |
|
||||
|
||||
For more information, see the [VS Code keyboard cheat sheets](https://code.visualstudio.com/docs/getstarted/keybindings#_keyboard-shortcuts-reference), where you can also see how to customize your keybindings.
|
||||
|
||||
## Commands
|
||||
### Commands
|
||||
|
||||
Commands make VS Code extremely powerful.
|
||||
|
||||
To invoke a command, press `cmd+shift+P` and select the command you want to execute.
|
||||
For example, to see the Foam graph:
|
||||
|
||||
- press `cmd+shift+P`
|
||||
- press `cmd+shift+P` to open the command bar
|
||||
- start typing `show graph`
|
||||
- select the `Foam: Show Graph` command
|
||||
|
||||
And watch the magic unfold.
|
||||
|
||||
To see all foam commands, type "foam" in the command bar.
|
||||
For more information on commands, see [commands on the VS Code site](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette).
|
||||
|
||||
If you want to learn more about VS Code, check out their [website](https://code.visualstudio.com/docs#first-steps).
|
||||
|
||||
## Panels
|
||||
### Panels
|
||||
|
||||
You can see a few panels on the left, including:
|
||||
Foam integrates with VS Code panels to provide insights into individual notes and the whole knowledge base.
|
||||
|
||||
- `Outline`: this panel shows you the structure of the file based on the headings
|
||||
- `Tag Explorer`: This shows you the tags in your workspace, see [[tags]] for more information on tags
|
||||
- **`Foam: links`**: Shows all notes that link to and from the currently active note, helping you understand connections and navigate your knowledge graph
|
||||
- **`Foam: Orphaned Notes`**: Displays notes that have no incoming or outgoing links, helping you identify isolated content that might need better integration
|
||||
- **`Tag Explorer`**: Shows all tags used across your workspace in a hierarchical view, see [[tags]] for more information on tags
|
||||
- **`Foam: Graph`**: Visual representation of your note connections (also available as a separate graph view)
|
||||
|
||||
### Styling and Themes
|
||||
|
||||
VS Code is very configurable when it comes to themes and style. Find your ideal set up by running the command `Color Theme`.
|
||||
For more information see the [VS Code documentation](https://code.visualstudio.com/docs/configure/themes).
|
||||
|
||||
### Multi-Cursor Editing
|
||||
|
||||
Edit multiple locations simultaneously for efficient note management:
|
||||
|
||||
**Basic Multi-Cursor:**
|
||||
|
||||
- `Alt+Click` / `Option+Click` - Add cursor at click location
|
||||
- `Ctrl+Alt+Down` / `Cmd+Option+Down` - Add cursor below
|
||||
- `Ctrl+Alt+Up` / `Cmd+Option+Up` - Add cursor above
|
||||
- `Ctrl+D` / `Cmd+D` - Select next occurrence of word
|
||||
- `Ctrl+Shift+L` / `Cmd+Shift+L` - Select all occurrences
|
||||
|
||||
**Bulk wikilink creation:**
|
||||
|
||||
1. **Select a word** (e.g., "Python")
|
||||
2. **Press `Ctrl+Shift+L`** to select all occurrences
|
||||
3. **Type `[[]]`** to wrap all instances
|
||||
4. **Arrow key** to position cursor inside brackets
|
||||
|
||||
### Find and Replace
|
||||
|
||||
Powerful search and replace for note maintenance:
|
||||
|
||||
**Basic Find/Replace:**
|
||||
|
||||
- `Ctrl+F` / `Cmd+F` - Find in current file
|
||||
- `Ctrl+H` / `Cmd+H` - Replace in current file
|
||||
- `Ctrl+Shift+F` / `Cmd+Shift+F` - Find across workspace
|
||||
- `Ctrl+Shift+H` / `Cmd+Shift+H` - Replace across workspace
|
||||
|
||||
### Text Folding
|
||||
|
||||
Organize long notes with collapsible sections:
|
||||
|
||||
**Folding Controls:**
|
||||
|
||||
- **Click fold icons** in the gutter next to headings
|
||||
- `Ctrl+Shift+[` / `Cmd+Option+[` - Fold current section
|
||||
- `Ctrl+Shift+]` / `Cmd+Option+]` - Unfold current section
|
||||
- `Ctrl+K Ctrl+0` / `Cmd+K Cmd+0` - Fold all
|
||||
- `Ctrl+K Ctrl+J` / `Cmd+K Cmd+J` - Unfold all
|
||||
|
||||
## File Management
|
||||
|
||||
### Explorer Integration
|
||||
|
||||
Leverage VS Code's file explorer for note organization:
|
||||
|
||||
**File Operations:**
|
||||
|
||||
- **Drag and drop** files to reorganize
|
||||
- **Right-click context menus** for quick actions
|
||||
- **New file/folder** creation with shortcuts
|
||||
- **Bulk selection** with Ctrl+Click / Cmd+Click
|
||||
|
||||
**Quick File Actions:**
|
||||
|
||||
- `F2` - Rename file (Foam updates links automatically)
|
||||
- `Delete` - Move to trash
|
||||
- `Ctrl+C` / `Cmd+C` then `Ctrl+V` / `Cmd+V` - Copy/paste files
|
||||
- **Right-click → Reveal in Explorer/Finder** - Open in file system
|
||||
|
||||
### Quick Open
|
||||
|
||||
Rapid file navigation for large knowledge bases:
|
||||
|
||||
**Quick Open Commands:**
|
||||
|
||||
- `Ctrl+P` / `Cmd+P` - Go to file
|
||||
- `Ctrl+Shift+O` / `Cmd+Shift+O` - Go to symbol (headings in Markdown)
|
||||
- `Ctrl+T` / `Cmd+T` - Go to symbol in workspace
|
||||
- `Ctrl+G` / `Cmd+G` - Go to line number
|
||||
|
||||
**Search Patterns:**
|
||||
|
||||
```
|
||||
# Go to File (Ctrl+P)
|
||||
machine # Finds "machine-learning.md"
|
||||
proj alpha # Finds "project-alpha.md"
|
||||
daily/2025 # Finds files in daily/2025 folder
|
||||
|
||||
# Go to Symbol (Ctrl+Shift+O)
|
||||
@introduction # Jump to "Introduction" heading
|
||||
@#setup # Jump to "Setup" heading
|
||||
:50 # Go to line 50
|
||||
```
|
||||
|
||||
## Search and Discovery
|
||||
|
||||
### Global Search
|
||||
|
||||
Find content across your entire knowledge base:
|
||||
|
||||
**Search Interface (`Ctrl+Shift+F` / `Cmd+Shift+F`):**
|
||||
|
||||
- **Search box** - Enter your query
|
||||
- **Replace box** - Toggle with replace arrow
|
||||
- **Include/Exclude patterns** - Filter by file types or folders
|
||||
- **Match case** - Case-sensitive search
|
||||
- **Match whole word** - Exact word matching
|
||||
- **Use regular expression** - Advanced pattern matching
|
||||
|
||||
### Timeline View
|
||||
|
||||
Track changes to your notes over time:
|
||||
|
||||
**Accessing Timeline:**
|
||||
|
||||
1. **Open Explorer panel**
|
||||
2. **Expand "Timeline" section** at bottom
|
||||
3. **Select a file** to see its change history
|
||||
4. **Click timeline entries** to see diff views
|
||||
|
||||
**Timeline Features:**
|
||||
|
||||
- **Git commits** show when notes were changed
|
||||
- **File saves** track editing sessions
|
||||
- **Diff views** highlight what changed
|
||||
- **Restore points** for recovering previous versions
|
||||
|
||||
### Outline View
|
||||
|
||||
Navigate long notes with hierarchical structure:
|
||||
|
||||
**Outline Panel:**
|
||||
|
||||
1. **Enable in Explorer** (expand "Outline" section)
|
||||
2. **Shows heading hierarchy** for current note
|
||||
3. **Click headings** to jump to sections
|
||||
4. **Collapse/expand** sections in outline
|
||||
|
||||
## Version Control Integration
|
||||
|
||||
### Git Integration
|
||||
|
||||
Track changes to your knowledge base:
|
||||
|
||||
**Source Control Panel:**
|
||||
|
||||
- **View changes** - See modified files
|
||||
- **Stage changes** - Click `+` to stage files
|
||||
- **Commit changes** - Enter message and commit
|
||||
- **Sync changes** - Push/pull from remote
|
||||
|
||||
**Git Workflow for Notes:**
|
||||
|
||||
1. **Write and edit** your notes
|
||||
2. **Review changes** in Source Control panel
|
||||
3. **Stage relevant files** for commit
|
||||
4. **Write meaningful commit message**
|
||||
5. **Commit and push** to backup/share
|
||||
|
||||
**Useful Git Features:**
|
||||
|
||||
- **Diff view** - See exactly what changed
|
||||
- **File history** - Track note evolution over time
|
||||
- **Branch management** - Experiment with different organization approaches
|
||||
- **Merge conflicts** - Resolve when collaborating
|
||||
|
||||
## Markdown Features
|
||||
|
||||
### Preview Integration
|
||||
|
||||
View formatted notes alongside editing:
|
||||
|
||||
**Preview Commands:**
|
||||
|
||||
- `Ctrl+Shift+V` / `Cmd+Shift+V` - Open preview
|
||||
- `Ctrl+K V` / `Cmd+K V` - Open preview to side
|
||||
- **Preview lock** - Pin preview to specific file
|
||||
|
||||
**Diagrams (with Mermaid extension):**
|
||||
|
||||
````markdown
|
||||
```mermaid
|
||||
graph TD
|
||||
A[Foam Workspace] --> B[Notes]
|
||||
A --> C[Templates]
|
||||
A --> D[Assets]
|
||||
B --> E[Wikilinks]
|
||||
B --> F[Tags]
|
||||
E --> G[Graph View]
|
||||
```
|
||||
````
|
||||
|
||||
## Extension Ecosystem
|
||||
|
||||
Extend Foam's capabilities with complementary extensions.
|
||||
Look for them in the [VS Code Marketplace](https://marketplace.visualstudio.com/).
|
||||
|
||||
## What's Next?
|
||||
|
||||
With VS Code mastery, explore advanced Foam topics:
|
||||
|
||||
1. **[[recommended-extensions]]** - See complementary extensions to improve your note taking experience
|
||||
2. **[[publishing]]** - Share your knowledge base
|
||||
|
||||
## Settings
|
||||
|
||||
To view or change the settings in VS Code, press `cmd+,` on Mac and `ctrl+,` on Windows/Linux.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[tags]: ../features/tags.md "Tags"
|
||||
[//end]: # "Autogenerated link references"
|
||||
[recommended-extensions]: recommended-extensions.md "Recommended Extensions"
|
||||
[publishing]: ../publishing/publishing.md "Publishing pages"
|
||||
[//end]: # "Autogenerated link references"
|
||||
|
||||
79
docs/user/getting-started/installation.md
Normal file
79
docs/user/getting-started/installation.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# Installation
|
||||
|
||||
Getting started with Foam is straightforward. This guide will walk you through installing everything you need to begin your knowledge management journey.
|
||||
|
||||
## Step 1: Install Visual Studio Code
|
||||
|
||||
Foam is built on VS Code, Microsoft's free, open-source code editor. You can download it at https://code.visualstudio.com/
|
||||
|
||||
### Why VS Code?
|
||||
|
||||
VS Code provides:
|
||||
|
||||
- Excellent Markdown editing capabilities
|
||||
- Rich extension ecosystem
|
||||
- Cross-platform compatibility
|
||||
- Integrated terminal and Git support
|
||||
- Customizable interface and shortcuts
|
||||
|
||||
To learn more about using VS Code with Foam, check [[get-started-with-vscode]]
|
||||
|
||||
## Step 2: Install the Foam Extension
|
||||
|
||||
The Foam extension adds knowledge management superpowers to VS Code.
|
||||
|
||||
1. **Open VS Code**
|
||||
2. **Click the Extensions icon** in the sidebar (or press `Ctrl+Shift+X` / `Cmd+Shift+X`)
|
||||
3. **Search for "Foam"** in the extensions marketplace
|
||||
4. **Click "Install"** on the official Foam extension by Foam Team
|
||||
5. **Reload VS Code** when prompted
|
||||
|
||||
### What the Foam Extension Provides
|
||||
|
||||
- Wikilink auto-completion and navigation
|
||||
- Backlink discovery and panel
|
||||
- Graph visualization
|
||||
- Powerful note template engine
|
||||
- Daily notes functionality
|
||||
|
||||
## Step 3: Install Recommended Extensions
|
||||
|
||||
While Foam works on its own, it is focused on the networking aspect of your notes. You might want to install additional extensions to improve the editing experience or the functionility of your notes.
|
||||
|
||||
### Useful Extensions
|
||||
|
||||
- **Markdown All in One** - Rich Markdown editing features. Highly recommended.
|
||||
|
||||
Other extensions:
|
||||
|
||||
- **Spell Right** - Spell checking for your notes
|
||||
- **Paste Image** - Easily insert images from clipboard
|
||||
- **Todo Tree** - Track TODO items across your workspace
|
||||
|
||||
## What's Next?
|
||||
|
||||
Now that Foam is installed, you're ready to:
|
||||
|
||||
1. **[[first-workspace]]** - Set up your knowledge base structure
|
||||
2. **[[get-started-with-vscode]]** - Learn how to use VS Code for note taking
|
||||
3. **[[note-taking-in-foam]]** - Write your first Markdown notes
|
||||
4. **[[navigation]]** - Connect your thoughts with wikilinks
|
||||
5. **[[graph-view]]** - Visualize your knowledge network
|
||||
|
||||
## Getting Help
|
||||
|
||||
If you encounter issues:
|
||||
|
||||
- Check the [[frequently-asked-questions]] for common problems
|
||||
- Visit the [Foam Community Discord](https://foambubble.github.io/join-discord/w)
|
||||
- Browse [GitHub Issues](https://github.com/foambubble/foam/issues) for known problems
|
||||
- Ask questions in [GitHub Discussions](https://github.com/foambubble/foam/discussions)
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[get-started-with-vscode]: get-started-with-vscode.md "Using Foam with VS Code Features"
|
||||
[first-workspace]: first-workspace.md "Creating Your First Workspace"
|
||||
[note-taking-in-foam]: note-taking-in-foam.md "Note-Taking in Foam"
|
||||
[navigation]: navigation.md "Navigation in Foam"
|
||||
[graph-view]: ../features/graph-view.md "Graph Visualization"
|
||||
[frequently-asked-questions]: ../frequently-asked-questions.md "Frequently Asked Questions"
|
||||
[//end]: # "Autogenerated link references"
|
||||
139
docs/user/getting-started/navigation.md
Normal file
139
docs/user/getting-started/navigation.md
Normal file
@@ -0,0 +1,139 @@
|
||||
# Navigation in Foam
|
||||
|
||||
Navigation is where Foam truly shines. Unlike traditional file systems or notebooks, Foam lets you move through your knowledge by following connections between ideas. This guide will teach you how to navigate efficiently using wikilinks, backlinks, and other powerful features.
|
||||
|
||||
_[📹 Watch: Mastering navigation in Foam]_
|
||||
|
||||
## Understanding Wikilinks
|
||||
|
||||
Wikilinks are the backbone of Foam navigation. They connect your thoughts and let you jump between related concepts instantly.
|
||||
|
||||
### Basic Wikilink Syntax
|
||||
|
||||
```markdown
|
||||
I'm learning about [[Machine Learning]] and its applications in [[Data Science]].
|
||||
|
||||
This reminds me of my notes on [[Python Programming]] from yesterday.
|
||||
```
|
||||
|
||||
When you type `[[`, Foam shows you a list of existing notes to link to. If the note doesn't exist, Foam creates a placeholder that you can click to create the note later.
|
||||
|
||||
### Wikilink Variations
|
||||
|
||||
**Link to a specific heading:**
|
||||
|
||||
```markdown
|
||||
See the [[Project Management#Risk Assessment]] section for details.
|
||||
```
|
||||
|
||||
**Link to a specific block:**
|
||||
|
||||
```markdown
|
||||
See the [[Project Management#^block-id]] paragraph for details.
|
||||
```
|
||||
|
||||
**Link with alias:**
|
||||
|
||||
```markdown
|
||||
According to [[Einstein, Albert|Einstein]], imagination is more important than knowledge.
|
||||
```
|
||||
|
||||
### Autocomplete and Link Assistance
|
||||
|
||||
Foam provides intelligent autocomplete when creating links:
|
||||
|
||||
1. **Type `[[`** - Foam shows a dropdown of existing notes
|
||||
2. **Start typing** - The list filters to matching notes
|
||||
3. **Use arrow keys** to navigate suggestions
|
||||
4. **Press Enter** to insert the selected link
|
||||
|
||||
## The Foam Graph
|
||||
|
||||
For a visual overview of your knowledge base, Foam offers a [[graph-view]]. This feature renders your notes as nodes and the links between them as connections, creating an interactive map of your thoughts.
|
||||
|
||||
_[📹 Watch: Navigation with the Foam Graph]_
|
||||
|
||||
### Using the Graph
|
||||
|
||||
1. **Open the Command Palette** (`Ctrl+Shift+P` / `Cmd+Shift+P`)
|
||||
2. **Run the "Foam: Show Graph" command**
|
||||
3. The graph will open in a new panel. You can:
|
||||
|
||||
- **Click on a node** to navigate to that note.
|
||||
- **Pan and zoom** to explore different areas of your knowledge base.
|
||||
- **See how ideas cluster** and identify central concepts.
|
||||
|
||||
## Backlinks: The Power of Reverse Navigation
|
||||
|
||||
Backlinks show you which notes reference the current note. This creates a web of knowledge where ideas naturally connect.
|
||||
|
||||
### Viewing Backlinks
|
||||
|
||||
1. **Open any note**
|
||||
2. **Look for the "Connections" panel** in the sidebar
|
||||
3. **See all notes that link to your current note**
|
||||
4. **Click any backlink** to jump to that note
|
||||
|
||||
## Quick Navigation Features
|
||||
|
||||
### Command Palette Navigation
|
||||
|
||||
Press `Ctrl+Shift+P` / `Cmd+Shift+P` and try these commands:
|
||||
|
||||
- **"Foam: Open Random Note"** - Discover forgotten knowledge
|
||||
- **"Foam: Open Daily Note"** - Quick access to today's notes
|
||||
- **"Go to File"** (`Ctrl+P` / `Cmd+P`) - Fast file opening
|
||||
- **"Go to Symbol"** (`Ctrl+Shift+O` / `Cmd+Shift+O`) - Jump to headings within a note
|
||||
|
||||
### File Explorer Integration
|
||||
|
||||
The VS Code file explorer shows your note structure:
|
||||
|
||||
- **Click any `.md` file** to open it
|
||||
- **Use the search box** to filter files
|
||||
- **Right-click** for context menus (rename, delete, etc.)
|
||||
|
||||
Foam also supports the Note Explorer, which is like the file explorer, but centered around the Foam metadata.
|
||||
|
||||
### Quick Open
|
||||
|
||||
Press `Ctrl+P` / `Cmd+P` and start typing:
|
||||
|
||||
- **File names** - `machine` finds "machine-learning.md"
|
||||
- **Partial paths** - `daily/2025` finds daily notes from 2025
|
||||
- **Recent files** - Empty search shows recently opened files
|
||||
|
||||
## Link Management and Maintenance
|
||||
|
||||
### Finding Broken Links - Placeholders
|
||||
|
||||
In Foam broken links are considered placeholders for future notes.
|
||||
Placeholders (references to non-existent notes) appear differently:
|
||||
|
||||
- In editor: `[[missing-note]]` will be highlighted a different color
|
||||
- In preview: Shows as regular text or with special styling
|
||||
|
||||
Clicking on a placeholder in the editor will create the corresponding note.
|
||||
|
||||
**To find all placeholders:**
|
||||
|
||||
You can find placeholders by looking at the `Placeholders` treeview.
|
||||
|
||||
### Renaming and Moving Notes
|
||||
|
||||
When you rename a note file:
|
||||
|
||||
1. **Use VS Code's rename function** (`F2` key)
|
||||
2. **Foam automatically updates** all links to that note
|
||||
3. **Check the "Problems" panel** for any issues
|
||||
|
||||
Currently you cannot rename whole folders.
|
||||
|
||||
## What's Next?
|
||||
|
||||
With navigation mastered, you're ready to:
|
||||
|
||||
1. **[Explore the graph view](../features/graph-view.md)** - Visualize your knowledge network
|
||||
2. **[Learn about backlinks](../features/backlinks.md)** - Master bidirectional linking
|
||||
3. **[Set up templates](../features/templates.md)** - Standardize your note creation
|
||||
4. **[Use tags effectively](../features/tags.md)** - Add another layer of organization
|
||||
238
docs/user/getting-started/note-taking-in-foam.md
Normal file
238
docs/user/getting-started/note-taking-in-foam.md
Normal file
@@ -0,0 +1,238 @@
|
||||
# Note-Taking in Foam
|
||||
|
||||
Effective note-taking is the foundation of any knowledge management system. In Foam, you'll write notes in Markdown, a simple and powerful format that's both human-readable and widely supported. This guide will teach you everything you need to know about writing great notes in Foam.
|
||||
|
||||
## Markdown Basics
|
||||
|
||||
Markdown is a lightweight markup language that uses simple syntax to format text. Here are the essentials:
|
||||
|
||||
### Headings
|
||||
|
||||
```markdown
|
||||
# Heading 1 (Main Title)
|
||||
|
||||
## Heading 2 (Major Section)
|
||||
|
||||
### Heading 3 (Subsection)
|
||||
|
||||
#### Heading 4 (Minor Section)
|
||||
```
|
||||
|
||||
### Text Formatting
|
||||
|
||||
```markdown
|
||||
**Bold text**
|
||||
_Italic text_
|
||||
**_Bold and italic_**
|
||||
~~Strikethrough~~
|
||||
`Inline code`
|
||||
```
|
||||
|
||||
### Lists
|
||||
|
||||
```markdown
|
||||
## Unordered Lists
|
||||
|
||||
- First item
|
||||
- Second item
|
||||
- Nested item
|
||||
- Another nested item
|
||||
|
||||
## Ordered Lists
|
||||
|
||||
1. First step
|
||||
2. Second step
|
||||
1. Sub-step
|
||||
2. Another sub-step
|
||||
```
|
||||
|
||||
### Links and Images
|
||||
|
||||
```markdown
|
||||
[External link](https://example.com)
|
||||

|
||||
```
|
||||
|
||||
### Code Blocks
|
||||
|
||||
````markdown
|
||||
```javascript
|
||||
function greet(name) {
|
||||
return `Hello, ${name}!`;
|
||||
}
|
||||
```
|
||||
````
|
||||
|
||||
### Tables
|
||||
|
||||
```markdown
|
||||
| Column 1 | Column 2 | Column 3 |
|
||||
| -------- | -------- | -------- |
|
||||
| Data 1 | Data 2 | Data 3 |
|
||||
| Data 4 | Data 5 | Data 6 |
|
||||
```
|
||||
|
||||
### Quotes and Dividers
|
||||
|
||||
```markdown
|
||||
> This is a quote or important note
|
||||
> It can span multiple lines
|
||||
|
||||
---
|
||||
|
||||
Use three dashes for horizontal dividers
|
||||
```
|
||||
|
||||
_[📹 Watch: Markdown syntax essentials for note-taking]_
|
||||
|
||||
## Foam-Specific Features
|
||||
|
||||
Beyond standard Markdown, Foam adds several powerful features:
|
||||
|
||||
### Wikilinks
|
||||
|
||||
Connect your notes with double brackets:
|
||||
|
||||
```markdown
|
||||
I'm reading about [[Project Management]] and its relationship to [[Personal Productivity]].
|
||||
|
||||
This connects to [[2025-01-25-daily-note]] where I first had this insight.
|
||||
```
|
||||
|
||||
### Note Embedding
|
||||
|
||||
Include content from other notes via [[embeds]]:
|
||||
|
||||
```markdown
|
||||
![[Project Management#Key Principles]]
|
||||
|
||||
This embeds the "Key Principles" section from the Project Management note.
|
||||
```
|
||||
|
||||
### Tags
|
||||
|
||||
Organize your content with [[docs-v2/user/features/tags]]:
|
||||
|
||||
```markdown
|
||||
#productivity #learning #foam
|
||||
|
||||
Tags can be anywhere in your note and help with organization and filtering.
|
||||
```
|
||||
|
||||
Use nested tags for better organization:
|
||||
|
||||
```markdown
|
||||
#work/projects/website
|
||||
#learning/programming/javascript
|
||||
#personal/health/exercise
|
||||
```
|
||||
|
||||
Those tags will show as a tree structure in the [Tag Explorer](../features/tags.md)
|
||||
|
||||
### Note Properties (YAML Front Matter)
|
||||
|
||||
Add metadata to your notes:
|
||||
|
||||
```markdown
|
||||
---
|
||||
title: 'Advanced Note-Taking Strategies'
|
||||
tags: [productivity, learning, methods]
|
||||
created: 2025-01-25
|
||||
modified: 2025-01-25
|
||||
status: draft
|
||||
---
|
||||
|
||||
# Advanced Note-Taking Strategies
|
||||
|
||||
Your note content goes here...
|
||||
```
|
||||
|
||||
## Writing Effective Notes
|
||||
|
||||
### The Atomic Principle
|
||||
|
||||
Each note should focus on one concept or idea:
|
||||
|
||||
**Good Example:**
|
||||
|
||||
```markdown
|
||||
# The Feynman Technique
|
||||
|
||||
A learning method where you explain a concept in simple terms as if teaching it to someone else.
|
||||
|
||||
## Steps
|
||||
|
||||
1. Choose a topic to learn
|
||||
2. Explain it in simple terms
|
||||
3. Identify gaps in understanding
|
||||
4. Simplify and use analogies
|
||||
|
||||
## Why It Works
|
||||
|
||||
- Forces active engagement with material
|
||||
- Reveals knowledge gaps quickly
|
||||
- Improves retention through teaching
|
||||
|
||||
Related: [[Active Learning]] [[Study Methods]]
|
||||
```
|
||||
|
||||
**Avoid:**
|
||||
Mixing multiple unrelated concepts in one note.
|
||||
|
||||
### Use Descriptive Titles
|
||||
|
||||
Your note titles should clearly indicate the content:
|
||||
|
||||
**Good:** `REST API Design Principles`
|
||||
**Good:** `Meeting Notes - Product Roadmap Review 2025-01-25`
|
||||
**Avoid:** `Stuff I Learned Today`
|
||||
**Avoid:** `Notes`
|
||||
|
||||
### Link Generously
|
||||
|
||||
Don't hesitate to create links, even to notes that don't exist yet:
|
||||
|
||||
```markdown
|
||||
# Machine Learning Fundamentals
|
||||
|
||||
Machine learning is a subset of [[Artificial Intelligence]] that focuses on creating algorithms that can learn from [[Data]].
|
||||
|
||||
Key concepts include:
|
||||
|
||||
- [[Supervised Learning]]
|
||||
- [[Unsupervised Learning]]
|
||||
- [[Neural Networks]]
|
||||
- [[Feature Engineering]]
|
||||
|
||||
This connects to my work on [[Customer Behavior Analysis]] and [[Predictive Analytics]].
|
||||
```
|
||||
|
||||
Foam will create placeholder pages for missing notes, making it easy to fill in knowledge gaps later.
|
||||
|
||||
## Keyboard Shortcuts
|
||||
|
||||
Essential VS Code shortcuts for note-taking:
|
||||
|
||||
| Shortcut | Action |
|
||||
| ------------------------------ | --------------------- |
|
||||
| `Ctrl+N` / `Cmd+N` | New file |
|
||||
| `Ctrl+S` / `Cmd+S` | Save file |
|
||||
| `Ctrl+P` / `Cmd+P` | Quick file open |
|
||||
| `Ctrl+Shift+P` / `Cmd+Shift+P` | Command palette |
|
||||
| `Ctrl+K V` / `Cmd+K V` | Open Markdown preview |
|
||||
| `Ctrl+[` / `Cmd+[` | Decrease indent |
|
||||
| `Ctrl+]` / `Cmd+]` | Increase indent |
|
||||
| `Alt+Z` / `Option+Z` | Toggle word wrap |
|
||||
|
||||
## What's Next?
|
||||
|
||||
Now that you understand note-taking basics:
|
||||
|
||||
1. **[[navigation]]** - Learn to move efficiently between notes with wikilinks
|
||||
2. **[Explore the graph view](../features/graph-view.md)** - Visualize the connections in your knowledge base
|
||||
3. **[Set up templates](../features/templates.md)** - Create reusable note structures
|
||||
4. **[Use daily notes](../features/daily-notes.md)** - Establish a daily capture routine
|
||||
|
||||
[//begin]: # 'Autogenerated link references for markdown compatibility'
|
||||
[navigation]: navigation.md 'Navigation in Foam'
|
||||
[//end]: # 'Autogenerated link references'
|
||||
@@ -4,7 +4,7 @@ These extensions defined in `.vscode/extensions.json` are automatically installe
|
||||
|
||||
This list is subject to change.
|
||||
|
||||
- [Foam for VSCode](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode) (alpha)
|
||||
- [Foam for VSCode](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode)
|
||||
- [Markdown All In One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one)
|
||||
- [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
|
||||
|
||||
@@ -12,14 +12,13 @@ This list is subject to change.
|
||||
|
||||
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)
|
||||
- [Markdown Preview Mermaid Support](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid)
|
||||
- [Mermaid Markdown Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=bpruitt-goddard.mermaid-markdown-syntax-highlighting)
|
||||
- [Excalidraw whiteboard and sketching tool integration](https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor)
|
||||
- [VSCode PDF Viewing](https://marketplace.visualstudio.com/items?itemName=tomoki1207.pdf)
|
||||
- [Project Manager](https://marketplace.visualstudio.com/items?itemName=alefragnani.project-manager) (to quickly switch between projects)
|
||||
- [Markdown Extended](https://marketplace.visualstudio.com/items?itemName=jebbs.markdown-extended) (with `kbd` option disabled, `kbd` turns wikilinks into non-clickable buttons)
|
||||
- [GitDoc](https://marketplace.visualstudio.com/items?itemName=vsls-contrib.gitdoc) (easy version management via git auto commits)
|
||||
- [Markdown Footnotes](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-footnotes) (Adds [^footnote] syntax support to VS Code's built-in markdown preview)
|
||||
- [Todo Tree](https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.todo-tree) (Searches workspace for TODO and related comments and summarizes those lines in vs-code gutter)
|
||||
- [Emojisense](https://marketplace.visualstudio.com/items?itemName=bierner.emojisense) (provides emoji autocomplete and suggestions in markdown files)
|
||||
- [Markdown Emoji](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-emoji) (adds `:smile:` syntax support, works with emojisense to provide autocomplete for this syntax)
|
||||
- [Mermaid diagrams Support](https://marketplace.visualstudio.com/items?itemName=MermaidChart.vscode-mermaid-chart) (adds syntax highlighting for Mermaid code blocks in markdown and renders Mermaid diagrams in markdown preview)
|
||||
- [Excalidraw whiteboard and sketching tool integration](https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor) (create and edit hand-drawn style diagrams and sketches directly in VS Code)
|
||||
- [VSCode PDF Viewing](https://marketplace.visualstudio.com/items?itemName=tomoki1207.pdf) (view PDF files directly within VS Code without external applications)
|
||||
- [Project Manager](https://marketplace.visualstudio.com/items?itemName=alefragnani.project-manager) (easily switch between multiple projects and workspaces)
|
||||
- [Markdown Extended](https://marketplace.visualstudio.com/items?itemName=jebbs.markdown-extended) (extended markdown syntax support with additional formatting options - use with `kbd` option disabled as it conflicts with wikilinks)
|
||||
- [GitDoc](https://marketplace.visualstudio.com/items?itemName=vsls-contrib.gitdoc) (automatic git commits for easy version management and backup of your notes)
|
||||
- [Markdown Footnotes](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-footnotes) (adds footnote syntax support `[^footnote]` to VS Code's built-in markdown preview)
|
||||
- [Todo Tree](https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.todo-tree) (scans workspace for TODO, FIXME, and other comment tags, displaying them in a tree view and editor gutter)
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
# Writing Notes
|
||||
|
||||
Notes are simple text files with some extra flavor, in the shape of Markdown syntax and support for extra properties (see [[note-properties]]).
|
||||
|
||||
## Foam Syntax
|
||||
|
||||
Foam uses standard Markdown, with a few added twists:
|
||||
|
||||
- the title of a note (e.g. in the [[graph-visualization]]) is given by precedence based on:
|
||||
- the `title` property (see [[note-properties]])
|
||||
- the first `# heading 1` of the file
|
||||
- the file name
|
||||
|
||||
## Markdown Syntax
|
||||
|
||||
With Markdown, we can style our notes in a simple way, while keeping the document a simple text file (the best way to future-proof your writings!).
|
||||
|
||||
You can see the formatted output by running the `Markdown: Open Preview to the Side` command.
|
||||
|
||||
Here is a high level overview of Markdown, for more information on the Markdown syntax [see here](https://commonmark.org/help/).
|
||||
|
||||
# Heading 1
|
||||
|
||||
## Heading 2
|
||||
|
||||
### Heading 3
|
||||
|
||||
#### Heading 4
|
||||
|
||||
##### Heading 5
|
||||
|
||||
###### Heading 6
|
||||
|
||||
This is a [link to google](https://www.google.com).
|
||||
|
||||
This is a wikilink (aka internal link) to [[note-properties]].
|
||||
|
||||
Here is an image:
|
||||

|
||||
|
||||
> this is a blockquote
|
||||
> it can span multiple lines
|
||||
|
||||
- list item
|
||||
- list item
|
||||
- list item
|
||||
|
||||
1. One
|
||||
2. Two
|
||||
3. Three
|
||||
|
||||
This text is **in bold** and this is *italic*.
|
||||
|
||||
The following is a horizontal rule
|
||||
|
||||
---
|
||||
|
||||
This is a table:
|
||||
| Column 1 | Column 2 |
|
||||
| -------- | -------- |
|
||||
| R1C1 | R1C2 |
|
||||
| R2C1 | R2C2 |
|
||||
|
||||
You can `inline code` or
|
||||
|
||||
```text
|
||||
you can create
|
||||
code blocks
|
||||
```
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[note-properties]: ../features/note-properties.md "Note Properties"
|
||||
[graph-visualization]: ../features/graph-visualization.md "Graph Visualization"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -1,33 +1,50 @@
|
||||
# Using Foam
|
||||
|
||||
Foam is a collection VS Code extensions and recipes that power up the editor
|
||||
into a full-blown note taking system. This folder contains user documentation
|
||||
describing how to get started using Foam, what its main features are, and
|
||||
strategies for getting the most out of Foam. The full docs are included in the
|
||||
`foam-template` repo that most users start from.
|
||||
Foam is a personal knowledge management system built on [Visual Studio Code](https://code.visualstudio.com/) and [GitHub](https://github.com/). It helps you organize research, create discoverable notes, and publish your knowledge.
|
||||
|
||||
> See also [[frequently-asked-questions]].
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Wikilinks** - Connect thoughts with `[[double bracket]]` syntax
|
||||
- **Embeds** - Include content from other notes with `![[note]]` syntax
|
||||
- **Backlinks** - Automatically discover connections between notes
|
||||
- **Graph visualization** - See your knowledge network visually
|
||||
- **Daily notes** - Capture timestamped thoughts
|
||||
- **Templates** - Standardize note creation
|
||||
- **Tags** - Organize and filter content
|
||||
|
||||
## Why Choose Foam?
|
||||
|
||||
- **Free and open source** - No subscriptions or vendor lock-in
|
||||
- **Own your data** - Notes stored as standard Markdown files
|
||||
- **VS Code integration** - Leverage powerful editing and extensions
|
||||
- **Git-based** - Version control and collaboration built-in
|
||||
|
||||
Foam is like a bathtub: _What you get out of it depends on what you put into it._
|
||||
|
||||
## Getting Started
|
||||
|
||||
- [[installation]]
|
||||
- [[get-started-with-vscode]]
|
||||
- [[recommended-extensions]]
|
||||
- [[creating-new-notes]]
|
||||
- [[write-notes-in-foam]]
|
||||
- [[sync-notes-with-source-control]]
|
||||
- [[first-workspace]]
|
||||
- [[note-taking-in-foam]]
|
||||
- [[sync-notes]]
|
||||
- [[keyboard-shortcuts]]
|
||||
|
||||
## Features
|
||||
|
||||
- [[wikilinks]]
|
||||
- [[embeds]]
|
||||
- [[tags]]
|
||||
- [[backlinking]]
|
||||
- [[daily-notes]]
|
||||
- [[including-notes]]
|
||||
- [[embeds]]
|
||||
- [[spell-checking]]
|
||||
- [[graph-visualization]]
|
||||
- [[graph-view]]
|
||||
- [[note-properties]]
|
||||
- [[note-templates]]
|
||||
- [[templates]]
|
||||
- [[paste-images-from-clipboard]]
|
||||
- [[custom-markdown-preview-styles]]
|
||||
- [[link-reference-definitions]]
|
||||
@@ -53,21 +70,22 @@ See [[publishing]] for more details.
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[frequently-asked-questions]: frequently-asked-questions.md "Frequently Asked Questions"
|
||||
[get-started-with-vscode]: getting-started/get-started-with-vscode.md "Getting started with VS Code"
|
||||
[installation]: getting-started/installation.md "Installation"
|
||||
[get-started-with-vscode]: getting-started/get-started-with-vscode.md "Using Foam with VS Code Features"
|
||||
[recommended-extensions]: getting-started/recommended-extensions.md "Recommended Extensions"
|
||||
[creating-new-notes]: getting-started/creating-new-notes.md "Creating New Notes"
|
||||
[write-notes-in-foam]: getting-started/write-notes-in-foam.md "Writing Notes"
|
||||
[sync-notes-with-source-control]: getting-started/sync-notes-with-source-control.md "Sync notes with source control"
|
||||
[first-workspace]: getting-started/first-workspace.md "Creating Your First Workspace"
|
||||
[note-taking-in-foam]: getting-started/note-taking-in-foam.md "Note-Taking in Foam"
|
||||
[sync-notes]: getting-started/sync-notes.md "Sync notes with source control"
|
||||
[keyboard-shortcuts]: getting-started/keyboard-shortcuts.md "Keyboard Shortcuts"
|
||||
[wikilinks]: features/wikilinks.md "Wikilinks"
|
||||
[embeds]: features/embeds.md "Note Embeds"
|
||||
[tags]: features/tags.md "Tags"
|
||||
[backlinking]: features/backlinking.md "Backlinking"
|
||||
[backlinking]: features/backlinking.md "Backlinks"
|
||||
[daily-notes]: features/daily-notes.md "Daily Notes"
|
||||
[including-notes]: features/including-notes.md "Including notes in a note"
|
||||
[spell-checking]: features/spell-checking.md "Spell Checking"
|
||||
[graph-visualization]: features/graph-visualization.md "Graph Visualization"
|
||||
[graph-view]: features/graph-view.md "Graph Visualization"
|
||||
[note-properties]: features/note-properties.md "Note Properties"
|
||||
[note-templates]: features/note-templates.md "Note Templates"
|
||||
[templates]: features/templates.md "Note Templates"
|
||||
[paste-images-from-clipboard]: features/paste-images-from-clipboard.md "Paste Images from Clipboard"
|
||||
[custom-markdown-preview-styles]: features/custom-markdown-preview-styles.md "Custom Markdown Preview Styles"
|
||||
[link-reference-definitions]: features/link-reference-definitions.md "Link Reference Definitions"
|
||||
|
||||
173
docs/user/recipes/generate-material-for-mkdocs-site.md
Normal file
173
docs/user/recipes/generate-material-for-mkdocs-site.md
Normal file
@@ -0,0 +1,173 @@
|
||||
# Generate a site using the Material for MkDocs theme
|
||||
|
||||
Configuring a static-site generator (SSG) to publish your Foam provides access to functionality not available through Foam's default publishing mechanism. For example, compare the [original Foam documentation site](https://foambubble.github.io/foam/) with a [Material for MkDocs version](https://djplaner.github.io/foam-with-material-for-mkdocs/) created using the simple configuration detailed below. Try out the search functionality on the Material for MkDocs version. This [digital garden](https://djon.es/memex) and this [blog](https://djon.es/blog/) provide more advanced examples of Foam content published using Material for MkDocs.
|
||||
|
||||
The following explains how to configure the [Material for MkDocs theme](https://squidfunk.github.io/mkdocs-material/) for the [MkDocs SSG](https://www.mkdocs.org) to publish your Foam.
|
||||
|
||||
Like most SSGs (e.g. [Gatsby](https://www.gatsbyjs.com/) is another SSG that can be [used to publish your Foam](https://foambubble.github.io/foam/user/publishing/generate-gatsby-site)) site content is accepted in the form of Markdown files. Like those produced by Foam. SSGs differ in the languages they are written in (MkDocs is Python, Gatsby is Javascript and React) and the features they provide. MkDocs and Material for MkDocs are designed to support project documentation. Gatsby is more general purpose and provides a nice feature set.
|
||||
|
||||
You choose your poison.
|
||||
|
||||
## Requirements
|
||||
|
||||
To use Material for MkDocs to publish your Foam you need:
|
||||
|
||||
- An existing Foam workspace with content.
|
||||
- [Python installed on your computer](https://realpython.com/installing-python/).
|
||||
- Some familiarity and comfort with using the command line on your computer.
|
||||
|
||||
## Instructions
|
||||
|
||||
Configuring Material for MkDocs to publish your Foam involves the following steps:
|
||||
|
||||
1. [Install Material for MkDocs and other requirements](#install-material-for-mkdocs-and-other-requirements).
|
||||
|
||||
Install the Material for MkDocs theme, MkDocs, and other required Python modules.
|
||||
|
||||
2. [Configure Material for MkDocs for your Foam](#configure-material-for-mkdocs-for-your-foam).
|
||||
|
||||
Create a `mkdocs.yml` file in the root of your Foam workspace directory. This file configures Material for MkDocs to work with your Foam.
|
||||
|
||||
2. [Preview and test your site locally](#preview-and-test-your-site-locally).
|
||||
|
||||
Run MkDocs to preview and test your Material for MkDocs Foam site locally. Good for testing and local use.
|
||||
|
||||
3. [Further customise Material for MkDocs](#further-customise-material-for-mkdocs).
|
||||
|
||||
Explore and leverage the additional configuration settings, possible customisations, and additional themes and plugins to customise your site to your needs.
|
||||
|
||||
4. [Publish your site](#publish-your-site).
|
||||
|
||||
Publish your Material for MkDocs Foam site to the web for others to enjoy. There are many options for publishing your site, including GitHub, GitLab, Netlify, and others.
|
||||
|
||||
### Install Material for MkDocs and other requirements
|
||||
|
||||
Material for MkDocs provides [detailed installation instructions](https://squidfunk.github.io/mkdocs-material/getting-started/) which cover the full range of options for installing and configuring Material for MkDocs. The following is a summary of the recommended process.
|
||||
|
||||
1. Within your Foam workspace directory, create a [Python virtual environment](https://realpython.com/what-is-pip/#using-pip-in-a-python-virtual-environment)
|
||||
|
||||
- `python -m venv .venv`
|
||||
- `source .venv/bin/activate` (Linux/Mac) or `.venv\Scripts\activate` (Windows)
|
||||
|
||||
2. Install Material for MkDocs
|
||||
|
||||
- `pip install mkdocs-material`
|
||||
|
||||
3. Install additional Python modules
|
||||
|
||||
- `pip install mkdocs-roamlinks-plugin`
|
||||
- `pip install mkdocs-exclude`
|
||||
|
||||
### Configure Material for MkDocs for your Foam
|
||||
|
||||
To configure Material for MkDocs for your Foam workspace, create a `mkdocs.yml` file in the root of your Foam workspace directory. Below you will find a sample `mkdocs.yml` file (adapted from the [foam-mkdocs-template repository](https://github.com/Jackiexiao/foam-mkdocs-template/tree/master)). Copy and paste it into your `mkdocs.yml` file, then edit it to suit your needs. In particular, don't forget to change the `site_name` and `site_url` to match your Foam workspace. Though this can be left a little later.
|
||||
|
||||
Material for MkDocs provides documentation on both [minimal](https://squidfunk.github.io/mkdocs-material/creating-your-site/#minimal-configuration) and [advanced](https://squidfunk.github.io/mkdocs-material/creating-your-site/#advanced-configuration) configuration of `mkdocs.yml`. Which are revisited in the [customise section below](#further-customise-your-site)
|
||||
|
||||
```yaml
|
||||
site_name: My site # Change this to your site name
|
||||
site_url: https://mydomain.org/mysite # change this
|
||||
theme:
|
||||
name: material
|
||||
features:
|
||||
- navigation.expand
|
||||
- tabs
|
||||
markdown_extensions:
|
||||
- attr_list
|
||||
- pymdownx.tabbed
|
||||
- nl2br
|
||||
- toc:
|
||||
permalink: '#'
|
||||
slugify: !!python/name:pymdownx.slugs.uslugify
|
||||
- admonition
|
||||
- codehilite:
|
||||
guess_lang: false
|
||||
linenums: false
|
||||
- footnotes
|
||||
- meta
|
||||
- def_list
|
||||
- pymdownx.arithmatex
|
||||
- pymdownx.betterem:
|
||||
smart_enable: all
|
||||
- pymdownx.caret
|
||||
- pymdownx.critic
|
||||
- pymdownx.details
|
||||
- pymdownx.inlinehilite
|
||||
- pymdownx.magiclink
|
||||
- pymdownx.mark
|
||||
- pymdownx.smartsymbols
|
||||
- pymdownx.superfences
|
||||
- pymdownx.tasklist
|
||||
- pymdownx.tilde
|
||||
plugins:
|
||||
- search
|
||||
- roamlinks
|
||||
- exclude:
|
||||
glob:
|
||||
- "*.tmp"
|
||||
- "*.pdf"
|
||||
- "*.gz"
|
||||
regex:
|
||||
- '.*\.(tmp|bin|tar)$'
|
||||
```
|
||||
|
||||
### Preview and test your site locally
|
||||
|
||||
MkDocs provides a live preview server allowing you to preview and test your Material for MkDocs Foam site. The server will continue to rebuid your site as you write.
|
||||
|
||||
The simplest method to use the preview service is to run the following command whilst in the rood directory of your Foam workspace:
|
||||
|
||||
```bash
|
||||
mkdocs serve
|
||||
```
|
||||
|
||||
See the Material for MkDocs site for more, including [how to run the preview server via docker](https://squidfunk.github.io/mkdocs-material/creating-your-site/#previewing-as-you-write)
|
||||
|
||||
### Further customise your site
|
||||
|
||||
Further customisation is available through expanding the configuration of Material for MkDocs, using additional MkDocs plugins, customising HTML/CSS, using Markdown extensions, writing your own Python scripts, and more.
|
||||
|
||||
For more on the available customisation paths, see the following:
|
||||
|
||||
- Material for MkDocs [Advanced configuration](https://squidfunk.github.io/mkdocs-material/creating-your-site/#advanced-configuration) or the [Set up section](https://squidfunk.github.io/mkdocs-material/setup/)
|
||||
|
||||
For more configuration options to be included in your `mkdocs.yml` file, including customising: colours, fonts, language, icons, navigation, header, footer etc.
|
||||
|
||||
- Material for MkDocs [Customisation](https://squidfunk.github.io/mkdocs-material/customization/)
|
||||
|
||||
For advice on enhancing the visual design of your site by customising and replacing provided HTML, CSS, and Javascript.
|
||||
|
||||
- Material for MkDocs [Reference](https://squidfunk.github.io/mkdocs-material/reference/)
|
||||
|
||||
An overview of customisation methods that can be used directly within your Markdown files, including: admonitions, annotations, buttons, code blocks, content tabs, data tables, diagrams, grids, Mathematics, etc.
|
||||
|
||||
- a [catalog of 300 MkDocs projects and plugins](https://github.com/mkdocs/catalog#readme)
|
||||
|
||||
For functionality and ideas not included in Material for MkDocs, including: additional themes, plugins, and extensions.
|
||||
|
||||
### Building and publishing your site
|
||||
|
||||
As a Static Site Generator (SSG), MkDocs generates a collection of static HTML and other types of files. Publishing your site involves building those HTML files and placing them onto your web server. The method will vary depending on your web server and hosting provider.
|
||||
|
||||
The MkDocs documentation site provides an explanation of the [simplest method to publish your site to any provider](https://www.mkdocs.org/user-guide/deploying-your-docs/#other-providers) using `mkdocs build` and `scp`.
|
||||
|
||||
The Material for MkDocs [publish page](https://squidfunk.github.io/mkdocs-material/publishing-your-site/) lists options for publishing to
|
||||
|
||||
- GitHub using [mkdocs](https://squidfunk.github.io/mkdocs-material/publishing-your-site/#with-mkdocs)
|
||||
|
||||
Perhaps the simplest method, if you are already using GitHub to host your Foam workspace.
|
||||
|
||||
- GitHub using [GitHub actions](https://squidfunk.github.io/mkdocs-material/publishing-your-site/github-actions/)
|
||||
|
||||
A more automated method of publishing your site to GitHub, using GitHub actions.
|
||||
|
||||
- [GitLab](https://squidfunk.github.io/mkdocs-material/publishing-your-site/#with-mkdocs)
|
||||
|
||||
- [Cloudflage pages](https://deborahwrites.com/guides/deploy-host-mkdocs/deploy-mkdocs-material-cloudflare/)
|
||||
|
||||
- [Netlify](https://deborahwrites.com/guides/deploy-host-mkdocs/deploy-mkdocs-material-netlify/)
|
||||
|
||||
- [Fly.io](https://documentation.breadnet.co.uk/cloud/fly/mkdocs-on-fly/#prerequisites)
|
||||
|
||||
- [Scaleway](https://www.scaleway.com/en/docs/tutorials/using-bucket-website-with-mkdocs/)
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
# Make Backlinks More Prominent
|
||||
|
||||
One of the most most common early feature requests in Foam is to make the Markdown Notes Backlinks Explorer more prominent.
|
||||
|
||||
This #recipe shows you how to do that.
|
||||
|
||||
At the moment, you can drag the explorer pane to your bottom pane, and either show it side by side with another pane, or have take the full width of the editor:
|
||||
|
||||

|
||||
|
||||
In the future we'll want to improve this feature by
|
||||
|
||||
- [[materialized-backlinks]]
|
||||
- Providing more context around back link reference
|
||||
- Could be done by tweaking Markdown Notes slightly. Maybe a user setting?
|
||||
- Make back links editable using [VS Code Search Editors](https://code.visualstudio.com/updates/v1_43#_search-editors)
|
||||
- [Suggested by @Jash on Discord](https://discordapp.com/channels/729975036148056075/729978910363746315/730999992419876956)
|
||||
|
||||
[//begin]: # "Autogenerated link references for markdown compatibility"
|
||||
[materialized-backlinks]: ../../dev/proposals/materialized-backlinks.md "Materialized Backlinks (stub)"
|
||||
[//end]: # "Autogenerated link references"
|
||||
@@ -1,58 +0,0 @@
|
||||
# Custom Note Macros
|
||||
|
||||
This #recipe allows you to create custom note macros.
|
||||
|
||||
## Installation
|
||||
|
||||
**This extension is not included in the template**
|
||||
|
||||
To install search note-macros in vscode or head to [note-macros - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=NeelyInnovations.note-macros)
|
||||
|
||||
## Instructions
|
||||
|
||||
### Run macro From command pallette
|
||||
|
||||
Simply use `Ctrl+P` or `Alt+P` depend on your os, and type `Note Macros: Run A Macro` then chose the macro you want to execute.
|
||||
|
||||
### Create Custom Note Macros
|
||||
|
||||
Create your own custom macros by adding them to your `settings.json` (Code|File > Preferences > User Settings). A full example can be found at [settings.json](https://github.com/kneely/note-macros/blob/master/settings.json)
|
||||
|
||||
For example:
|
||||
|
||||
This macro creates a Weekly note in the Weekly note Directory.
|
||||
|
||||
```json
|
||||
{
|
||||
"note-macros": {
|
||||
"Weekly": [
|
||||
{
|
||||
"type": "note",
|
||||
"directory": "Weekly",
|
||||
"extension": ".md",
|
||||
"name": "weekly-note",
|
||||
"date": "yyyy-W"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For an explanation of the fields please go to [note-macros - Explanation of Fields](https://github.com/kneely/note-macros#explanation-of-fields)
|
||||
|
||||
### Add Keybindings to Run your Macros
|
||||
|
||||
in `keybindings.json` (Code|File > Preferences > Keyboard Shortcuts) add bindings to your macros:
|
||||
|
||||
```json
|
||||
{
|
||||
"key": "ctrl+cmd+/",
|
||||
"command": "note-macros.Weekly"
|
||||
}
|
||||
```
|
||||
|
||||
## Issues and Feedback
|
||||
|
||||
If you have any issues or questions please look at the [README.md](https://github.com/kneely/note-macros#note-macros) on the [note-macros](https://github.com/kneely/note-macros) GitHub.
|
||||
|
||||
If you run into any issues that are not fixed by referring to the README or feature requests please open an [issue](https://github.com/kneely/note-macros/issues).
|
||||
@@ -30,7 +30,7 @@ A #recipe is a guide, tip or strategy for getting the most out of your Foam work
|
||||
|
||||
## Discover
|
||||
|
||||
- Explore your notes using [[graph-visualization]]
|
||||
- Explore your notes using [[graph-view]]
|
||||
- Discover relationships with [[backlinking]]
|
||||
- Simulating [[unlinked-references]]
|
||||
|
||||
@@ -44,7 +44,7 @@ A #recipe is a guide, tip or strategy for getting the most out of your Foam work
|
||||
- Use shortcuts for [[creating-new-notes]]
|
||||
- Instantly create and access your [[daily-notes]]
|
||||
- Add and explore [[tags]]
|
||||
- Create [[note-templates]]
|
||||
- Create [[templates]]
|
||||
- Find [[orphans]]
|
||||
- Use custom [[note-macros]] to create weekly, monthly etc. notes
|
||||
- Draw [[diagrams-in-markdown]]
|
||||
@@ -59,7 +59,7 @@ A #recipe is a guide, tip or strategy for getting the most out of your Foam work
|
||||
- _More..._
|
||||
- VS Code Advanced Features [[todo]] [[good-first-task]]
|
||||
- Focus with Zen Mode
|
||||
- Display content of other notes in the preview tab by [[including-notes]]
|
||||
- Display content of other notes in the preview tab by [[embeds]]
|
||||
|
||||
## Version control
|
||||
|
||||
@@ -77,6 +77,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)
|
||||
- [[generate-material-for-mkdocs-site]] by [@djplaner](https://github.com/djplaner)
|
||||
- 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
|
||||
@@ -139,6 +140,7 @@ _See [[contribution-guide]] and [[how-to-write-recipes]]._
|
||||
[publish-to-vercel]: ../publishing/publish-to-vercel.md "Publish to Vercel"
|
||||
[publish-to-netlify-with-eleventy]: ../publishing/publish-to-netlify-with-eleventy.md "Publish to Netlify with Eleventy"
|
||||
[generate-gatsby-site]: ../publishing/generate-gatsby-site.md "Generate a site using Gatsby"
|
||||
[generate-material-for-mkdocs-site]: generate-material-for-mkdocs-site.md "Generate a site using the Material for MkDocs theme"
|
||||
[publish-to-github]: ../publishing/publish-to-github.md "Publish to GitHub"
|
||||
[math-support-with-mathjax]: ../publishing/math-support-with-mathjax.md "Math Support"
|
||||
[math-support-with-katex]: ../publishing/math-support-with-katex.md "Katex Math Rendering"
|
||||
|
||||
@@ -4,5 +4,5 @@
|
||||
],
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"version": "0.26.6"
|
||||
"version": "0.28.2"
|
||||
}
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
"reset": "yarn && yarn clean && yarn build",
|
||||
"clean": "lerna run clean",
|
||||
"build": "lerna run build",
|
||||
"test": "yarn workspace foam-vscode test --stream",
|
||||
"test": "yarn workspace foam-vscode test",
|
||||
"test:unit": "yarn workspace foam-vscode test:unit",
|
||||
"lint": "lerna run lint",
|
||||
"watch": "lerna run watch --concurrency 20 --stream"
|
||||
"watch": "lerna run watch --concurrency 20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"all-contributors-cli": "^6.16.1",
|
||||
|
||||
@@ -4,6 +4,135 @@ 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.28.2] - 2025-10-01
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed build for web extension (#1523)
|
||||
|
||||
## [0.28.1] - 2025-09-25
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Removed duplicate links in dataviz graph (#1511 - thanks @Tenormis)
|
||||
- Use letter case to further disambiguate note identifiers (#1519, #1303)
|
||||
- Sanitize `filepath` before creating note from template (#1520, #1216)
|
||||
|
||||
## [0.28.0] - 2025-09-24
|
||||
|
||||
Features:
|
||||
|
||||
- Added workspace symbols for note aliases (#1461)
|
||||
- Added tag navigation and peek (#1510)
|
||||
- Added support for tag refactoring (#1513)
|
||||
- Added support for wikilink images styling (#1514)
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Added support for image link title attribute (#1514)
|
||||
- Exposing FOAM_DATE_DAY_ISO variable (#1512 - thanks @ChThH)
|
||||
|
||||
## [0.27.7] - 2025-09-13
|
||||
|
||||
Features:
|
||||
|
||||
- Added `FOAM_DATE_DAY_ISO` template variable for ISO weekday number (1-7, Monday=1)
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed root-path relative links opening new notes instead of existing files (#1505)
|
||||
|
||||
## [0.27.6] - 2025-09-13
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed URI handling across scheme/authority (fixes #1404)
|
||||
|
||||
## [0.27.5] - 2025-09-06
|
||||
|
||||
Features:
|
||||
|
||||
- Added `FOAM_CURRENT_DIR` template variable for explicit current directory context (#1507)
|
||||
|
||||
## [0.27.4] - 2025-09-05
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed double template application when using absolute `filepath` properties (#1499)
|
||||
|
||||
## [0.27.3] - 2025-09-05
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Improved timezone handling for create-note when passing string date
|
||||
- Added debugging for daily note issue (#1505, #1502, #1494)
|
||||
- Deprecated daily note settings (use daily-note template instead)
|
||||
|
||||
## [0.27.2] - 2025-07-25
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Ensure absolute paths used in create-note command are relative to workspace
|
||||
- Improved Windows path handling in URIs
|
||||
|
||||
## [0.27.1] - 2025-07-24
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed handling of daily note template on Windows machines (#1492)
|
||||
|
||||
## [0.27.0] - 2025-07-23
|
||||
|
||||
Features:
|
||||
|
||||
- Introduced a unified note creation engine supporting both Markdown and JavaScript templates
|
||||
|
||||
Internal:
|
||||
|
||||
- Improved testing framework by creating a mocked VS Code environment
|
||||
|
||||
## [0.26.12] - 2025-06-18
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fix YAML parsing (#1467)
|
||||
- Improved regex parsing (#1479 - thanks @s-jacob-powell)
|
||||
|
||||
## [0.26.11] - 2025-04-19
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Support for custom fonts in graph view (#1457 - thanks @Tenormis)
|
||||
|
||||
## [0.26.10] - 2025-03-29
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- General improvment of wiki embeds (#1443)
|
||||
|
||||
## [0.26.9] - 2025-03-29
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Defensive get of link object ID in graph (#1438)
|
||||
|
||||
Internal:
|
||||
|
||||
- Updated `force-graph` library
|
||||
|
||||
## [0.26.8] - 2025-03-14
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Tag hierarchy now visible in graph (#1436)
|
||||
- Improved Notes Explorer layout
|
||||
|
||||
## [0.26.7] - 2025-03-09
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Improved parsing of tags (fixes #1434)
|
||||
|
||||
## [0.26.6] - 2025-03-08
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
@@ -25,6 +25,9 @@ const config = {
|
||||
platform: 'browser',
|
||||
format: 'cjs',
|
||||
outfile: `out/bundles/extension-web.js`,
|
||||
define: {
|
||||
global: 'globalThis',
|
||||
},
|
||||
plugins: [
|
||||
polyfillPlugin.polyfillNode({
|
||||
// Options (optional)
|
||||
@@ -64,7 +67,7 @@ async function main() {
|
||||
...config[platform],
|
||||
entryPoints: ['src/extension.ts'],
|
||||
bundle: true,
|
||||
minify: production,
|
||||
minify: false, //production,
|
||||
sourcemap: !production,
|
||||
sourcesContent: false,
|
||||
external: ['vscode'],
|
||||
|
||||
@@ -123,7 +123,7 @@ module.exports = {
|
||||
// runner: "jest-runner",
|
||||
|
||||
// The paths to modules that run some code to configure or set up the testing environment before each test
|
||||
// setupFiles: [],
|
||||
setupFiles: ['<rootDir>/src/test/support/jest-setup.ts'],
|
||||
|
||||
// A list of paths to modules that run some code to configure or set up the testing framework before each test
|
||||
setupFilesAfterEnv: ['jest-extended'],
|
||||
@@ -153,9 +153,8 @@ module.exports = {
|
||||
|
||||
// The regexp pattern or array of patterns that Jest uses to detect test files
|
||||
// This is overridden in every runCLI invocation but it's here as the default
|
||||
// for vscode-jest. We only want unit tests in the test explorer (sidebar),
|
||||
// since spec tests require the entire extension host to be launched before.
|
||||
testRegex: ['\\.test\\.ts$'],
|
||||
// for vscode-jest. Both .test.ts and .spec.ts files use the vscode-mock.
|
||||
testRegex: ['\\.(test|spec)\\.ts$'],
|
||||
|
||||
// This option allows the use of a custom results processor
|
||||
// testResultsProcessor: undefined,
|
||||
|
||||
@@ -8,19 +8,16 @@
|
||||
"type": "git"
|
||||
},
|
||||
"homepage": "https://github.com/foambubble/foam",
|
||||
"version": "0.26.6",
|
||||
"version": "0.28.2",
|
||||
"license": "MIT",
|
||||
"publisher": "foam",
|
||||
"engines": {
|
||||
"vscode": "^1.70.0"
|
||||
"vscode": "^1.96.0"
|
||||
},
|
||||
"icon": "assets/icon/FOAM_ICON_256.png",
|
||||
"categories": [
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"workspaceContains:.vscode/foam.json"
|
||||
],
|
||||
"main": "./out/bundles/extension-node.js",
|
||||
"browser": "./out/bundles/extension-web.js",
|
||||
"capabilities": {
|
||||
@@ -107,6 +104,28 @@
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
"view/item/context": [
|
||||
{
|
||||
"command": "foam-vscode.search-tag",
|
||||
"when": "view == foam-vscode.tags-explorer && viewItem == tag",
|
||||
"group": "inline",
|
||||
"icon": "$(search)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.rename-tag",
|
||||
"when": "view == foam-vscode.tags-explorer && viewItem == tag",
|
||||
"group": "inline",
|
||||
"icon": "$(edit)"
|
||||
}
|
||||
],
|
||||
"editor/context": [
|
||||
{
|
||||
"command": "foam-vscode.rename-tag",
|
||||
"when": "editorTextFocus && resourceExtname == '.md'",
|
||||
"group": "foam",
|
||||
"title": "Rename Tag"
|
||||
}
|
||||
],
|
||||
"view/title": [
|
||||
{
|
||||
"command": "foam-vscode.views.connections.show:backlinks",
|
||||
@@ -355,6 +374,16 @@
|
||||
"command": "foam-vscode.convert-link-style-incopy",
|
||||
"title": "Foam: Convert Link Format in Copy"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.search-tag",
|
||||
"title": "Foam: Search Tag",
|
||||
"icon": "$(search)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.rename-tag",
|
||||
"title": "Foam: Rename Tag",
|
||||
"icon": "$(edit)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.orphans.group-by:folder",
|
||||
"title": "Group By Folder",
|
||||
@@ -577,15 +606,18 @@
|
||||
"default": false
|
||||
},
|
||||
"foam.openDailyNote.fileExtension": {
|
||||
"deprecationMessage": "This setting is deprecated and will be removed in future versions. Please use the daily-note template: https://github.com/foambubble/foam/blob/fe0228bdcc647e85682670656b5f09203b1c2518/docs/user/features/daily-notes.md",
|
||||
"type": "string",
|
||||
"default": "md"
|
||||
},
|
||||
"foam.openDailyNote.filenameFormat": {
|
||||
"deprecationMessage": "This setting is deprecated and will be removed in future versions. Please use the daily-note template: https://github.com/foambubble/foam/blob/fe0228bdcc647e85682670656b5f09203b1c2518/docs/user/features/daily-notes.md",
|
||||
"type": "string",
|
||||
"default": "isoDate",
|
||||
"markdownDescription": "Specifies how the daily note filename is formatted. See the [dateformat docs](https://www.npmjs.com/package/dateformat) for valid formats"
|
||||
},
|
||||
"foam.openDailyNote.titleFormat": {
|
||||
"deprecationMessage": "This setting is deprecated and will be removed in future versions. Please use the daily-note template: https://github.com/foambubble/foam/blob/fe0228bdcc647e85682670656b5f09203b1c2518/docs/user/features/daily-notes.md",
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
@@ -594,6 +626,7 @@
|
||||
"markdownDescription": "Specifies how the daily note title is formatted. Will default to the filename format if set to null. See the [dateformat docs](https://www.npmjs.com/package/dateformat) for valid formats"
|
||||
},
|
||||
"foam.openDailyNote.directory": {
|
||||
"deprecationMessage": "This setting is deprecated and will be removed in future versions. Please use the daily-note template: https://github.com/foambubble/foam/blob/fe0228bdcc647e85682670656b5f09203b1c2518/docs/user/features/daily-notes.md",
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
@@ -679,12 +712,13 @@
|
||||
"test-setup": "yarn compile && yarn build && yarn test-reset-workspace",
|
||||
"test": "yarn test-setup && node ./out/test/run-tests.js",
|
||||
"test:unit": "yarn test-setup && node ./out/test/run-tests.js --unit",
|
||||
"test:unit-without-specs": "yarn test-setup && node ./out/test/run-tests.js --unit --exclude-specs",
|
||||
"test:e2e": "yarn test-setup && node ./out/test/run-tests.js --e2e",
|
||||
"lint": "dts lint src",
|
||||
"clean": "rimraf out",
|
||||
"watch": "nodemon --watch 'src/**/*.ts' --exec 'yarn build' --ext ts",
|
||||
"vscode:start-debugging": "yarn clean && yarn watch",
|
||||
"package-extension": "npx vsce package --yarn",
|
||||
"package-extension": "npx @vscode/vsce@3.6.0 package --yarn",
|
||||
"install-extension": "code --install-extension ./foam-vscode-$npm_package_version.vsix",
|
||||
"open-in-browser": "vscode-test-web --quality=stable --browser=chromium --extensionDevelopmentPath=. ",
|
||||
"publish-extension-openvsx": "npx ovsx publish foam-vscode-$npm_package_version.vsix -p $OPENVSX_TOKEN",
|
||||
@@ -697,7 +731,7 @@
|
||||
"@types/lodash": "^4.14.157",
|
||||
"@types/markdown-it": "^12.0.1",
|
||||
"@types/micromatch": "^4.0.1",
|
||||
"@types/node": "^13.11.0",
|
||||
"@types/node": "^18.0.0",
|
||||
"@types/picomatch": "^2.2.1",
|
||||
"@types/remove-markdown": "^0.1.1",
|
||||
"@types/vscode": "^1.70.0",
|
||||
@@ -726,6 +760,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"dateformat": "4.5.1",
|
||||
"dayjs": "^1.11.13",
|
||||
"detect-newline": "^3.1.0",
|
||||
"github-slugger": "^1.4.0",
|
||||
"gray-matter": "^4.0.2",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createTestNote, createTestWorkspace } from '../../test/test-utils';
|
||||
import { FoamTags } from './tags';
|
||||
import { Location } from './location';
|
||||
|
||||
describe('FoamTags', () => {
|
||||
it('Collects tags from a list of resources', () => {
|
||||
@@ -23,12 +24,17 @@ describe('FoamTags', () => {
|
||||
ws.set(pageB);
|
||||
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
['primary', [pageA.uri, pageB.uri]],
|
||||
['secondary', [pageA.uri]],
|
||||
['third', [pageB.uri]],
|
||||
[
|
||||
'primary',
|
||||
[
|
||||
Location.forObjectWithRange(pageA.uri, pageA.tags[0]),
|
||||
Location.forObjectWithRange(pageB.uri, pageB.tags[0]),
|
||||
],
|
||||
],
|
||||
['secondary', [Location.forObjectWithRange(pageA.uri, pageA.tags[1])]],
|
||||
['third', [Location.forObjectWithRange(pageB.uri, pageB.tags[1])]],
|
||||
])
|
||||
);
|
||||
});
|
||||
@@ -51,7 +57,11 @@ describe('FoamTags', () => {
|
||||
ws.set(taglessPage);
|
||||
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
expect(tags.tags).toEqual(new Map([['primary', [page.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
['primary', [Location.forObjectWithRange(page.uri, page.tags[0])]],
|
||||
])
|
||||
);
|
||||
|
||||
const newPage = createTestNote({
|
||||
uri: '/page-b.md',
|
||||
@@ -62,7 +72,17 @@ describe('FoamTags', () => {
|
||||
ws.set(newPage);
|
||||
tags.update();
|
||||
|
||||
expect(tags.tags).toEqual(new Map([['primary', [page.uri, newPage.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
[
|
||||
'primary',
|
||||
[
|
||||
Location.forObjectWithRange(page.uri, page.tags[0]),
|
||||
Location.forObjectWithRange(newPage.uri, newPage.tags[0]),
|
||||
],
|
||||
],
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('Replaces the tag when a note is updated with an altered tag', () => {
|
||||
@@ -78,7 +98,11 @@ describe('FoamTags', () => {
|
||||
ws.set(page);
|
||||
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
expect(tags.tags).toEqual(new Map([['primary', [page.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
['primary', [Location.forObjectWithRange(page.uri, page.tags[0])]],
|
||||
])
|
||||
);
|
||||
|
||||
const pageEdited = createTestNote({
|
||||
uri: '/page-a.md',
|
||||
@@ -90,7 +114,14 @@ describe('FoamTags', () => {
|
||||
ws.set(pageEdited);
|
||||
tags.update();
|
||||
|
||||
expect(tags.tags).toEqual(new Map([['new', [page.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
[
|
||||
'new',
|
||||
[Location.forObjectWithRange(pageEdited.uri, pageEdited.tags[0])],
|
||||
],
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('Updates the metadata of a tag when the note is moved', () => {
|
||||
@@ -105,7 +136,11 @@ describe('FoamTags', () => {
|
||||
ws.set(page);
|
||||
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
expect(tags.tags).toEqual(new Map([['primary', [page.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
['primary', [Location.forObjectWithRange(page.uri, page.tags[0])]],
|
||||
])
|
||||
);
|
||||
|
||||
const pageEdited = createTestNote({
|
||||
uri: '/new-place/page-a.md',
|
||||
@@ -118,7 +153,14 @@ describe('FoamTags', () => {
|
||||
ws.set(pageEdited);
|
||||
tags.update();
|
||||
|
||||
expect(tags.tags).toEqual(new Map([['primary', [pageEdited.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
[
|
||||
'primary',
|
||||
[Location.forObjectWithRange(pageEdited.uri, pageEdited.tags[0])],
|
||||
],
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('Updates the metadata of a tag when a note is deleted', () => {
|
||||
@@ -133,11 +175,15 @@ describe('FoamTags', () => {
|
||||
ws.set(page);
|
||||
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
expect(tags.tags).toEqual(new Map([['primary', [page.uri]]]));
|
||||
expect(tags.tags).toEqual(
|
||||
new Map([
|
||||
['primary', [Location.forObjectWithRange(page.uri, page.tags[0])]],
|
||||
])
|
||||
);
|
||||
|
||||
ws.delete(page.uri);
|
||||
tags.update();
|
||||
|
||||
expect(tags.tags).toEqual(new Map());
|
||||
expect(tags.tags.size).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { FoamWorkspace } from './workspace';
|
||||
import { URI } from './uri';
|
||||
import { IDisposable } from '../common/lifecycle';
|
||||
import { debounce } from 'lodash';
|
||||
import { Emitter } from '../common/event';
|
||||
import { Tag } from './note';
|
||||
import { Location } from './location';
|
||||
|
||||
export class FoamTags implements IDisposable {
|
||||
public readonly tags: Map<string, URI[]> = new Map();
|
||||
public readonly tags: Map<string, Location<Tag>[]> = new Map();
|
||||
|
||||
private onDidUpdateEmitter = new Emitter<void>();
|
||||
onDidUpdate = this.onDidUpdateEmitter.event;
|
||||
@@ -50,10 +51,10 @@ export class FoamTags implements IDisposable {
|
||||
update(): void {
|
||||
this.tags.clear();
|
||||
for (const resource of this.workspace.resources()) {
|
||||
for (const tag of new Set(resource.tags.map(t => t.label))) {
|
||||
const tagMeta = this.tags.get(tag) ?? [];
|
||||
tagMeta.push(resource.uri);
|
||||
this.tags.set(tag, tagMeta);
|
||||
for (const tag of resource.tags) {
|
||||
const tagLocations = this.tags.get(tag.label) ?? [];
|
||||
tagLocations.push(Location.forObjectWithRange(resource.uri, tag));
|
||||
this.tags.set(tag.label, tagLocations);
|
||||
}
|
||||
}
|
||||
this.onDidUpdateEmitter.fire();
|
||||
|
||||
@@ -7,13 +7,16 @@ describe('Foam URI', () => {
|
||||
describe('URI parsing', () => {
|
||||
const base = URI.file('/path/to/file.md');
|
||||
test.each([
|
||||
['https://www.google.com', URI.parse('https://www.google.com')],
|
||||
['/path/to/a/file.md', URI.parse('file:///path/to/a/file.md')],
|
||||
['../relative/file.md', URI.parse('file:///path/relative/file.md')],
|
||||
['https://www.google.com', URI.parse('https://www.google.com', 'file')],
|
||||
['/path/to/a/file.md', URI.parse('file:///path/to/a/file.md', 'file')],
|
||||
[
|
||||
'../relative/file.md',
|
||||
URI.parse('file:///path/relative/file.md', 'file'),
|
||||
],
|
||||
['#section', base.with({ fragment: 'section' })],
|
||||
[
|
||||
'../relative/file.md#section',
|
||||
URI.parse('file:/path/relative/file.md#section'),
|
||||
URI.parse('file:///path/relative/file.md#section', 'file'),
|
||||
],
|
||||
])('URI Parsing (%s)', (input, exp) => {
|
||||
const result = base.resolve(input);
|
||||
@@ -25,8 +28,8 @@ describe('Foam URI', () => {
|
||||
});
|
||||
|
||||
it('normalizes the Windows drive letter to upper case', () => {
|
||||
const upperCase = URI.parse('file:///C:/this/is/a/Path');
|
||||
const lowerCase = URI.parse('file:///c:/this/is/a/Path');
|
||||
const upperCase = URI.parse('file:///C:/this/is/a/Path', 'file');
|
||||
const lowerCase = URI.parse('file:///c:/this/is/a/Path', 'file');
|
||||
expect(upperCase.path).toEqual('/C:/this/is/a/Path');
|
||||
expect(lowerCase.path).toEqual('/C:/this/is/a/Path');
|
||||
expect(upperCase.toFsPath()).toEqual('C:\\this\\is\\a\\Path');
|
||||
@@ -35,11 +38,11 @@ describe('Foam URI', () => {
|
||||
|
||||
it('consistently parses file paths', () => {
|
||||
const win1 = URI.file('c:\\this\\is\\a\\path');
|
||||
const win2 = URI.parse('c:\\this\\is\\a\\path');
|
||||
const win2 = URI.parse('c:\\this\\is\\a\\path', 'file');
|
||||
expect(win1).toEqual(win2);
|
||||
|
||||
const unix1 = URI.file('/this/is/a/path');
|
||||
const unix2 = URI.parse('/this/is/a/path');
|
||||
const unix2 = URI.parse('/this/is/a/path', 'file');
|
||||
expect(unix1).toEqual(unix2);
|
||||
});
|
||||
|
||||
@@ -124,4 +127,133 @@ describe('asAbsoluteUri', () => {
|
||||
asAbsoluteUri(uri, [workspaceFolder1, workspaceFolder2, workspaceFolder3])
|
||||
).toEqual(workspaceFolder2.joinPath('file'));
|
||||
});
|
||||
|
||||
describe('forceSubfolder parameter', () => {
|
||||
it('should return the URI as-is when it is already a subfolder of a base folder', () => {
|
||||
const absolutePath = '/workspace/subfolder/file.md';
|
||||
const baseFolder = URI.file('/workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
expect(result.path).toEqual('/workspace/subfolder/file.md');
|
||||
});
|
||||
|
||||
it('should force URI to be a subfolder when forceSubfolder is true and URI is not a subfolder', () => {
|
||||
const absolutePath = '/other/path/file.md';
|
||||
const baseFolder = URI.file('/workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
expect(result.path).toEqual('/workspace/other/path/file.md');
|
||||
});
|
||||
|
||||
it('should use case-sensitive path comparison when checking if URI is already a subfolder', () => {
|
||||
const absolutePath = '/Workspace/subfolder/file.md'; // Different case
|
||||
const baseFolder = URI.file('/workspace'); // lowercase
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should be forced to subfolder because case-sensitive comparison fails
|
||||
expect(result.path).toEqual('/workspace/Workspace/subfolder/file.md');
|
||||
});
|
||||
|
||||
it('should not force subfolder when URI is exactly a case-sensitive match', () => {
|
||||
const absolutePath = '/workspace/subfolder/file.md';
|
||||
const baseFolder = URI.file('/workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should not be forced because it's already a subfolder (case matches)
|
||||
expect(result.path).toEqual('/workspace/subfolder/file.md');
|
||||
});
|
||||
|
||||
it('should handle multiple base folders when checking subfolder status', () => {
|
||||
const absolutePath = '/project2/subfolder/file.md';
|
||||
const baseFolder1 = URI.file('/project1');
|
||||
const baseFolder2 = URI.file('/project2');
|
||||
const result = asAbsoluteUri(
|
||||
absolutePath,
|
||||
[baseFolder1, baseFolder2],
|
||||
true
|
||||
);
|
||||
|
||||
// Should not be forced because it's already a subfolder of baseFolder2
|
||||
expect(result.path).toEqual('/project2/subfolder/file.md');
|
||||
});
|
||||
|
||||
describe('Windows paths', () => {
|
||||
it('should return the Windows URI as-is when it is already a subfolder of a base folder', () => {
|
||||
const absolutePath = 'C:\\workspace\\subfolder\\file.md';
|
||||
const baseFolder = URI.file('C:\\workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
expect(result.toFsPath()).toEqual('C:\\workspace\\subfolder\\file.md');
|
||||
});
|
||||
|
||||
it('should force Windows URI to be a subfolder when forceSubfolder is true and URI is not a subfolder', () => {
|
||||
const absolutePath = 'D:\\other\\path\\file.md';
|
||||
const baseFolder = URI.file('C:\\workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
expect(result.toFsPath()).toEqual(
|
||||
'C:\\workspace\\D:\\other\\path\\file.md'
|
||||
);
|
||||
});
|
||||
|
||||
it('should use case-insensitive path comparison for Windows paths when checking if URI is already a subfolder', () => {
|
||||
const absolutePath = 'C:\\Workspace\\subfolder\\file.md'; // Different case
|
||||
const baseFolder = URI.file('C:\\workspace'); // lowercase
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should be forced to subfolder because case-sensitive comparison fails
|
||||
expect(result.toFsPath()).toEqual('C:\\Workspace\\subfolder\\file.md');
|
||||
});
|
||||
|
||||
it('should not force Windows subfolder when URI is exactly a case-sensitive match', () => {
|
||||
const absolutePath = 'C:\\workspace\\subfolder\\file.md';
|
||||
const baseFolder = URI.file('C:\\workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should not be forced because it's already a subfolder (case matches)
|
||||
expect(result.toFsPath()).toEqual('C:\\workspace\\subfolder\\file.md');
|
||||
});
|
||||
|
||||
it('should handle different drive letters as non-subfolders', () => {
|
||||
const absolutePath = 'D:\\workspace\\subfolder\\file.md'; // Different drive
|
||||
const baseFolder = URI.file('C:\\workspace'); // Same path, different drive
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should be forced because different drives are not subfolders
|
||||
expect(result.toFsPath()).toEqual(
|
||||
'C:\\workspace\\D:\\workspace\\subfolder\\file.md'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle Windows backslash paths in case-sensitive comparison', () => {
|
||||
const absolutePath = 'C:\\Workspace\\subfolder\\file.md'; // Different case with backslashes
|
||||
const baseFolder = URI.file('c:\\Workspace'); // lowercase with backslashes
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should be forced to subfolder because case-sensitive comparison fails
|
||||
// Note: Drive letters are normalized to uppercase by URI.file()
|
||||
expect(result.toFsPath()).toEqual('C:\\Workspace\\subfolder\\file.md');
|
||||
});
|
||||
|
||||
it('should handle Windows backslash paths in case-sensitive comparison - reverse', () => {
|
||||
const absolutePath = 'c:\\Workspace\\subfolder\\file.md'; // Different case with backslashes
|
||||
const baseFolder = URI.file('C:\\Workspace'); // lowercase with backslashes
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
// Should be forced to subfolder because case-sensitive comparison fails
|
||||
// Note: Drive letters are normalized to uppercase by URI.file()
|
||||
expect(result.toFsPath()).toEqual('C:\\Workspace\\subfolder\\file.md');
|
||||
});
|
||||
|
||||
it('should handle forward slash absolute path also with windows base folders', () => {
|
||||
// Using this format for the path works on both windows and unix
|
||||
// and allows using absolute paths relative to the workspace root
|
||||
const absolutePath = '/subfolder/file.md';
|
||||
const baseFolder = URI.file('C:\\Workspace');
|
||||
const result = asAbsoluteUri(absolutePath, [baseFolder], true);
|
||||
|
||||
expect(result.toFsPath()).toEqual('C:\\Workspace\\subfolder\\file.md');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// See LICENSE for details
|
||||
|
||||
import { CharCode } from '../common/charCode';
|
||||
import { isNone } from '../utils';
|
||||
import * as pathUtils from '../utils/path';
|
||||
|
||||
/**
|
||||
@@ -44,13 +45,31 @@ export class URI {
|
||||
this.fragment = from.fragment ?? _empty;
|
||||
}
|
||||
|
||||
static parse(value: string): URI {
|
||||
/**
|
||||
* Parses a string value into a URI object.
|
||||
* @param value the string value of the URI
|
||||
* @param defaultScheme the default scheme to use if none is provided in the value.
|
||||
* - if a `string`, it will be used as the default scheme
|
||||
* - if a `URI`, its scheme will be used as the default scheme
|
||||
* - if `null`, no default scheme should be used (which forces `value` to have a scheme)
|
||||
* @returns the parsed URI object
|
||||
* @throws if no scheme is provided in value and no default scheme is given
|
||||
*/
|
||||
static parse(value: string, defaultScheme: URI | string | null): URI {
|
||||
const match = _regexp.exec(value);
|
||||
if (!match) {
|
||||
return new URI();
|
||||
}
|
||||
defaultScheme =
|
||||
defaultScheme instanceof URI
|
||||
? defaultScheme.scheme
|
||||
: (defaultScheme as string | null);
|
||||
const scheme = match[2] || defaultScheme;
|
||||
if (isNone(scheme)) {
|
||||
throw new Error(`Invalid URI: The URI scheme is missing: ${value}`);
|
||||
}
|
||||
return new URI({
|
||||
scheme: match[2] || 'file',
|
||||
scheme,
|
||||
authority: percentDecode(match[4] ?? _empty),
|
||||
path: pathUtils.fromFsPath(percentDecode(match[5] ?? _empty))[0],
|
||||
query: percentDecode(match[7] ?? _empty),
|
||||
@@ -73,7 +92,7 @@ export class URI {
|
||||
}
|
||||
|
||||
resolve(value: string | URI, isDirectory = false): URI {
|
||||
const uri = value instanceof URI ? value : URI.parse(value);
|
||||
const uri = value instanceof URI ? value : URI.parse(value, 'file');
|
||||
if (!uri.isAbsolute()) {
|
||||
if (uri.scheme === 'file' || uri.scheme === 'placeholder') {
|
||||
let newUri = this.with({ fragment: uri.fragment });
|
||||
@@ -124,6 +143,15 @@ export class URI {
|
||||
return new URI({ ...this, path });
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new URI with the specified changes.
|
||||
* Note that this does not validate the resulting URI, e.g. you can
|
||||
* set the path to a relative path.
|
||||
* If you want to ensure that the path is properly formatted, use `forPath` instead.
|
||||
*
|
||||
* @param change an object that describes the desired changes to the URI.
|
||||
* @returns a new URI instance with the updated fields
|
||||
*/
|
||||
with(change: {
|
||||
scheme?: string;
|
||||
authority?: string;
|
||||
@@ -140,6 +168,20 @@ export class URI {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new URI with the specified path.
|
||||
* The difference between `with({ path })` and `forPath(path)` is that
|
||||
* this function will ensure that the path is properly formatted (e.g. starting with a `/`)
|
||||
* whereas `with` will take the path "as is".
|
||||
*
|
||||
* @param path the new path
|
||||
* @returns a new URI instance with the updated path
|
||||
*/
|
||||
forPath(path: string): URI {
|
||||
const formattedPath = pathUtils.fromFsPath(percentDecode(path))[0];
|
||||
return new URI({ ...this, path: formattedPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI without the fragment and query information
|
||||
*/
|
||||
@@ -393,20 +435,38 @@ function encodeURIComponentMinimal(path: string): string {
|
||||
*
|
||||
* @param uri the uri to evaluate
|
||||
* @param baseFolders the base folders to use
|
||||
* @param forceSubfolder if true, if the URI is not a subfolder of any baseFolder,
|
||||
* it will be forced to be a subfolder of the first base folder
|
||||
* @returns an absolute uri
|
||||
*
|
||||
* TODO this probably needs to be moved to the workspace service
|
||||
*/
|
||||
export function asAbsoluteUri(
|
||||
uriOrPath: URI | string,
|
||||
baseFolders: URI[]
|
||||
baseFolders: URI[],
|
||||
forceSubfolder = false
|
||||
): URI {
|
||||
if (baseFolders.length === 0) {
|
||||
throw new Error('At least one base folder needed to compute URI');
|
||||
}
|
||||
const path = uriOrPath instanceof URI ? uriOrPath.path : uriOrPath;
|
||||
if (path.startsWith('/')) {
|
||||
return uriOrPath instanceof URI ? uriOrPath : baseFolders[0].with({ path });
|
||||
|
||||
const isDrivePath = /^[a-zA-Z]:/.test(path);
|
||||
// Check if this is already a POSIX absolute path
|
||||
if (path.startsWith('/') || isDrivePath) {
|
||||
const uri = baseFolders[0].forPath(path); // Validate the path
|
||||
|
||||
if (forceSubfolder) {
|
||||
const isAlreadySubfolder = baseFolders.some(folder =>
|
||||
isDrivePath
|
||||
? uri.path.toLowerCase().startsWith(folder.path.toLowerCase())
|
||||
: uri.path.startsWith(folder.path)
|
||||
);
|
||||
if (!isAlreadySubfolder) {
|
||||
return baseFolders[0].joinPath(uri.path);
|
||||
}
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
let tokens = path.split('/');
|
||||
while (tokens[0].trim() === '') {
|
||||
|
||||
@@ -183,4 +183,41 @@ describe('Identifier computation', () => {
|
||||
workspace.getIdentifier(noteABis.uri, [noteB.uri, noteA.uri])
|
||||
).toEqual('note-a');
|
||||
});
|
||||
|
||||
it('should handle case-sensitive filenames correctly (#1303)', () => {
|
||||
const workspace = new FoamWorkspace('.md');
|
||||
const noteUppercase = createTestNote({ uri: '/a/Note.md' });
|
||||
const noteLowercase = createTestNote({ uri: '/b/note.md' });
|
||||
|
||||
workspace.set(noteUppercase).set(noteLowercase);
|
||||
|
||||
// Should find exact case matches
|
||||
expect(workspace.listByIdentifier('Note').length).toEqual(1);
|
||||
expect(workspace.listByIdentifier('Note')[0].uri.path).toEqual(
|
||||
'/a/Note.md'
|
||||
);
|
||||
|
||||
expect(workspace.listByIdentifier('note').length).toEqual(1);
|
||||
expect(workspace.listByIdentifier('note')[0].uri.path).toEqual(
|
||||
'/b/note.md'
|
||||
);
|
||||
|
||||
// Should not treat them as the same identifier
|
||||
expect(workspace.listByIdentifier('Note')[0]).not.toEqual(
|
||||
workspace.listByIdentifier('note')[0]
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate correct identifiers for case-sensitive files', () => {
|
||||
const workspace = new FoamWorkspace('.md');
|
||||
const noteUppercase = createTestNote({ uri: '/a/Note.md' });
|
||||
const noteLowercase = createTestNote({ uri: '/b/note.md' });
|
||||
|
||||
workspace.set(noteUppercase).set(noteLowercase);
|
||||
|
||||
// Each should have a unique identifier without directory disambiguation
|
||||
// since they differ by case, they are not considered conflicting
|
||||
expect(workspace.getIdentifier(noteUppercase.uri)).toEqual('Note');
|
||||
expect(workspace.getIdentifier(noteLowercase.uri)).toEqual('note');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,6 +52,16 @@ export class FoamWorkspace implements IDisposable {
|
||||
return deleted ?? null;
|
||||
}
|
||||
|
||||
clear() {
|
||||
const resources = Array.from(this._resources.values());
|
||||
this._resources.clear();
|
||||
|
||||
// Fire delete events for all resources
|
||||
resources.forEach(resource => {
|
||||
this.onDidDeleteEmitter.fire(resource);
|
||||
});
|
||||
}
|
||||
|
||||
public exists(uri: URI): boolean {
|
||||
return isSome(this.find(uri));
|
||||
}
|
||||
@@ -79,13 +89,12 @@ export class FoamWorkspace implements IDisposable {
|
||||
|
||||
public listByIdentifier(identifier: string): Resource[] {
|
||||
let needle = this.getTrieIdentifier(identifier);
|
||||
|
||||
const mdNeedle =
|
||||
getExtension(normalize(identifier)) !== this.defaultExtension
|
||||
? this.getTrieIdentifier(identifier + this.defaultExtension)
|
||||
: undefined;
|
||||
|
||||
const resources: Resource[] = [];
|
||||
let resources: Resource[] = [];
|
||||
|
||||
this._resources.find(needle).forEach(elm => resources.push(elm[1]));
|
||||
|
||||
@@ -93,6 +102,15 @@ export class FoamWorkspace implements IDisposable {
|
||||
this._resources.find(mdNeedle).forEach(elm => resources.push(elm[1]));
|
||||
}
|
||||
|
||||
// if multiple resources found, try to filter exact case matches
|
||||
if (resources.length > 1) {
|
||||
resources = resources.filter(
|
||||
r =>
|
||||
r.uri.getBasename() === identifier ||
|
||||
r.uri.getBasename() === identifier + this.defaultExtension
|
||||
);
|
||||
}
|
||||
|
||||
return resources.sort(Resource.sortByPath);
|
||||
}
|
||||
|
||||
@@ -105,7 +123,7 @@ export class FoamWorkspace implements IDisposable {
|
||||
const amongst = [];
|
||||
const basename = forResource.getBasename();
|
||||
|
||||
this.listByIdentifier(basename).map(res => {
|
||||
this.listByIdentifier(basename).forEach(res => {
|
||||
// skip self
|
||||
if (res.uri.isEqual(forResource)) {
|
||||
return;
|
||||
|
||||
@@ -152,6 +152,94 @@ describe('MarkdownLink', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('parse direct link with title attributes', () => {
|
||||
it('should parse image with double-quoted title', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('image.jpg');
|
||||
expect(parsed.alias).toEqual('alt text');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should parse image with single-quoted title', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('image.jpg');
|
||||
expect(parsed.alias).toEqual('alt text');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should handle sections with titles', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('image.jpg');
|
||||
expect(parsed.section).toEqual('section');
|
||||
expect(parsed.alias).toEqual('alt text');
|
||||
});
|
||||
|
||||
it('should handle URLs with spaces in titles', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('path/to/file.jpg');
|
||||
expect(parsed.alias).toEqual('alt');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should maintain compatibility with titleless images', () => {
|
||||
const link = parser.parse(getRandomURI(), ``)
|
||||
.links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('image.jpg');
|
||||
expect(parsed.alias).toEqual('alt text');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should handle complex URLs with titles', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('path/to/image.jpg');
|
||||
expect(parsed.alias).toEqual('alt');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should parse regular links with titles', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
`[link text](document.md "Link title")`
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('document.md');
|
||||
expect(parsed.alias).toEqual('link text');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
|
||||
it('should handle titles with special characters', () => {
|
||||
const link = parser.parse(
|
||||
getRandomURI(),
|
||||
``
|
||||
).links[0];
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('image.jpg');
|
||||
expect(parsed.alias).toEqual('alt');
|
||||
expect(parsed.section).toEqual('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('rename wikilink', () => {
|
||||
it('should rename the target only', () => {
|
||||
const link = parser.parse(
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { ResourceLink } from '../model/note';
|
||||
import { TextEdit } from './text-edit';
|
||||
|
||||
export abstract class MarkdownLink {
|
||||
private static wikilinkRegex = new RegExp(
|
||||
/\[\[([^#|]+)?#?([^|]+)?\|?(.*)?\]\]/
|
||||
);
|
||||
private static directLinkRegex = new RegExp(
|
||||
/\[(.*)\]\(<?([^#>]*)?#?([^\]>]+)?>?\)/
|
||||
/\[(.*)\]\(<?([^#>]*?)(?:#([^>\s"'()]*))?(?:\s+(?:"[^"]*"|'[^']*'))?>?\)/
|
||||
);
|
||||
|
||||
public static analyzeLink(link: ResourceLink) {
|
||||
@@ -45,7 +46,7 @@ export abstract class MarkdownLink {
|
||||
type?: 'wikilink' | 'link';
|
||||
isEmbed?: boolean;
|
||||
}
|
||||
) {
|
||||
): TextEdit {
|
||||
const { target, section, alias } = MarkdownLink.analyzeLink(link);
|
||||
const newTarget = delta.target ?? target;
|
||||
const newSection = delta.section ?? section ?? '';
|
||||
|
||||
@@ -242,6 +242,18 @@ title: - one
|
||||
|
||||
expect(note.properties).toEqual({});
|
||||
});
|
||||
|
||||
it('#1467 - should parse yaml frontmatter with colon in value', () => {
|
||||
const note = createNoteFromMarkdown(`
|
||||
---
|
||||
tags: test
|
||||
source: https://example.com/page:123
|
||||
---
|
||||
|
||||
# Note with colon in meta value\n`);
|
||||
expect(note.properties.source).toBe('https://example.com/page:123');
|
||||
expect(note.tags[0].label).toEqual('test');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Tags', () => {
|
||||
@@ -320,20 +332,55 @@ this is some #text that includes #tags we #care-about.
|
||||
]);
|
||||
});
|
||||
|
||||
it('provides rough range for tags in yaml', () => {
|
||||
it('provides a specific range for tags in yaml', () => {
|
||||
// For now it's enough to just get the YAML block range
|
||||
// in the future we might want to be more specific
|
||||
|
||||
const noteA = createNoteFromMarkdown(`
|
||||
---
|
||||
prop: hello world
|
||||
tags: [hello, world, this_is_good]
|
||||
another: i love the world
|
||||
---
|
||||
# this is a heading
|
||||
this is some text
|
||||
`);
|
||||
expect(noteA.tags[0]).toEqual({
|
||||
label: 'hello',
|
||||
range: Range.create(1, 0, 3, 3),
|
||||
range: Range.create(3, 7, 3, 12),
|
||||
});
|
||||
expect(noteA.tags[1]).toEqual({
|
||||
label: 'world',
|
||||
range: Range.create(3, 14, 3, 19),
|
||||
});
|
||||
expect(noteA.tags[2]).toEqual({
|
||||
label: 'this_is_good',
|
||||
range: Range.create(3, 21, 3, 33),
|
||||
});
|
||||
|
||||
const noteB = createNoteFromMarkdown(`
|
||||
---
|
||||
prop: hello world
|
||||
tags:
|
||||
- hello
|
||||
- world
|
||||
- this_is_good
|
||||
another: i love the world
|
||||
---
|
||||
# this is a heading
|
||||
this is some text
|
||||
`);
|
||||
expect(noteB.tags[0]).toEqual({
|
||||
label: 'hello',
|
||||
range: Range.create(4, 2, 4, 7),
|
||||
});
|
||||
expect(noteB.tags[1]).toEqual({
|
||||
label: 'world',
|
||||
range: Range.create(5, 2, 5, 7),
|
||||
});
|
||||
expect(noteB.tags[2]).toEqual({
|
||||
label: 'this_is_good',
|
||||
range: Range.create(6, 2, 6, 14),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -173,15 +173,51 @@ const getTextFromChildren = (root: Node): string => {
|
||||
return text;
|
||||
};
|
||||
|
||||
function getPropertiesInfoFromYAML(yamlText: string): {
|
||||
[key: string]: { key: string; value: string; text: string; line: number };
|
||||
} {
|
||||
const yamlProps = `\n${yamlText}`
|
||||
.split(/[\n](\w+:)/g)
|
||||
.filter(item => item.trim() !== '');
|
||||
const lines = yamlText.split('\n');
|
||||
let result: { line: number; key: string; text: string; value: string }[] = [];
|
||||
for (let i = 0; i < yamlProps.length / 2; i++) {
|
||||
const key = yamlProps[i * 2].replace(':', '');
|
||||
const value = yamlProps[i * 2 + 1].trim();
|
||||
const text = yamlProps[i * 2] + yamlProps[i * 2 + 1];
|
||||
result.push({ key, value, text, line: -1 });
|
||||
}
|
||||
result = result.map(p => {
|
||||
const line = lines.findIndex(l => l.startsWith(p.key + ':'));
|
||||
return { ...p, line };
|
||||
});
|
||||
return result.reduce((acc, curr) => {
|
||||
acc[curr.key] = curr;
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
const tagsPlugin: ParserPlugin = {
|
||||
name: 'tags',
|
||||
onDidFindProperties: (props, note, node) => {
|
||||
if (isSome(props.tags)) {
|
||||
const tagPropertyInfo = getPropertiesInfoFromYAML((node as any).value)[
|
||||
'tags'
|
||||
];
|
||||
const tagPropertyStartLine =
|
||||
node.position!.start.line + tagPropertyInfo.line;
|
||||
const tagPropertyLines = tagPropertyInfo.text.split('\n');
|
||||
const yamlTags = extractTagsFromProp(props.tags);
|
||||
for (const tag of yamlTags) {
|
||||
const tagLine = tagPropertyLines.findIndex(l => l.includes(tag));
|
||||
const line = tagPropertyStartLine + tagLine;
|
||||
const charStart = tagPropertyLines[tagLine].indexOf(tag);
|
||||
note.tags.push({
|
||||
label: tag,
|
||||
range: astPositionToFoamRange(node.position!),
|
||||
range: Range.createFromPosition(
|
||||
Position.create(line, charStart),
|
||||
Position.create(line, charStart + tag.length)
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,6 +309,101 @@ describe('Link resolution', () => {
|
||||
expect(ws.resolveLink(noteC, noteC.links[0])).toEqual(noteA.uri);
|
||||
expect(noteD.links).toEqual([]);
|
||||
});
|
||||
|
||||
describe('Workspace-relative paths (root-path relative)', () => {
|
||||
it('should resolve workspace-relative paths starting with /', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/workspace/dir1/page-a.md',
|
||||
links: [{ to: '/dir2/page-b.md' }],
|
||||
});
|
||||
const noteB = createTestNote({
|
||||
uri: '/workspace/dir2/page-b.md',
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace([URI.file('/workspace')]);
|
||||
|
||||
ws.set(noteA).set(noteB);
|
||||
expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(noteB.uri);
|
||||
});
|
||||
|
||||
it('should resolve workspace-relative paths with nested directories', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/workspace/project/notes/page-a.md',
|
||||
links: [{ to: '/project/assets/image.png' }],
|
||||
});
|
||||
const assetB = createTestNote({
|
||||
uri: '/workspace/project/assets/image.png',
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace([URI.file('/workspace')]);
|
||||
|
||||
ws.set(noteA).set(assetB);
|
||||
expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(assetB.uri);
|
||||
});
|
||||
|
||||
it('should handle workspace-relative paths with fragments', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/workspace/dir1/page-a.md',
|
||||
links: [{ to: '/dir2/page-b.md#section' }],
|
||||
});
|
||||
const noteB = createTestNote({
|
||||
uri: '/workspace/dir2/page-b.md',
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace([URI.file('/workspace')]);
|
||||
|
||||
ws.set(noteA).set(noteB);
|
||||
const resolved = ws.resolveLink(noteA, noteA.links[0]);
|
||||
expect(resolved).toEqual(noteB.uri.with({ fragment: 'section' }));
|
||||
});
|
||||
|
||||
it('should fall back to placeholder for non-existent workspace-relative paths', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/workspace/dir1/page-a.md',
|
||||
links: [{ to: '/dir2/non-existent.md' }],
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace([URI.file('/workspace')]);
|
||||
|
||||
ws.set(noteA);
|
||||
const resolved = ws.resolveLink(noteA, noteA.links[0]);
|
||||
expect(resolved.isPlaceholder()).toBe(true);
|
||||
expect(resolved.path).toEqual('/workspace/dir2/non-existent.md');
|
||||
});
|
||||
|
||||
it('should work with multiple workspace roots', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/workspace1/dir1/page-a.md',
|
||||
links: [{ to: '/shared/page-b.md' }],
|
||||
});
|
||||
const noteB = createTestNote({
|
||||
uri: '/workspace2/shared/page-b.md',
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace([
|
||||
URI.file('/workspace1'),
|
||||
URI.file('/workspace2'),
|
||||
]);
|
||||
|
||||
ws.set(noteA).set(noteB);
|
||||
expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(noteB.uri);
|
||||
});
|
||||
|
||||
it('should preserve existing absolute path behavior when no workspace roots provided', () => {
|
||||
const noteA = createTestNote({
|
||||
uri: '/path/to/page-a.md',
|
||||
links: [{ to: '/path/to/another/page-b.md' }],
|
||||
});
|
||||
const noteB = createTestNote({
|
||||
uri: '/path/to/another/page-b.md',
|
||||
});
|
||||
|
||||
const ws = createTestWorkspace();
|
||||
ws.set(noteA).set(noteB);
|
||||
// Default provider without workspace roots should work as before
|
||||
expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(noteB.uri);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ export class MarkdownResourceProvider implements ResourceProvider {
|
||||
constructor(
|
||||
private readonly dataStore: IDataStore,
|
||||
private readonly parser: ResourceParser,
|
||||
public readonly noteExtensions: string[] = ['.md']
|
||||
public readonly noteExtensions: string[] = ['.md'],
|
||||
private readonly workspaceRoots: URI[] = []
|
||||
) {}
|
||||
|
||||
supports(uri: URI) {
|
||||
@@ -82,16 +83,50 @@ export class MarkdownResourceProvider implements ResourceProvider {
|
||||
break;
|
||||
}
|
||||
case 'link': {
|
||||
// force ambiguous links to be treated as relative
|
||||
const path =
|
||||
target.startsWith('/') ||
|
||||
target.startsWith('./') ||
|
||||
target.startsWith('../')
|
||||
? target
|
||||
: './' + target;
|
||||
targetUri =
|
||||
workspace.find(path, resource.uri)?.uri ??
|
||||
URI.placeholder(resource.uri.resolve(path).path);
|
||||
let path: string;
|
||||
let foundResource: Resource | null = null;
|
||||
|
||||
if (target.startsWith('/')) {
|
||||
// Handle workspace-relative paths (root-path relative)
|
||||
if (this.workspaceRoots.length > 0) {
|
||||
// Try to resolve against each workspace root
|
||||
for (const workspaceRoot of this.workspaceRoots) {
|
||||
const candidatePath = target.substring(1); // Remove leading '/'
|
||||
const absolutePath = workspaceRoot.joinPath(candidatePath);
|
||||
const found = workspace.find(absolutePath);
|
||||
if (found) {
|
||||
foundResource = found;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundResource) {
|
||||
// Not found in any workspace root, create placeholder relative to first workspace root
|
||||
const firstRoot = this.workspaceRoots[0];
|
||||
const candidatePath = target.substring(1);
|
||||
const absolutePath = firstRoot.joinPath(candidatePath);
|
||||
targetUri = URI.placeholder(absolutePath.path);
|
||||
} else {
|
||||
targetUri = foundResource.uri;
|
||||
}
|
||||
} else {
|
||||
// No workspace roots provided, fall back to existing behavior
|
||||
path = target;
|
||||
targetUri =
|
||||
workspace.find(path, resource.uri)?.uri ??
|
||||
URI.placeholder(resource.uri.resolve(path).path);
|
||||
}
|
||||
} else {
|
||||
// Handle relative paths and non-root paths
|
||||
path =
|
||||
target.startsWith('./') || target.startsWith('../')
|
||||
? target
|
||||
: './' + target;
|
||||
targetUri =
|
||||
workspace.find(path, resource.uri)?.uri ??
|
||||
URI.placeholder(resource.uri.resolve(path).path);
|
||||
}
|
||||
|
||||
if (section && !targetUri.isPlaceholder()) {
|
||||
targetUri = targetUri.with({ fragment: section });
|
||||
}
|
||||
|
||||
611
packages/foam-vscode/src/core/services/tag-edit.test.ts
Normal file
611
packages/foam-vscode/src/core/services/tag-edit.test.ts
Normal file
@@ -0,0 +1,611 @@
|
||||
import { createTestNote, createTestWorkspace } from '../../test/test-utils';
|
||||
import { FoamTags } from '../model/tags';
|
||||
import { TagEdit } from './tag-edit';
|
||||
import { Range } from '../model/range';
|
||||
import { Position } from '../model/position';
|
||||
import { URI } from '../model/uri';
|
||||
|
||||
describe('TagEdit', () => {
|
||||
describe('createRenameTagEdits', () => {
|
||||
it('should generate edits for all occurrences of a tag', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const pageA = createTestNote({
|
||||
uri: '/page-a.md',
|
||||
title: 'Page A',
|
||||
tags: ['oldtag', 'anothertag'],
|
||||
});
|
||||
|
||||
// Manually set the ranges for testing
|
||||
pageA.tags[0].range = Range.create(0, 5, 0, 11);
|
||||
pageA.tags[1].range = Range.create(1, 5, 1, 15);
|
||||
|
||||
const pageB = createTestNote({
|
||||
uri: '/page-b.md',
|
||||
title: 'Page B',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
// Manually set the range for testing
|
||||
pageB.tags[0].range = Range.create(2, 10, 2, 16);
|
||||
|
||||
ws.set(pageA);
|
||||
ws.set(pageB);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createRenameTagEdits(foamTags, 'oldtag', 'newtag');
|
||||
|
||||
expect(result.totalOccurrences).toBe(2);
|
||||
expect(result.edits).toHaveLength(2);
|
||||
|
||||
// Check edits - should contain one edit for each page
|
||||
const pageAEdit = result.edits.find(
|
||||
e => e.uri.toString() === 'file:///page-a.md'
|
||||
);
|
||||
expect(pageAEdit).toBeDefined();
|
||||
expect(pageAEdit!.edit).toEqual({
|
||||
range: Range.create(0, 5, 0, 11),
|
||||
newText: 'newtag',
|
||||
});
|
||||
|
||||
const pageBEdit = result.edits.find(
|
||||
e => e.uri.toString() === 'file:///page-b.md'
|
||||
);
|
||||
expect(pageBEdit).toBeDefined();
|
||||
expect(pageBEdit!.edit).toEqual({
|
||||
range: Range.create(2, 10, 2, 16),
|
||||
newText: 'newtag',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return empty result when tag does not exist', () => {
|
||||
const ws = createTestWorkspace();
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const result = TagEdit.createRenameTagEdits(
|
||||
foamTags,
|
||||
'nonexistent',
|
||||
'newtag'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(0);
|
||||
expect(result.edits).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should handle multiple edits in the same file', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['duplicatetag', 'duplicatetag'],
|
||||
});
|
||||
|
||||
// Manually set the ranges for testing
|
||||
page.tags[0].range = Range.create(0, 5, 0, 17);
|
||||
page.tags[1].range = Range.create(5, 10, 5, 22);
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createRenameTagEdits(
|
||||
foamTags,
|
||||
'duplicatetag',
|
||||
'newtag'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(2);
|
||||
expect(result.edits).toHaveLength(2);
|
||||
|
||||
// Filter edits for the specific page
|
||||
const pageEdits = result.edits.filter(e => e.uri.isEqual(page.uri));
|
||||
expect(pageEdits).toHaveLength(2);
|
||||
expect(pageEdits.map(e => e.edit)).toEqual([
|
||||
{
|
||||
range: Range.create(0, 5, 0, 17),
|
||||
newText: 'newtag',
|
||||
},
|
||||
{
|
||||
range: Range.create(5, 10, 5, 22),
|
||||
newText: 'newtag',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should preserve # prefix for hashtag-style tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['hashtag'],
|
||||
});
|
||||
|
||||
// Simulate a hashtag range that includes the # prefix (length = label + 1)
|
||||
page.tags[0].range = Range.create(0, 5, 0, 13); // "#hashtag" = 8 chars
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createRenameTagEdits(
|
||||
foamTags,
|
||||
'hashtag',
|
||||
'newtag'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(1);
|
||||
expect(result.edits).toHaveLength(1);
|
||||
|
||||
const pageEdit = result.edits[0];
|
||||
expect(pageEdit.uri.toString()).toBe('file:///page.md');
|
||||
expect(pageEdit.edit).toEqual({
|
||||
range: Range.create(0, 5, 0, 13),
|
||||
newText: '#newtag', // Should include # prefix
|
||||
});
|
||||
});
|
||||
|
||||
it('should not add # prefix for YAML-style tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['yamltag'],
|
||||
});
|
||||
|
||||
// Simulate a YAML tag range that does not include # prefix (length = label only)
|
||||
page.tags[0].range = Range.create(0, 5, 0, 12); // "yamltag" = 7 chars
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createRenameTagEdits(
|
||||
foamTags,
|
||||
'yamltag',
|
||||
'newtag'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(1);
|
||||
expect(result.edits).toHaveLength(1);
|
||||
|
||||
const pageEdit = result.edits[0];
|
||||
expect(pageEdit.uri.toString()).toBe('file:///page.md');
|
||||
expect(pageEdit.edit).toEqual({
|
||||
range: Range.create(0, 5, 0, 12),
|
||||
newText: 'newtag', // Should not include # prefix
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateTagRename', () => {
|
||||
it('should accept valid tag rename', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(foamTags, 'oldtag', 'newtag');
|
||||
|
||||
expect(result.isValid).toBe(true);
|
||||
expect(result.message).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should reject rename of non-existent tag', () => {
|
||||
const ws = createTestWorkspace();
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const result = TagEdit.validateTagRename(
|
||||
foamTags,
|
||||
'nonexistent',
|
||||
'newtag'
|
||||
);
|
||||
|
||||
expect(result.isValid).toBe(false);
|
||||
expect(result.message).toContain('does not exist');
|
||||
});
|
||||
|
||||
it('should reject empty new tag name', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(foamTags, 'oldtag', '');
|
||||
|
||||
expect(result.isValid).toBe(false);
|
||||
expect(result.message).toContain('cannot be empty');
|
||||
});
|
||||
|
||||
it('should detect merge when renaming to existing tag', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag', 'existingtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(
|
||||
foamTags,
|
||||
'oldtag',
|
||||
'existingtag'
|
||||
);
|
||||
|
||||
expect(result.isValid).toBe(true);
|
||||
expect(result.isMerge).toBe(true);
|
||||
expect(result.sourceOccurrences).toBe(1);
|
||||
expect(result.targetOccurrences).toBe(1);
|
||||
expect(result.message).toContain('merge');
|
||||
expect(result.message).toContain('oldtag');
|
||||
expect(result.message).toContain('existingtag');
|
||||
});
|
||||
|
||||
it('should reject tag names with spaces', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(foamTags, 'oldtag', 'new tag');
|
||||
|
||||
expect(result.isValid).toBe(false);
|
||||
expect(result.message).toContain('Invalid tag label');
|
||||
});
|
||||
|
||||
it('should handle new tag name with # prefix', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(foamTags, 'oldtag', '#newtag');
|
||||
|
||||
expect(result.isValid).toBe(true);
|
||||
expect(result.isMerge).toBe(false);
|
||||
expect(result.sourceOccurrences).toBe(1);
|
||||
expect(result.targetOccurrences).toBe(0);
|
||||
expect(result.message).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should reject renaming to same tag name', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['oldtag'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.validateTagRename(foamTags, 'oldtag', 'oldtag');
|
||||
|
||||
expect(result.isValid).toBe(false);
|
||||
expect(result.isMerge).toBe(false);
|
||||
expect(result.sourceOccurrences).toBe(1);
|
||||
expect(result.targetOccurrences).toBe(1);
|
||||
expect(result.message).toContain('same as the current name');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findChildTags', () => {
|
||||
it('should find direct child tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['project', 'project/frontend', 'project/backend', 'other'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const childTags = TagEdit.findChildTags(foamTags, 'project');
|
||||
|
||||
expect(childTags).toEqual(['project/backend', 'project/frontend']);
|
||||
});
|
||||
|
||||
it('should find nested child tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: [
|
||||
'project',
|
||||
'project/frontend',
|
||||
'project/frontend/react',
|
||||
'project/backend',
|
||||
'project/backend/api',
|
||||
'other',
|
||||
],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const childTags = TagEdit.findChildTags(foamTags, 'project');
|
||||
|
||||
expect(childTags).toEqual([
|
||||
'project/backend',
|
||||
'project/backend/api',
|
||||
'project/frontend',
|
||||
'project/frontend/react',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return empty array when no child tags exist', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['project', 'other', 'standalone'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const childTags = TagEdit.findChildTags(foamTags, 'project');
|
||||
|
||||
expect(childTags).toEqual([]);
|
||||
});
|
||||
|
||||
it('should not return partial matches', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['project', 'projectile', 'project-old'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const childTags = TagEdit.findChildTags(foamTags, 'project');
|
||||
|
||||
expect(childTags).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createHierarchicalRenameEdits', () => {
|
||||
it('should rename parent and all child tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const pageA = createTestNote({
|
||||
uri: '/page-a.md',
|
||||
title: 'Page A',
|
||||
tags: ['project', 'project/frontend'],
|
||||
});
|
||||
|
||||
const pageB = createTestNote({
|
||||
uri: '/page-b.md',
|
||||
title: 'Page B',
|
||||
tags: ['project/backend', 'other'],
|
||||
});
|
||||
|
||||
ws.set(pageA);
|
||||
ws.set(pageB);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createHierarchicalRenameEdits(
|
||||
foamTags,
|
||||
'project',
|
||||
'work'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(3); // project, project/frontend, project/backend
|
||||
expect(result.edits).toHaveLength(3);
|
||||
|
||||
// Check that all expected tags are renamed
|
||||
const editedTags = result.edits.map(edit => edit.edit.newText);
|
||||
expect(editedTags).toContain('work');
|
||||
expect(editedTags).toContain('work/frontend');
|
||||
expect(editedTags).toContain('work/backend');
|
||||
});
|
||||
|
||||
it('should handle nested hierarchies correctly', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['project', 'project/frontend', 'project/frontend/react'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createHierarchicalRenameEdits(
|
||||
foamTags,
|
||||
'project',
|
||||
'work'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(3);
|
||||
|
||||
const editedTags = result.edits.map(edit => edit.edit.newText);
|
||||
expect(editedTags).toContain('work');
|
||||
expect(editedTags).toContain('work/frontend');
|
||||
expect(editedTags).toContain('work/frontend/react');
|
||||
});
|
||||
|
||||
it('should work when parent tag has no children', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['standalone', 'other'],
|
||||
});
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
const result = TagEdit.createHierarchicalRenameEdits(
|
||||
foamTags,
|
||||
'standalone',
|
||||
'single'
|
||||
);
|
||||
|
||||
expect(result.totalOccurrences).toBe(1);
|
||||
expect(result.edits).toHaveLength(1);
|
||||
expect(result.edits[0].edit.newText).toBe('single');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTagAtPosition', () => {
|
||||
it('should find tag at exact position', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['testtag'],
|
||||
});
|
||||
|
||||
// Manually set the range for testing
|
||||
page.tags[0].range = Range.create(0, 5, 0, 12);
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
// Test positions within the tag range
|
||||
const pageUri = URI.parse('file:///page.md', 'file');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 5))
|
||||
).toBe('testtag');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 8))
|
||||
).toBe('testtag');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 12))
|
||||
).toBe('testtag');
|
||||
|
||||
// Test positions outside the tag range
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 4))
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 13))
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(1, 5))
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined for non-existent file', () => {
|
||||
const ws = createTestWorkspace();
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const nonexistentUri = URI.parse('file:///nonexistent.md', 'file');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(
|
||||
foamTags,
|
||||
nonexistentUri,
|
||||
Position.create(0, 5)
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle multiple tags and return the correct one', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['firsttag', 'secondtag'],
|
||||
});
|
||||
|
||||
// Manually set the ranges for testing
|
||||
page.tags[0].range = Range.create(0, 5, 0, 13);
|
||||
page.tags[1].range = Range.create(0, 20, 0, 29);
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
// Should return the correct tag for each position
|
||||
const pageUri = URI.parse('file:///page.md', 'file');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 8))
|
||||
).toBe('firsttag');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 25))
|
||||
).toBe('secondtag');
|
||||
|
||||
// Position between tags should return undefined
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(0, 15))
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle multiline tags', () => {
|
||||
const ws = createTestWorkspace();
|
||||
|
||||
const page = createTestNote({
|
||||
uri: '/page.md',
|
||||
title: 'Page',
|
||||
tags: ['multilinetag'],
|
||||
});
|
||||
|
||||
// Manually set the range for testing
|
||||
page.tags[0].range = Range.create(1, 10, 3, 5);
|
||||
|
||||
ws.set(page);
|
||||
|
||||
const foamTags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
// Should find tag on different lines within the range
|
||||
const pageUri = URI.parse('file:///page.md', 'file');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(1, 15))
|
||||
).toBe('multilinetag');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(2, 0))
|
||||
).toBe('multilinetag');
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(3, 3))
|
||||
).toBe('multilinetag');
|
||||
|
||||
// Should not find tag outside the range
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(1, 5))
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
TagEdit.getTagAtPosition(foamTags, pageUri, Position.create(3, 10))
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
284
packages/foam-vscode/src/core/services/tag-edit.ts
Normal file
284
packages/foam-vscode/src/core/services/tag-edit.ts
Normal file
@@ -0,0 +1,284 @@
|
||||
import { FoamTags } from '../model/tags';
|
||||
import { TextEdit, WorkspaceTextEdit } from './text-edit';
|
||||
import { Location } from '../model/location';
|
||||
import { Tag } from '../model/note';
|
||||
import { URI } from '../model/uri';
|
||||
import { Range } from '../model/range';
|
||||
import { Position } from '../model/position';
|
||||
import { WORD_REGEX } from '../utils/hashtags';
|
||||
|
||||
/**
|
||||
* Result object containing all information needed to perform a tag rename operation.
|
||||
*/
|
||||
export interface TagEditResult {
|
||||
/**
|
||||
* Array of workspace text edits to perform the tag rename operation.
|
||||
*/
|
||||
edits: WorkspaceTextEdit[];
|
||||
|
||||
/**
|
||||
* Total number of tag occurrences that will be renamed across all files.
|
||||
*/
|
||||
totalOccurrences: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class for performing tag editing operations in Foam workspaces.
|
||||
* Provides functionality to rename tags across multiple files while maintaining
|
||||
* consistency and data integrity.
|
||||
*/
|
||||
export abstract class TagEdit {
|
||||
/**
|
||||
* Generate text edits to rename a tag across the workspace.
|
||||
*
|
||||
* @param foamTags The FoamTags instance containing all tag locations
|
||||
* @param oldTagLabel The current tag label to rename (without # prefix)
|
||||
* @param newTagLabel The new tag label (without # prefix)
|
||||
* @returns TagEditResult containing all necessary workspace text edits
|
||||
*/
|
||||
public static createRenameTagEdits(
|
||||
foamTags: FoamTags,
|
||||
oldTagLabel: string,
|
||||
newTagLabel: string
|
||||
): TagEditResult {
|
||||
const tagLocations = foamTags.tags.get(oldTagLabel) ?? [];
|
||||
const workspaceEdits: WorkspaceTextEdit[] = [];
|
||||
|
||||
for (const location of tagLocations) {
|
||||
const textEdit = this.createSingleTagEdit(
|
||||
location,
|
||||
oldTagLabel,
|
||||
newTagLabel
|
||||
);
|
||||
workspaceEdits.push({
|
||||
uri: location.uri,
|
||||
edit: textEdit,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
edits: workspaceEdits,
|
||||
totalOccurrences: tagLocations.length,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single text edit for a tag location.
|
||||
*
|
||||
* @param location The location of the tag to rename
|
||||
* @param oldTagLabel The current tag label to determine original format
|
||||
* @param newTagLabel The new tag label to replace with
|
||||
* @returns TextEdit for this specific tag occurrence
|
||||
*/
|
||||
private static createSingleTagEdit(
|
||||
location: Location<Tag>,
|
||||
oldTagLabel: string,
|
||||
newTagLabel: string
|
||||
): TextEdit {
|
||||
const range = location.range;
|
||||
const rangeLength = range.end.character - range.start.character;
|
||||
|
||||
// If range length is tag label length + 1, it's a hashtag (includes #)
|
||||
// If range length equals tag label length, it's a YAML tag (no #)
|
||||
const isHashtag = rangeLength === oldTagLabel.length + 1;
|
||||
|
||||
const newText = isHashtag ? `#${newTagLabel}` : newTagLabel;
|
||||
|
||||
return {
|
||||
range: location.range,
|
||||
newText,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if a tag rename operation is safe and allowed.
|
||||
*
|
||||
* @param foamTags The FoamTags instance containing current tag information
|
||||
* @param oldTagLabel The tag being renamed (must exist in workspace)
|
||||
* @param newTagLabel The proposed new tag label (will be cleaned of # prefix)
|
||||
* @returns Validation result with merge information and statistics
|
||||
*/
|
||||
public static validateTagRename(
|
||||
foamTags: FoamTags,
|
||||
oldTagLabel: string,
|
||||
newTagLabel: string
|
||||
): {
|
||||
isValid: boolean;
|
||||
isMerge: boolean;
|
||||
sourceOccurrences: number;
|
||||
targetOccurrences: number;
|
||||
message?: string;
|
||||
} {
|
||||
const sourceOccurrences = foamTags.tags.get(oldTagLabel)?.length ?? 0;
|
||||
|
||||
// Check if old tag exists
|
||||
if (!foamTags.tags.has(oldTagLabel)) {
|
||||
return {
|
||||
isValid: false,
|
||||
isMerge: false,
|
||||
sourceOccurrences: 0,
|
||||
targetOccurrences: 0,
|
||||
message: `Tag "${oldTagLabel}" does not exist in the workspace.`,
|
||||
};
|
||||
}
|
||||
|
||||
// Clean the new tag label (remove # if present)
|
||||
const cleanNewLabel = newTagLabel?.startsWith('#')
|
||||
? newTagLabel.substring(1)
|
||||
: newTagLabel;
|
||||
|
||||
// Check if new tag label is empty or invalid
|
||||
if (!cleanNewLabel || cleanNewLabel.trim() === '') {
|
||||
return {
|
||||
isValid: false,
|
||||
isMerge: false,
|
||||
sourceOccurrences,
|
||||
targetOccurrences: 0,
|
||||
message: 'New tag label cannot be empty.',
|
||||
};
|
||||
}
|
||||
|
||||
// Check for invalid characters in tag label
|
||||
const match = cleanNewLabel.match(WORD_REGEX);
|
||||
if (!match || match[0] !== cleanNewLabel) {
|
||||
return {
|
||||
isValid: false,
|
||||
isMerge: false,
|
||||
sourceOccurrences,
|
||||
targetOccurrences: 0,
|
||||
message: 'Invalid tag label.',
|
||||
};
|
||||
}
|
||||
|
||||
// Check if renaming to same tag (no-op)
|
||||
if (cleanNewLabel === oldTagLabel) {
|
||||
return {
|
||||
isValid: false,
|
||||
isMerge: false,
|
||||
sourceOccurrences,
|
||||
targetOccurrences: sourceOccurrences,
|
||||
message: 'New tag name is the same as the current name.',
|
||||
};
|
||||
}
|
||||
|
||||
const targetOccurrences = foamTags.tags.get(cleanNewLabel)?.length ?? 0;
|
||||
const isMerge = foamTags.tags.has(cleanNewLabel);
|
||||
|
||||
return {
|
||||
isValid: true,
|
||||
isMerge: isMerge,
|
||||
sourceOccurrences,
|
||||
targetOccurrences,
|
||||
message: isMerge
|
||||
? `This will merge "${oldTagLabel}" (${sourceOccurrences} occurrence${
|
||||
sourceOccurrences !== 1 ? 's' : ''
|
||||
}) into "${cleanNewLabel}" (${targetOccurrences} occurrence${
|
||||
targetOccurrences !== 1 ? 's' : ''
|
||||
})`
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all child tags for a given parent tag.
|
||||
*
|
||||
* This method searches for tags that start with the parent tag followed by
|
||||
* a forward slash, indicating they are hierarchical children.
|
||||
*
|
||||
* @param foamTags The FoamTags instance containing all tag information
|
||||
* @param parentTag The parent tag to find children for (e.g., "project")
|
||||
* @returns Array of child tag labels (e.g., ["project/frontend", "project/backend"])
|
||||
*/
|
||||
public static findChildTags(foamTags: FoamTags, parentTag: string): string[] {
|
||||
const childTags: string[] = [];
|
||||
const parentPrefix = parentTag + '/';
|
||||
|
||||
for (const [tagLabel] of foamTags.tags) {
|
||||
if (tagLabel.startsWith(parentPrefix)) {
|
||||
childTags.push(tagLabel);
|
||||
}
|
||||
}
|
||||
|
||||
return childTags.sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create text edits to rename a parent tag and all its children hierarchically.
|
||||
*
|
||||
* This method performs a comprehensive rename operation that updates both
|
||||
* the parent tag and all child tags, maintaining the hierarchical structure
|
||||
* with the new parent name.
|
||||
*
|
||||
* @param foamTags The FoamTags instance containing all tag locations
|
||||
* @param oldParentTag The current parent tag label (without # prefix)
|
||||
* @param newParentTag The new parent tag label (without # prefix)
|
||||
* @returns TagEditResult containing all necessary workspace text edits
|
||||
*/
|
||||
public static createHierarchicalRenameEdits(
|
||||
foamTags: FoamTags,
|
||||
oldParentTag: string,
|
||||
newParentTag: string
|
||||
): TagEditResult {
|
||||
const allEdits: WorkspaceTextEdit[] = [];
|
||||
let totalOccurrences = 0;
|
||||
|
||||
// Rename the parent tag itself
|
||||
const parentResult = this.createRenameTagEdits(
|
||||
foamTags,
|
||||
oldParentTag,
|
||||
newParentTag
|
||||
);
|
||||
allEdits.push(...parentResult.edits);
|
||||
totalOccurrences += parentResult.totalOccurrences;
|
||||
|
||||
// Find and rename all child tags
|
||||
const childTags = this.findChildTags(foamTags, oldParentTag);
|
||||
for (const childTag of childTags) {
|
||||
// Replace the parent portion with the new parent name
|
||||
const newChildTag = childTag.replace(
|
||||
oldParentTag + '/',
|
||||
newParentTag + '/'
|
||||
);
|
||||
const childResult = this.createRenameTagEdits(
|
||||
foamTags,
|
||||
childTag,
|
||||
newChildTag
|
||||
);
|
||||
allEdits.push(...childResult.edits);
|
||||
totalOccurrences += childResult.totalOccurrences;
|
||||
}
|
||||
|
||||
return {
|
||||
edits: allEdits,
|
||||
totalOccurrences,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the tag at a specific position in a document.
|
||||
*
|
||||
* @param foamTags The FoamTags instance containing all tag location data
|
||||
* @param uri The URI of the file to search in
|
||||
* @param position The position in the document (line and character)
|
||||
* @returns The tag label if a tag is found at the position, undefined otherwise
|
||||
*/
|
||||
public static getTagAtPosition(
|
||||
foamTags: FoamTags,
|
||||
uri: URI,
|
||||
position: Position
|
||||
): string | undefined {
|
||||
// Search through all tags to find one that contains the given position
|
||||
for (const [tagLabel, locations] of foamTags.tags) {
|
||||
for (const location of locations) {
|
||||
if (!location.uri.isEqual(uri)) {
|
||||
continue;
|
||||
}
|
||||
if (Range.containsPosition(location.range, position)) {
|
||||
return tagLabel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import detectNewline from 'detect-newline';
|
||||
import { Position } from '../model/position';
|
||||
import { Range } from '../model/range';
|
||||
import { URI } from '../model/uri';
|
||||
|
||||
export interface TextEdit {
|
||||
range: Range;
|
||||
@@ -42,3 +43,16 @@ const getOffset = (
|
||||
}
|
||||
return offset + Math.min(position.character, lines[i]?.length ?? 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* A text edit with workspace context, combining a URI location with the edit operation.
|
||||
*
|
||||
* This interface uses composition to pair a text edit with its file location,
|
||||
* providing a self-contained unit for workspace-wide text modifications.
|
||||
*/
|
||||
export interface WorkspaceTextEdit {
|
||||
/** The URI of the file where this edit should be applied */
|
||||
uri: URI;
|
||||
/** The text edit operation to perform */
|
||||
edit: TextEdit;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { isSome } from './core';
|
||||
export const HASHTAG_REGEX =
|
||||
/(?<=^|\s)#([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;
|
||||
const WORD_REGEX =
|
||||
export const WORD_REGEX =
|
||||
/(?<=^|\s)([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;
|
||||
|
||||
export const extractHashtags = (
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { workspace } from 'vscode';
|
||||
import { createDailyNoteIfNotExists, getDailyNoteUri } from './dated-notes';
|
||||
/* @unit-ready */
|
||||
import { workspace, window } from 'vscode';
|
||||
import {
|
||||
CREATE_DAILY_NOTE_WARNING_RESPONSE,
|
||||
createDailyNoteIfNotExists,
|
||||
getDailyNoteUri,
|
||||
} from './dated-notes';
|
||||
import { isWindows } from './core/common/platform';
|
||||
import {
|
||||
cleanWorkspace,
|
||||
@@ -10,7 +15,11 @@ import {
|
||||
withModifiedFoamConfiguration,
|
||||
} from './test/test-utils-vscode';
|
||||
import { fromVsCodeUri } from './utils/vsc-utils';
|
||||
import { URI } from './core/model/uri';
|
||||
import { fileExists, readFile } from './services/editor';
|
||||
import {
|
||||
getDailyNoteTemplateCandidateUris,
|
||||
getDailyNoteTemplateUri,
|
||||
} from './services/templates';
|
||||
|
||||
describe('getDailyNoteUri', () => {
|
||||
const date = new Date('2021-02-07T00:00:00Z');
|
||||
@@ -45,25 +54,350 @@ describe('getDailyNoteUri', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Daily note template', () => {
|
||||
it('Uses the daily note variables in the template', async () => {
|
||||
const targetDate = new Date(2021, 8, 12);
|
||||
describe('Daily note creation and template processing', () => {
|
||||
const DAILY_NOTE_TEMPLATE = ['.foam', 'templates', 'daily-note.md'];
|
||||
|
||||
const template = await createFile(
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
'hello ${FOAM_DATE_MONTH_NAME} ${FOAM_DATE_DATE} hello',
|
||||
['.foam', 'templates', 'daily-note.md']
|
||||
);
|
||||
beforeEach(async () => {
|
||||
// Ensure daily note template are removed before each test
|
||||
for (const template of getDailyNoteTemplateCandidateUris()) {
|
||||
if (await fileExists(template)) {
|
||||
await deleteFile(template);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
describe('Basic daily note creation', () => {
|
||||
it('Creates a new daily note when it does not exist', async () => {
|
||||
const targetDate = new Date(2021, 8, 1);
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
|
||||
await createDailyNoteIfNotExists(targetDate);
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toEqual('hello September 12 hello');
|
||||
expect(result.didCreateFile).toBe(true);
|
||||
expect(result.uri).toEqual(uri);
|
||||
|
||||
await deleteFile(template.uri);
|
||||
const doc = await showInEditor(uri);
|
||||
expect(doc.editor.document.getText()).toContain('2021-09-01');
|
||||
});
|
||||
|
||||
it('Opens existing daily note when it already exists', async () => {
|
||||
const targetDate = new Date(2021, 8, 2);
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
|
||||
// Create the file first
|
||||
await createFile('# Existing Note\n\nContent here', [uri.getBasename()]);
|
||||
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
expect(result.didCreateFile).toBe(false);
|
||||
expect(result.uri).toEqual(uri);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
expect(doc.editor.document.getText()).toContain('Existing Note');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Template variable resolution', () => {
|
||||
it('Resolves all FOAM_DATE_* variables correctly', async () => {
|
||||
const targetDate = new Date(2021, 8, 12); // September 12, 2021
|
||||
|
||||
const template = await createFile(
|
||||
`# \${FOAM_DATE_YEAR}-\${FOAM_DATE_MONTH}-\${FOAM_DATE_DATE}
|
||||
|
||||
Year: \${FOAM_DATE_YEAR} (short: \${FOAM_DATE_YEAR_SHORT})
|
||||
Month: \${FOAM_DATE_MONTH} (name: \${FOAM_DATE_MONTH_NAME}, short: \${FOAM_DATE_MONTH_NAME_SHORT})
|
||||
Date: \${FOAM_DATE_DATE}
|
||||
Day: \${FOAM_DATE_DAY_NAME} (short: \${FOAM_DATE_DAY_NAME_SHORT})
|
||||
Week: \${FOAM_DATE_WEEK}
|
||||
Unix: \${FOAM_DATE_SECONDS_UNIX}`,
|
||||
DAILY_NOTE_TEMPLATE
|
||||
);
|
||||
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(result.uri);
|
||||
const content = doc.editor.document.getText();
|
||||
|
||||
expect(content).toContain('# 2021-09-12');
|
||||
expect(content).toContain('Year: 2021 (short: 21)');
|
||||
expect(content).toContain('Month: 09 (name: September, short: Sep)');
|
||||
expect(content).toContain('Date: 12');
|
||||
expect(content).toContain('Day: Sunday (short: Sun)');
|
||||
expect(content).toContain('Week: 36');
|
||||
|
||||
await deleteFile(template.uri);
|
||||
await deleteFile(result.uri);
|
||||
});
|
||||
|
||||
it('Resolves FOAM_TITLE variable for daily notes', async () => {
|
||||
const targetDate = new Date(2021, 8, 13);
|
||||
|
||||
const template = await createFile(
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
'# Daily Note: ${FOAM_TITLE}\n\nToday is ${FOAM_TITLE}.',
|
||||
DAILY_NOTE_TEMPLATE
|
||||
);
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toContain('Daily Note: 2021-09-13');
|
||||
expect(content).toContain('Today is 2021-09-13.');
|
||||
await deleteFile(result.uri);
|
||||
await deleteFile(template.uri);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Configuration settings', () => {
|
||||
it('Respects custom filename format', async () => {
|
||||
const targetDate = new Date(2021, 8, 14);
|
||||
const customFormat = 'yyyy-mm-dd';
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
'openDailyNote.filenameFormat',
|
||||
customFormat,
|
||||
async () => {
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
expect(uri.getBasename()).toBe('2021-09-14.md');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('Respects custom file extension', async () => {
|
||||
const targetDate = new Date(2021, 8, 15);
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
'openDailyNote.fileExtension',
|
||||
'txt',
|
||||
async () => {
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
expect(uri.getBasename()).toBe('2021-09-15.txt');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('Respects custom directory setting', async () => {
|
||||
const targetDate = new Date(2021, 8, 16);
|
||||
const customDir = 'journal/daily';
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
'openDailyNote.directory',
|
||||
customDir,
|
||||
async () => {
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
expect(uri.path).toContain('/journal/daily/');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('Uses custom title format when specified', async () => {
|
||||
const targetDate = new Date(2021, 8, 17);
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
'openDailyNote.titleFormat',
|
||||
'fullDate',
|
||||
async () => {
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toContain('# Friday, September 17, 2021');
|
||||
await deleteFile(result.uri);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Template types and processing', () => {
|
||||
it('Processes Markdown templates correctly', async () => {
|
||||
const targetDate = new Date(2021, 8, 19);
|
||||
|
||||
const template = await createFile(
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
'hello ${FOAM_DATE_MONTH_NAME} ${FOAM_DATE_DATE} hello',
|
||||
DAILY_NOTE_TEMPLATE
|
||||
);
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toEqual('hello September 19 hello');
|
||||
await deleteFile(result.uri);
|
||||
await deleteFile(template.uri);
|
||||
});
|
||||
|
||||
it('Processes JavaScript templates correctly', async () => {
|
||||
const targetDate = new Date(2021, 8, 20);
|
||||
|
||||
const jsTemplate = await createFile(
|
||||
`async function createNote ({ foamDate }) {
|
||||
const monthName = foamDate.toLocaleString('default', { month: 'long' });
|
||||
const day = foamDate.getDate();
|
||||
return {
|
||||
filepath: \`\${foamDate.getFullYear()}-\${String(foamDate.getMonth() + 1).padStart(2, '0')}-\${String(day).padStart(2, '0')}.md\`,
|
||||
content: \`# JS Template: \${monthName} \${day}\n\nGenerated by JavaScript template.\`
|
||||
};
|
||||
};`,
|
||||
['.foam', 'templates', 'daily-note.js']
|
||||
);
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toContain('# JS Template: September 20');
|
||||
expect(content).toContain('Generated by JavaScript template.');
|
||||
|
||||
await deleteFile(jsTemplate.uri);
|
||||
await deleteFile(result.uri);
|
||||
});
|
||||
|
||||
it('Falls back to default text when no template exists', async () => {
|
||||
const targetDate = new Date(2021, 8, 21);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(result.uri);
|
||||
const content = doc.editor.document.getText();
|
||||
expect(content).toContain('# 2021-09-21'); // Should use fallback text with formatted date
|
||||
});
|
||||
|
||||
it('prompts to create a daily note template if one does not exist', async () => {
|
||||
const targetDate = new Date(2021, 8, 23);
|
||||
const foam = {} as any;
|
||||
|
||||
expect(await getDailyNoteTemplateUri()).not.toBeDefined();
|
||||
|
||||
// Intercept the showWarningMessage call
|
||||
const showWarningMessageSpy = jest
|
||||
.spyOn(window, 'showWarningMessage')
|
||||
.mockResolvedValue(CREATE_DAILY_NOTE_WARNING_RESPONSE as any); // simulate user action
|
||||
|
||||
await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
expect(showWarningMessageSpy.mock.calls[0][0]).toMatch(
|
||||
/No daily note template found/
|
||||
);
|
||||
|
||||
const templateUri = await getDailyNoteTemplateUri();
|
||||
|
||||
expect(templateUri).toBeDefined();
|
||||
expect(await fileExists(templateUri)).toBe(true);
|
||||
|
||||
const templateContent = await readFile(templateUri);
|
||||
expect(templateContent).toContain('foam_template:');
|
||||
|
||||
// Clean up the created template
|
||||
await deleteFile(templateUri);
|
||||
showWarningMessageSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('Processes template frontmatter metadata correctly', async () => {
|
||||
const targetDate = new Date(2021, 8, 22);
|
||||
|
||||
const template = await createFile(
|
||||
`---
|
||||
tags: [daily, journal]
|
||||
author: foam
|
||||
---
|
||||
# Daily Note
|
||||
|
||||
Content here with \${FOAM_DATE_MONTH_NAME} \${FOAM_DATE_DATE}`,
|
||||
DAILY_NOTE_TEMPLATE
|
||||
);
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
const result = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
|
||||
const doc = await showInEditor(uri);
|
||||
const content = doc.editor.document.getText();
|
||||
|
||||
// Should not contain the frontmatter separator in final content
|
||||
expect(content).toContain(`---
|
||||
tags: [daily, journal]
|
||||
author: foam
|
||||
---`);
|
||||
expect(content).toContain('# Daily Note');
|
||||
expect(content).toContain('Content here with September 22');
|
||||
|
||||
await deleteFile(template.uri);
|
||||
await deleteFile(result.uri);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Issue #1499 - Double template application with absolute paths', () => {
|
||||
it('should not apply template twice when reopening existing daily note with absolute filepath template', async () => {
|
||||
const targetDate = new Date(2021, 8, 25);
|
||||
const TEMPLATE_WITH_ABSOLUTE_FILEPATH = `---
|
||||
foam_template:
|
||||
name: Daily note
|
||||
description: Daily note template
|
||||
filepath: '/\${FOAM_DATE_YEAR}-\${FOAM_DATE_MONTH}-\${FOAM_DATE_DATE}.md'
|
||||
---
|
||||
|
||||
# \${FOAM_DATE_YEAR}-\${FOAM_DATE_MONTH}-\${FOAM_DATE_DATE} - DAILY NOTE
|
||||
|
||||
Daily content here.`;
|
||||
|
||||
// Create the template with absolute filepath
|
||||
const template = await createFile(
|
||||
TEMPLATE_WITH_ABSOLUTE_FILEPATH,
|
||||
DAILY_NOTE_TEMPLATE
|
||||
);
|
||||
|
||||
const uri = getDailyNoteUri(targetDate);
|
||||
const foam = {} as any; // Mock Foam instance
|
||||
|
||||
// First call: Create the daily note
|
||||
const result1 = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
expect(result1.didCreateFile).toBe(true);
|
||||
|
||||
const doc1 = await showInEditor(uri);
|
||||
const content1 = doc1.editor.document.getText();
|
||||
expect(content1).toContain('# 2021-09-25 - DAILY NOTE');
|
||||
expect(content1).toContain('Daily content here.');
|
||||
|
||||
// Count how many times the template content appears (should be once)
|
||||
const templateOccurrences1 = (
|
||||
content1.match(/# 2021-09-25 - DAILY NOTE/g) || []
|
||||
).length;
|
||||
expect(templateOccurrences1).toBe(1);
|
||||
|
||||
await closeEditors();
|
||||
|
||||
// Second call: Open existing daily note (this should NOT apply template again)
|
||||
const result2 = await createDailyNoteIfNotExists(targetDate, foam);
|
||||
expect(result2.didCreateFile).toBe(false); // File already exists
|
||||
|
||||
const doc2 = await showInEditor(uri);
|
||||
const content2 = doc2.editor.document.getText();
|
||||
|
||||
// Verify template is NOT applied twice
|
||||
const templateOccurrences2 = (
|
||||
content2.match(/# 2021-09-25 - DAILY NOTE/g) || []
|
||||
).length;
|
||||
expect(templateOccurrences2).toBe(1); // Should still be 1, not 2
|
||||
|
||||
// Content should be identical to first time
|
||||
expect(content2).toEqual(content1);
|
||||
|
||||
await deleteFile(template.uri);
|
||||
await deleteFile(result1.uri);
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { Uri, window, workspace } from 'vscode';
|
||||
import { joinPath } from './core/utils/path';
|
||||
import dateFormat from 'dateformat';
|
||||
import { URI } from './core/model/uri';
|
||||
import { NoteFactory } from './services/templates';
|
||||
import { getDailyNoteTemplateUri } from './services/templates';
|
||||
import { getFoamVsCodeConfig } from './services/config';
|
||||
import { asAbsoluteWorkspaceUri, focusNote } from './services/editor';
|
||||
import { Foam } from './core/model/foam';
|
||||
import { createNote } from './features/commands/create-note';
|
||||
import { fromVsCodeUri } from './utils/vsc-utils';
|
||||
import { showInEditor } from './test/test-utils-vscode';
|
||||
|
||||
/**
|
||||
* Open the daily note file.
|
||||
@@ -12,13 +17,14 @@ import { asAbsoluteWorkspaceUri, focusNote } from './services/editor';
|
||||
* it gets created along with any folders in its path.
|
||||
*
|
||||
* @param date The target date. If not provided, the function returns immediately.
|
||||
* @param foam The Foam instance, used to create the note.
|
||||
*/
|
||||
export async function openDailyNoteFor(date?: Date) {
|
||||
export async function openDailyNoteFor(date?: Date, foam?: Foam) {
|
||||
if (date == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { didCreateFile, uri } = await createDailyNoteIfNotExists(date);
|
||||
const { didCreateFile, uri } = await createDailyNoteIfNotExists(date, foam);
|
||||
// if a new file is created, the editor is automatically created
|
||||
// but forcing the focus will block the template placeholders from working
|
||||
// so we only explicitly focus on the note if the file already exists
|
||||
@@ -54,30 +60,86 @@ export function getDailyNoteUri(date: Date): URI {
|
||||
*/
|
||||
export function getDailyNoteFileName(date: Date): string {
|
||||
const filenameFormat: string = getFoamVsCodeConfig(
|
||||
'openDailyNote.filenameFormat'
|
||||
'openDailyNote.filenameFormat',
|
||||
'yyyy-mm-dd'
|
||||
);
|
||||
const fileExtension: string = getFoamVsCodeConfig(
|
||||
'openDailyNote.fileExtension'
|
||||
'openDailyNote.fileExtension',
|
||||
'md'
|
||||
);
|
||||
|
||||
return `${dateFormat(date, filenameFormat, false)}.${fileExtension}`;
|
||||
}
|
||||
|
||||
const DEFAULT_DAILY_NOTE_TEMPLATE = `---
|
||||
foam_template:
|
||||
filepath: "/journal/\${FOAM_DATE_YEAR}-\${FOAM_DATE_MONTH}-\${FOAM_DATE_DATE}.md"
|
||||
description: "Daily note template"
|
||||
---
|
||||
# \${FOAM_DATE_YEAR}-\${FOAM_DATE_MONTH}-\${FOAM_DATE_DATE}
|
||||
|
||||
> you probably want to delete these instructions as you customize your template
|
||||
|
||||
Welcome to your new daily note template.
|
||||
The file is located in \`.foam/templates/daily-note.md\`.
|
||||
The text in this file will be used as the content of your daily note.
|
||||
You can customize it as you like, and you can use the following variables in the template:
|
||||
- \`\${FOAM_DATE_YEAR}\`: The year of the date
|
||||
- \`\${FOAM_DATE_MONTH}\`: The month of the date
|
||||
- \`\${FOAM_DATE_DATE}\`: The day of the date
|
||||
- \`\${FOAM_TITLE}\`: The title of the note
|
||||
|
||||
Go to https://github.com/foambubble/foam/blob/main/docs/user/features/daily-notes.md for more details.
|
||||
For more complex templates, including Javascript dynamic templates, see https://github.com/foambubble/foam/blob/main/docs/user/features/note-templates.md.
|
||||
`;
|
||||
|
||||
export const CREATE_DAILY_NOTE_WARNING_RESPONSE = 'Create daily note template';
|
||||
|
||||
/**
|
||||
* Create a daily note if it does not exist.
|
||||
* Create a daily note using the unified creation engine (supports JS templates)
|
||||
*
|
||||
* In the case that the folders referenced in the file path also do not exist,
|
||||
* this function will create all folders in the path.
|
||||
*
|
||||
* @param currentDate The current date, to be used as a title.
|
||||
* @returns Whether the file was created.
|
||||
* @param targetDate The target date
|
||||
* @param foam The Foam instance
|
||||
* @returns Whether the file was created and the URI
|
||||
*/
|
||||
export async function createDailyNoteIfNotExists(targetDate: Date) {
|
||||
const uriFromLegacyConfiguration = getDailyNoteUri(targetDate);
|
||||
const pathFromLegacyConfiguration = uriFromLegacyConfiguration.toFsPath();
|
||||
export async function createDailyNoteIfNotExists(targetDate: Date, foam: Foam) {
|
||||
const templatePath = await getDailyNoteTemplateUri();
|
||||
|
||||
if (!templatePath) {
|
||||
window
|
||||
.showWarningMessage(
|
||||
'No daily note template found. Using legacy configuration (deprecated). Create a daily note template to avoid this warning and customize your daily note.',
|
||||
CREATE_DAILY_NOTE_WARNING_RESPONSE
|
||||
)
|
||||
.then(async action => {
|
||||
if (action === CREATE_DAILY_NOTE_WARNING_RESPONSE) {
|
||||
const newTemplateUri = Uri.joinPath(
|
||||
workspace.workspaceFolders[0].uri,
|
||||
'.foam',
|
||||
'templates',
|
||||
'daily-note.md'
|
||||
);
|
||||
await workspace.fs.writeFile(
|
||||
newTemplateUri,
|
||||
new TextEncoder().encode(DEFAULT_DAILY_NOTE_TEMPLATE)
|
||||
);
|
||||
await showInEditor(fromVsCodeUri(newTemplateUri));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set up variables for template processing
|
||||
const formattedDate = dateFormat(targetDate, 'yyyy-mm-dd', false);
|
||||
const variables = {
|
||||
FOAM_TITLE: formattedDate,
|
||||
title: formattedDate,
|
||||
};
|
||||
|
||||
const dailyNoteUri = getDailyNoteUri(targetDate);
|
||||
const titleFormat: string =
|
||||
getFoamVsCodeConfig('openDailyNote.titleFormat') ??
|
||||
getFoamVsCodeConfig('openDailyNote.filenameFormat');
|
||||
getFoamVsCodeConfig('openDailyNote.filenameFormat') ??
|
||||
'isoDate';
|
||||
|
||||
const templateFallbackText = `# ${dateFormat(
|
||||
targetDate,
|
||||
@@ -85,9 +147,16 @@ export async function createDailyNoteIfNotExists(targetDate: Date) {
|
||||
false
|
||||
)}\n`;
|
||||
|
||||
return await NoteFactory.createFromDailyNoteTemplate(
|
||||
uriFromLegacyConfiguration,
|
||||
templateFallbackText,
|
||||
targetDate
|
||||
return await createNote(
|
||||
{
|
||||
notePath: dailyNoteUri.toFsPath(),
|
||||
templatePath: templatePath,
|
||||
text: templateFallbackText,
|
||||
date: targetDate,
|
||||
variables: variables,
|
||||
onFileExists: 'open',
|
||||
onRelativeNotePath: 'resolve-from-root',
|
||||
},
|
||||
foam
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { workspace, ExtensionContext, window, commands } from 'vscode';
|
||||
import { MarkdownResourceProvider } from './core/services/markdown-provider';
|
||||
import { bootstrap } from './core/model/foam';
|
||||
import { Logger } from './core/utils/log';
|
||||
import { fromVsCodeUri } from './utils/vsc-utils';
|
||||
|
||||
import { features } from './features';
|
||||
import { VsCodeOutputLogger, exposeLogger } from './services/logging';
|
||||
@@ -51,10 +52,16 @@ export async function activate(context: ExtensionContext) {
|
||||
|
||||
const { notesExtensions, defaultExtension } = getNotesExtensions();
|
||||
|
||||
// Get workspace roots for workspace-relative path resolution
|
||||
const workspaceRoots =
|
||||
workspace.workspaceFolders?.map(folder => fromVsCodeUri(folder.uri)) ??
|
||||
[];
|
||||
|
||||
const markdownProvider = new MarkdownResourceProvider(
|
||||
dataStore,
|
||||
parser,
|
||||
notesExtensions
|
||||
notesExtensions,
|
||||
workspaceRoots
|
||||
);
|
||||
|
||||
const attachmentExtConfig = getAttachmentsExtensions();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import { env, Position, Selection, commands } from 'vscode';
|
||||
import { createFile, showInEditor } from '../../test/test-utils-vscode';
|
||||
import { removeBrackets, toTitleCase } from './copy-without-brackets';
|
||||
@@ -9,7 +10,7 @@ describe('copy-without-brackets command', () => {
|
||||
'file.md',
|
||||
]);
|
||||
const { editor } = await showInEditor(uri);
|
||||
editor.selection = new Selection(new Position(0, 0), new Position(1, 0));
|
||||
editor.selection = new Selection(0, 0, 1, 0);
|
||||
await commands.executeCommand('foam-vscode.copy-without-brackets');
|
||||
const value = await env.clipboard.readText();
|
||||
expect(value).toEqual('This is my Test Content.');
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import { commands, window, workspace } from 'vscode';
|
||||
import { toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { createFile } from '../../test/test-utils-vscode';
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
import { commands, ExtensionContext } from 'vscode';
|
||||
import { askUserForTemplate, NoteFactory } from '../../services/templates';
|
||||
import { Resolver } from '../../services/variable-resolver';
|
||||
import { askUserForTemplate } from '../../services/templates';
|
||||
|
||||
export default async function activate(context: ExtensionContext) {
|
||||
context.subscriptions.push(
|
||||
commands.registerCommand(
|
||||
'foam-vscode.create-note-from-template',
|
||||
async () => {
|
||||
const templateUri = await askUserForTemplate();
|
||||
|
||||
if (templateUri) {
|
||||
const resolver = new Resolver(new Map(), new Date());
|
||||
|
||||
await NoteFactory.createFromTemplate(templateUri, resolver);
|
||||
}
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
askForTemplate: true,
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import { commands, window, workspace } from 'vscode';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { asAbsoluteWorkspaceUri, readFile } from '../../services/editor';
|
||||
@@ -41,7 +42,7 @@ describe('create-note command', () => {
|
||||
]);
|
||||
const target = getUriInWorkspace();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.path,
|
||||
notePath: target,
|
||||
templatePath: templateA.uri.path,
|
||||
text: 'hello',
|
||||
});
|
||||
@@ -54,7 +55,7 @@ describe('create-note command', () => {
|
||||
it('focuses on the newly created note', async () => {
|
||||
const target = getUriInWorkspace();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.path,
|
||||
notePath: target,
|
||||
text: 'hello',
|
||||
});
|
||||
expect(window.activeTextEditor.document.getText()).toEqual('hello');
|
||||
@@ -65,7 +66,7 @@ describe('create-note command', () => {
|
||||
it('supports variables', async () => {
|
||||
const target = getUriInWorkspace();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.path,
|
||||
notePath: target,
|
||||
text: 'hello ${FOAM_TITLE}', // eslint-disable-line no-template-curly-in-string
|
||||
variables: { FOAM_TITLE: 'world' },
|
||||
});
|
||||
@@ -77,7 +78,7 @@ describe('create-note command', () => {
|
||||
it('supports date variables', async () => {
|
||||
const target = getUriInWorkspace();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.path,
|
||||
notePath: target,
|
||||
text: 'hello ${FOAM_DATE_YEAR}', // eslint-disable-line no-template-curly-in-string
|
||||
date: '2021-10-01',
|
||||
});
|
||||
@@ -92,7 +93,7 @@ describe('create-note command', () => {
|
||||
expect(content).toEqual('hello');
|
||||
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.uri.path,
|
||||
notePath: target.uri,
|
||||
text: 'test overwrite',
|
||||
onFileExists: 'overwrite',
|
||||
});
|
||||
@@ -103,7 +104,7 @@ describe('create-note command', () => {
|
||||
|
||||
await closeEditors();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.uri.path,
|
||||
notePath: target.uri,
|
||||
text: 'test open',
|
||||
onFileExists: 'open',
|
||||
});
|
||||
@@ -114,7 +115,7 @@ describe('create-note command', () => {
|
||||
|
||||
await closeEditors();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.uri.path,
|
||||
notePath: target.uri,
|
||||
text: 'test cancel',
|
||||
onFileExists: 'cancel',
|
||||
});
|
||||
@@ -125,7 +126,7 @@ describe('create-note command', () => {
|
||||
.mockImplementationOnce(jest.fn(() => Promise.resolve(undefined)));
|
||||
await closeEditors();
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: target.uri.path,
|
||||
notePath: target.uri,
|
||||
text: 'test ask',
|
||||
onFileExists: 'ask',
|
||||
});
|
||||
@@ -201,6 +202,54 @@ describe('create-note command', () => {
|
||||
|
||||
// await deleteFile(base);
|
||||
});
|
||||
|
||||
it('throws an error if the template file does not exist', async () => {
|
||||
const nonExistentTemplatePath = '/non/existent/template/path.md';
|
||||
await expect(
|
||||
commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: 'note-with-missing-template.md',
|
||||
templatePath: nonExistentTemplatePath,
|
||||
text: 'should not matter',
|
||||
})
|
||||
).rejects.toThrow(
|
||||
`Failed to load template (file://${nonExistentTemplatePath}): Template file not found: file://${nonExistentTemplatePath}`
|
||||
);
|
||||
});
|
||||
|
||||
it('throws an error if the template file does not exist (relative path)', async () => {
|
||||
try {
|
||||
const nonExistentTemplatePath = 'relative/non-existent-template.md';
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: 'note-with-missing-template-relative.md',
|
||||
templatePath: nonExistentTemplatePath,
|
||||
text: 'should not matter',
|
||||
});
|
||||
throw new Error('Expected an error to be thrown');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain(`Failed to load template`); // eslint-disable-line jest/no-conditional-expect
|
||||
}
|
||||
});
|
||||
|
||||
it('creates a note with absolute path within the workspace', async () => {
|
||||
await commands.executeCommand('foam-vscode.create-note', {
|
||||
notePath: '/note-in-workspace.md',
|
||||
text: 'hello workspace',
|
||||
});
|
||||
expect(window.activeTextEditor.document.getText()).toEqual(
|
||||
'hello workspace'
|
||||
);
|
||||
expectSameUri(
|
||||
window.activeTextEditor.document.uri,
|
||||
fromVsCodeUri(workspace.workspaceFolders?.[0].uri).joinPath(
|
||||
'note-in-workspace.md'
|
||||
)
|
||||
);
|
||||
await deleteFile(
|
||||
fromVsCodeUri(workspace.workspaceFolders?.[0].uri).joinPath(
|
||||
'note-in-workspace.md'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('factories', () => {
|
||||
@@ -251,7 +300,7 @@ foam_template:
|
||||
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();
|
||||
expect(results.uri.path).toMatch(/hello-world.md$/);
|
||||
|
||||
const newNoteDoc = window.activeTextEditor.document;
|
||||
expect(newNoteDoc.uri.path).toMatch(/hello-world.md$/);
|
||||
@@ -260,4 +309,64 @@ foam_template:
|
||||
expect(doc.getText()).toEqual(`this is my [[hello-world]]`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Template filepath with FOAM_CURRENT_DIR', () => {
|
||||
it('should create note in current directory using FOAM_CURRENT_DIR variable', async () => {
|
||||
// Create a test subdirectory and a file in it
|
||||
const noteInSubdir = await createFile('Test content', [
|
||||
'subdir',
|
||||
'existing-note.md',
|
||||
]);
|
||||
|
||||
// Create a template with FOAM_CURRENT_DIR variable
|
||||
const template = await createFile(
|
||||
`---
|
||||
foam_template:
|
||||
filepath: \${FOAM_CURRENT_DIR}/\${FOAM_SLUG}.md
|
||||
---
|
||||
# \${FOAM_TITLE}
|
||||
|
||||
Template content using FOAM_CURRENT_DIR`,
|
||||
['.foam', 'templates', 'foam-current-dir-template.md']
|
||||
);
|
||||
|
||||
// Switch to the file in the subdirectory to set current editor context
|
||||
await showInEditor(noteInSubdir.uri);
|
||||
|
||||
// Create a note using the template - FOAM_CURRENT_DIR should resolve to current editor directory
|
||||
const resultInSubdir = await createNote(
|
||||
{
|
||||
templatePath: template.uri.path,
|
||||
title: 'My New Note',
|
||||
},
|
||||
{} as any
|
||||
);
|
||||
// The note should be created in the subdir because FOAM_CURRENT_DIR resolves to current editor directory
|
||||
expect(resultInSubdir.uri).toEqual(
|
||||
noteInSubdir.uri.getDirectory().joinPath('my-new-note.md')
|
||||
);
|
||||
|
||||
await closeEditors();
|
||||
// Create a note using the template - FOAM_CURRENT_DIR should resolve to current editor directory
|
||||
const resultInRoot = await createNote(
|
||||
{
|
||||
templatePath: template.uri.path,
|
||||
title: 'My New Note',
|
||||
},
|
||||
{} as any
|
||||
);
|
||||
// The note should be created in the workspace root because FOAM_CURRENT_DIR resolves to workspace root when no editor is active
|
||||
expect(resultInRoot.uri).toEqual(
|
||||
fromVsCodeUri(workspace.workspaceFolders[0].uri).joinPath(
|
||||
'my-new-note.md'
|
||||
)
|
||||
);
|
||||
|
||||
// Clean up
|
||||
await deleteFile(template.uri);
|
||||
await deleteFile(noteInSubdir.uri);
|
||||
await deleteFile(resultInRoot.uri);
|
||||
await deleteFile(resultInSubdir.uri);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,28 +1,35 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { workspace, commands, WorkspaceEdit, ExtensionContext } from 'vscode';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import {
|
||||
askUserForTemplate,
|
||||
getDefaultTemplateUri,
|
||||
getPathFromTitle,
|
||||
NoteFactory,
|
||||
} from '../../services/templates';
|
||||
import { NoteCreationEngine } from '../../services/note-creation-engine';
|
||||
import { TriggerFactory } from '../../services/note-creation-triggers';
|
||||
import { TemplateLoader } from '../../services/template-loader';
|
||||
import { Template } from '../../services/note-creation-types';
|
||||
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';
|
||||
import {
|
||||
fromVsCodeUri,
|
||||
toVsCodeRange,
|
||||
toVsCodeUri,
|
||||
} from '../../utils/vsc-utils';
|
||||
import { Logger } from '../../core/utils/log';
|
||||
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
context: ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
const foam = await foamPromise;
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand(CREATE_NOTE_COMMAND.command, args =>
|
||||
commands.registerCommand(CREATE_NOTE_COMMAND.command, args =>
|
||||
createNote(args, foam)
|
||||
)
|
||||
);
|
||||
@@ -33,11 +40,11 @@ interface CreateNoteArgs {
|
||||
* The path of the note to create.
|
||||
* If relative it will be resolved against the workspace root.
|
||||
*/
|
||||
notePath?: string;
|
||||
notePath?: string | URI;
|
||||
/**
|
||||
* The path of the template to use.
|
||||
*/
|
||||
templatePath?: string;
|
||||
templatePath?: string | URI;
|
||||
/**
|
||||
* Whether to ask the user to select a template for the new note. If so, overwrites templatePath.
|
||||
*/
|
||||
@@ -54,7 +61,7 @@ interface CreateNoteArgs {
|
||||
/**
|
||||
* The date used to resolve the FOAM_DATE_* variables. in YYYY-MM-DD format
|
||||
*/
|
||||
date?: string;
|
||||
date?: string | Date;
|
||||
/**
|
||||
* The title of the note (translates into the FOAM_TITLE variable)
|
||||
*/
|
||||
@@ -82,24 +89,39 @@ const DEFAULT_NEW_NOTE_TEXT = `# \${FOAM_TITLE}
|
||||
|
||||
\${FOAM_SELECTED_TEXT}`;
|
||||
|
||||
/**
|
||||
* Related to #1505.
|
||||
* This function forces the date to be local by removing any time information and
|
||||
* adding a local time (noon) to it.
|
||||
* @param dateString The date string, either in YYYY-MM-DD format or any format parsable by Date()
|
||||
* @returns The parsed Date object
|
||||
*/
|
||||
function forceLocalDate(dateString: string): Date {
|
||||
// Remove the time part if present
|
||||
const dateOnly = dateString.split('T')[0];
|
||||
// Otherwise, treat as local date by adding local noon time
|
||||
return new Date(dateOnly + 'T12:00:00');
|
||||
}
|
||||
|
||||
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(
|
||||
new Map(Object.entries(args.variables ?? {})),
|
||||
date
|
||||
);
|
||||
if (args.title) {
|
||||
resolver.define('FOAM_TITLE', args.title);
|
||||
}
|
||||
const text = args.text ?? DEFAULT_NEW_NOTE_TEXT;
|
||||
const schemaSource = vscode.workspace.workspaceFolders[0].uri;
|
||||
const noteUri =
|
||||
args.notePath &&
|
||||
new URI({
|
||||
scheme: schemaSource.scheme,
|
||||
path: args.notePath,
|
||||
});
|
||||
const foamDate =
|
||||
typeof args.date === 'string'
|
||||
? forceLocalDate(args.date)
|
||||
: args.date instanceof Date
|
||||
? args.date
|
||||
: new Date();
|
||||
|
||||
// Create appropriate trigger based on context
|
||||
const trigger = args.sourceLink
|
||||
? TriggerFactory.createPlaceholderTrigger(
|
||||
args.sourceLink.uri,
|
||||
foam.workspace.find(new URI(args.sourceLink.uri))?.title || 'Unknown',
|
||||
args.sourceLink
|
||||
)
|
||||
: TriggerFactory.createCommandTrigger('foam-vscode.create-note');
|
||||
|
||||
// Determine template path
|
||||
let templateUri: URI;
|
||||
if (args.askForTemplate) {
|
||||
const selectedTemplate = await askUserForTemplate();
|
||||
@@ -111,41 +133,91 @@ export async function createNote(args: CreateNoteArgs, foam: Foam) {
|
||||
} else {
|
||||
templateUri = args.templatePath
|
||||
? asAbsoluteWorkspaceUri(args.templatePath)
|
||||
: getDefaultTemplateUri();
|
||||
: await getDefaultTemplateUri();
|
||||
}
|
||||
|
||||
const createdNote = (await fileExists(templateUri))
|
||||
? await NoteFactory.createFromTemplate(
|
||||
templateUri,
|
||||
resolver,
|
||||
noteUri,
|
||||
text,
|
||||
args.onFileExists
|
||||
)
|
||||
: await NoteFactory.createNote(
|
||||
noteUri ?? (await getPathFromTitle(templateUri.scheme, resolver)),
|
||||
text,
|
||||
resolver,
|
||||
args.onFileExists,
|
||||
args.onRelativeNotePath
|
||||
);
|
||||
// Load template using the new system
|
||||
const templateLoader = new TemplateLoader();
|
||||
let template: Template;
|
||||
|
||||
if (args.sourceLink) {
|
||||
try {
|
||||
if (!templateUri) {
|
||||
template = {
|
||||
type: 'markdown',
|
||||
metadata: new Map(),
|
||||
content: args.text || DEFAULT_NEW_NOTE_TEXT,
|
||||
};
|
||||
} else if (await fileExists(templateUri)) {
|
||||
template = await templateLoader.loadTemplate(templateUri);
|
||||
} else {
|
||||
throw new Error(`Template file not found: ${templateUri}`);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to load template (${templateUri}): ${error.message}`
|
||||
);
|
||||
}
|
||||
|
||||
// If notePath is provided, add it to template metadata to avoid unnecessary title resolution
|
||||
if (args.notePath && template.type === 'markdown') {
|
||||
template.metadata.set(
|
||||
'filepath',
|
||||
typeof args.notePath === 'string'
|
||||
? args.notePath
|
||||
: args.notePath.toFsPath()
|
||||
);
|
||||
}
|
||||
|
||||
// Create resolver with all variables upfront
|
||||
const resolver = new Resolver(
|
||||
new Map(Object.entries(args.variables ?? {})),
|
||||
foamDate,
|
||||
args.title
|
||||
);
|
||||
|
||||
if (Logger.getLevel() === 'debug') {
|
||||
Logger.debug(`[createNote] args: ${JSON.stringify(args, null, 2)}`);
|
||||
Logger.debug(`[createNote] template: ${JSON.stringify(template, null, 2)}`);
|
||||
Logger.debug(`[createNote] resolver: ${JSON.stringify(resolver, null, 2)}`);
|
||||
Logger.debug(
|
||||
`[createNote] foamDate: ${foamDate.toISOString()} (timezone offset: ${foamDate.getTimezoneOffset()})`
|
||||
);
|
||||
}
|
||||
|
||||
// Process template using the new engine with unified resolver
|
||||
const engine = new NoteCreationEngine(
|
||||
foam,
|
||||
workspace.workspaceFolders.map(folder => fromVsCodeUri(folder.uri))
|
||||
);
|
||||
const result = await engine.processTemplate(trigger, template, resolver);
|
||||
|
||||
// Create the note using NoteFactory with the same resolver
|
||||
const createdNote = await NoteFactory.createNote(
|
||||
result.filepath,
|
||||
result.content,
|
||||
resolver,
|
||||
args.onFileExists,
|
||||
args.onRelativeNotePath
|
||||
);
|
||||
|
||||
// Handle source link updates for placeholders
|
||||
if (args.sourceLink && createdNote.uri) {
|
||||
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 updateLink = new WorkspaceEdit();
|
||||
const uri = toVsCodeUri(args.sourceLink.uri);
|
||||
updateLink.replace(
|
||||
uri,
|
||||
toVsCodeRange(args.sourceLink.range),
|
||||
edit.newText
|
||||
);
|
||||
await vscode.workspace.applyEdit(updateLink);
|
||||
await workspace.applyEdit(updateLink);
|
||||
}
|
||||
}
|
||||
|
||||
return createdNote;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,3 +11,5 @@ export { default as updateGraphCommand } from './update-graph';
|
||||
export { default as updateWikilinksCommand } from './update-wikilinks';
|
||||
export { default as createNote } from './create-note';
|
||||
export { default as generateStandaloneNote } from './convert-links-format-in-note';
|
||||
export { default as searchTagCommand } from './search-tag';
|
||||
export { default as renameTagCommand } from './rename-tag';
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
/* @unit-ready */
|
||||
import dateFormat from 'dateformat';
|
||||
import { commands, window } from 'vscode';
|
||||
|
||||
describe('open-daily-note-for-date command', () => {
|
||||
it('offers to pick which template to use', async () => {
|
||||
it('offers to pick which date to use', async () => {
|
||||
const spy = jest
|
||||
.spyOn(window, 'showQuickPick')
|
||||
.mockImplementationOnce(jest.fn(() => Promise.resolve(undefined)));
|
||||
|
||||
@@ -23,7 +23,7 @@ export default async function activate(
|
||||
.then(item => {
|
||||
return item?.date;
|
||||
});
|
||||
return openDailyNoteFor(date);
|
||||
return openDailyNoteFor(date, await foamPromise);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { ExtensionContext, commands } from 'vscode';
|
||||
import { getFoamVsCodeConfig } from '../../services/config';
|
||||
import { openDailyNoteFor } from '../../dated-notes';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
|
||||
export default async function activate(context: ExtensionContext) {
|
||||
export default async function activate(
|
||||
context: ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
context.subscriptions.push(
|
||||
commands.registerCommand('foam-vscode.open-daily-note', () =>
|
||||
openDailyNoteFor(new Date())
|
||||
commands.registerCommand('foam-vscode.open-daily-note', async () =>
|
||||
openDailyNoteFor(new Date(), await foamPromise)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
import { ExtensionContext, commands } from 'vscode';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import { getFoamVsCodeConfig } from '../../services/config';
|
||||
import {
|
||||
createDailyNoteIfNotExists,
|
||||
openDailyNoteFor,
|
||||
} from '../../dated-notes';
|
||||
|
||||
export default async function activate(context: ExtensionContext) {
|
||||
export default async function activate(
|
||||
context: ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
context.subscriptions.push(
|
||||
commands.registerCommand('foam-vscode.open-dated-note', date => {
|
||||
commands.registerCommand('foam-vscode.open-dated-note', async date => {
|
||||
const foam = await foamPromise;
|
||||
switch (getFoamVsCodeConfig('dateSnippets.afterCompletion')) {
|
||||
case 'navigateToNote':
|
||||
return openDailyNoteFor(date);
|
||||
return openDailyNoteFor(date, foam);
|
||||
case 'createNote':
|
||||
return createDailyNoteIfNotExists(date);
|
||||
return createDailyNoteIfNotExists(date, foam);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
@@ -3,19 +3,28 @@ import { CommandDescriptor } from '../../utils/commands';
|
||||
import { OpenResourceArgs, OPEN_COMMAND } from './open-resource';
|
||||
import * as filter from '../../core/services/resource-filter';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { closeEditors, createFile } from '../../test/test-utils-vscode';
|
||||
import {
|
||||
closeEditors,
|
||||
createFile,
|
||||
waitForNoteInFoamWorkspace,
|
||||
} from '../../test/test-utils-vscode';
|
||||
import { deleteFile } from '../../services/editor';
|
||||
import waitForExpect from 'wait-for-expect';
|
||||
|
||||
describe('open-resource command', () => {
|
||||
beforeEach(async () => {
|
||||
await jest.resetAllMocks();
|
||||
jest.resetAllMocks();
|
||||
await closeEditors();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await closeEditors();
|
||||
});
|
||||
|
||||
it('URI param has precedence over filter', async () => {
|
||||
const spy = jest.spyOn(filter, 'createFilter');
|
||||
const noteA = await createFile('Note A for open command');
|
||||
await waitForNoteInFoamWorkspace(noteA.uri);
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
@@ -26,7 +35,8 @@ describe('open-resource command', () => {
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
await waitForExpect(() => {
|
||||
expect(window.activeTextEditor).toBeTruthy();
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
@@ -36,15 +46,17 @@ describe('open-resource command', () => {
|
||||
|
||||
it('URI param accept URI object, or path', async () => {
|
||||
const noteA = await createFile('Note A for open command');
|
||||
await waitForNoteInFoamWorkspace(noteA.uri);
|
||||
|
||||
const uriCommand: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: URI.file('path/to/file.md'),
|
||||
uri: noteA.uri,
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(uriCommand.name, uriCommand.params);
|
||||
waitForExpect(() => {
|
||||
await waitForExpect(() => {
|
||||
expect(window.activeTextEditor).toBeTruthy();
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
|
||||
@@ -53,17 +65,18 @@ describe('open-resource command', () => {
|
||||
const pathCommand: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: URI.file('path/to/file.md'),
|
||||
uri: noteA.uri.path,
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(pathCommand.name, pathCommand.params);
|
||||
waitForExpect(() => {
|
||||
await waitForExpect(() => {
|
||||
expect(window.activeTextEditor).toBeTruthy();
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
await deleteFile(noteA.uri);
|
||||
});
|
||||
|
||||
it('User is notified if no resource is found', async () => {
|
||||
it('User is notified if no resource is found with filter', async () => {
|
||||
const spy = jest.spyOn(window, 'showInformationMessage');
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
@@ -74,12 +87,33 @@ describe('open-resource command', () => {
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
await waitForExpect(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('User is notified if no resource is found with URI', async () => {
|
||||
const spy = jest.spyOn(window, 'showInformationMessage');
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: URI.file('path/to/nonexistent.md'),
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('filter with multiple results will show a quick pick', async () => {
|
||||
const noteA = await createFile('Note A for filter test');
|
||||
const noteB = await createFile('Note B for filter test');
|
||||
await waitForNoteInFoamWorkspace(noteA.uri);
|
||||
await waitForNoteInFoamWorkspace(noteB.uri);
|
||||
|
||||
const spy = jest
|
||||
.spyOn(window, 'showQuickPick')
|
||||
.mockImplementationOnce(jest.fn(() => Promise.resolve(undefined)));
|
||||
@@ -92,8 +126,11 @@ describe('open-resource command', () => {
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
await waitForExpect(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
await deleteFile(noteA.uri);
|
||||
await deleteFile(noteB.uri);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -82,13 +82,18 @@ async function openResource(workspace: FoamWorkspace, args?: OpenResourceArgs) {
|
||||
);
|
||||
}
|
||||
|
||||
if (isSome(item)) {
|
||||
const targetUri =
|
||||
item.uri.path === vscode.window.activeTextEditor?.document.uri.path
|
||||
? vscode.window.activeTextEditor?.document.uri
|
||||
: toVsCodeUri(item.uri.asPlain());
|
||||
return vscode.commands.executeCommand('vscode.open', targetUri);
|
||||
if (isNone(item)) {
|
||||
vscode.window.showInformationMessage(
|
||||
'Foam: No note matches given filters or URI.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const targetUri =
|
||||
item.uri.path === vscode.window.activeTextEditor?.document.uri.path
|
||||
? vscode.window.activeTextEditor?.document.uri
|
||||
: toVsCodeUri(item.uri.asPlain());
|
||||
return vscode.commands.executeCommand('vscode.open', targetUri);
|
||||
}
|
||||
|
||||
interface ResourceItem extends vscode.QuickPickItem {
|
||||
|
||||
324
packages/foam-vscode/src/features/commands/rename-tag.ts
Normal file
324
packages/foam-vscode/src/features/commands/rename-tag.ts
Normal file
@@ -0,0 +1,324 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import { TagEdit } from '../../core/services/tag-edit';
|
||||
import { TagItem } from '../panels/tags-explorer';
|
||||
import { fromVsCodeUri, toVsCodeWorkspaceEdit } from '../../utils/vsc-utils';
|
||||
import { Logger } from '../../core/utils/log';
|
||||
import { Position } from '../../core/model/position';
|
||||
|
||||
/**
|
||||
* Command definition for the tag rename functionality.
|
||||
*
|
||||
* This command provides workspace-wide tag renaming capabilities with multiple
|
||||
* invocation methods: command palette, context menus, and programmatic calls.
|
||||
*/
|
||||
export const RENAME_TAG_COMMAND = {
|
||||
/** VS Code command identifier */
|
||||
command: 'foam-vscode.rename-tag',
|
||||
/** Display name shown in command palette */
|
||||
title: 'Foam: Rename Tag',
|
||||
};
|
||||
|
||||
/**
|
||||
* Activates the rename tag command feature.
|
||||
*
|
||||
* Registers the rename tag command with VS Code and sets up error handling.
|
||||
* The command supports multiple parameter combinations for different use cases.
|
||||
*
|
||||
* @param context VS Code extension context for registering disposables
|
||||
* @param foamPromise Promise that resolves to the initialized Foam instance
|
||||
*/
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
const foam = await foamPromise;
|
||||
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand(
|
||||
RENAME_TAG_COMMAND.command,
|
||||
async (tagLabelOrItem?: string | TagItem, newTagName?: string) => {
|
||||
try {
|
||||
await executeRenameTag(foam, tagLabelOrItem, newTagName);
|
||||
} catch (error) {
|
||||
Logger.error('Error executing rename tag command:', error);
|
||||
vscode.window.showErrorMessage(
|
||||
`Failed to rename tag: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the tag rename operation with flexible parameter handling.
|
||||
*
|
||||
* This function handles the complete tag rename workflow:
|
||||
* 1. Determine which tag to rename (from parameters, cursor position, or user selection)
|
||||
* 2. Get the new tag name (from parameters or user input)
|
||||
* 3. Validate the rename operation
|
||||
* 4. Apply the changes across the workspace
|
||||
*
|
||||
* @param foam The Foam instance containing workspace and tag information
|
||||
* @param tagLabelOrItem Optional tag to rename (string label or TagItem from explorer)
|
||||
* @param newTagName Optional new name for the tag
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Rename specific tag programmatically
|
||||
* await executeRenameTag(foam, 'oldtag', 'newtag');
|
||||
*
|
||||
* // Interactive rename with tag picker
|
||||
* await executeRenameTag(foam);
|
||||
*
|
||||
* // Rename tag from Tags Explorer context
|
||||
* await executeRenameTag(foam, tagItem);
|
||||
* ```
|
||||
*/
|
||||
async function executeRenameTag(
|
||||
foam: Foam,
|
||||
tagLabelOrItem?: string | TagItem,
|
||||
newTagName?: string
|
||||
): Promise<void> {
|
||||
let tagLabel: string | undefined;
|
||||
|
||||
// Determine the tag to rename
|
||||
if (typeof tagLabelOrItem === 'string') {
|
||||
tagLabel = tagLabelOrItem;
|
||||
} else if (
|
||||
tagLabelOrItem &&
|
||||
typeof tagLabelOrItem === 'object' &&
|
||||
'tag' in tagLabelOrItem
|
||||
) {
|
||||
tagLabel = tagLabelOrItem.tag;
|
||||
} else {
|
||||
// Try to detect tag from current cursor position
|
||||
const activeEditor = vscode.window.activeTextEditor;
|
||||
if (activeEditor && activeEditor.document.languageId === 'markdown') {
|
||||
const vsPosition = activeEditor.selection.active;
|
||||
const fileUri = fromVsCodeUri(activeEditor.document.uri);
|
||||
const position = Position.create(vsPosition.line, vsPosition.character);
|
||||
|
||||
tagLabel = TagEdit.getTagAtPosition(foam.tags, fileUri, position);
|
||||
}
|
||||
}
|
||||
|
||||
// If we still don't have a tag, show picker
|
||||
if (!tagLabel) {
|
||||
const allTags = Array.from(foam.tags.tags.keys()).sort();
|
||||
|
||||
if (allTags.length === 0) {
|
||||
vscode.window.showInformationMessage('No tags found in workspace.');
|
||||
return;
|
||||
}
|
||||
|
||||
tagLabel = await vscode.window.showQuickPick(allTags, {
|
||||
title: 'Select a tag to rename',
|
||||
placeHolder: 'Choose a tag to rename...',
|
||||
});
|
||||
|
||||
if (!tagLabel) {
|
||||
return; // User cancelled
|
||||
}
|
||||
}
|
||||
|
||||
// Get the new tag name from user or use provided parameter
|
||||
let finalNewTagName = newTagName;
|
||||
|
||||
// If newTagName was provided, validate it first
|
||||
if (finalNewTagName) {
|
||||
const cleanValue = finalNewTagName.startsWith('#')
|
||||
? finalNewTagName.substring(1)
|
||||
: finalNewTagName;
|
||||
|
||||
const validation = TagEdit.validateTagRename(
|
||||
foam.tags,
|
||||
tagLabel!,
|
||||
cleanValue
|
||||
);
|
||||
if (!validation.isValid) {
|
||||
throw new Error(validation.message);
|
||||
}
|
||||
|
||||
// Handle merge confirmation if needed
|
||||
if (validation.isMerge) {
|
||||
const confirmed = await vscode.window.showWarningMessage(
|
||||
`Tag "${cleanValue}" already exists (${
|
||||
validation.targetOccurrences
|
||||
} occurrence${
|
||||
validation.targetOccurrences !== 1 ? 's' : ''
|
||||
}). Merge "${tagLabel}" (${validation.sourceOccurrences} occurrence${
|
||||
validation.sourceOccurrences !== 1 ? 's' : ''
|
||||
}) into it?`,
|
||||
{ modal: true },
|
||||
'Merge Tags'
|
||||
);
|
||||
|
||||
if (confirmed !== 'Merge Tags') {
|
||||
throw new Error('Tag merge cancelled by user');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!finalNewTagName) {
|
||||
const currentOccurrences = foam.tags.tags.get(tagLabel)?.length ?? 0;
|
||||
finalNewTagName = await vscode.window.showInputBox({
|
||||
title: `Rename tag "${tagLabel}"`,
|
||||
prompt: `Enter new name for tag "${tagLabel}" (${currentOccurrences} occurrence${
|
||||
currentOccurrences !== 1 ? 's' : ''
|
||||
})`,
|
||||
value: tagLabel,
|
||||
validateInput: (value: string) => {
|
||||
const validation = TagEdit.validateTagRename(
|
||||
foam.tags,
|
||||
tagLabel!,
|
||||
value
|
||||
);
|
||||
|
||||
if (!validation.isValid) {
|
||||
return validation.message;
|
||||
}
|
||||
|
||||
// Show merge information but allow the input
|
||||
if (validation.isMerge) {
|
||||
return {
|
||||
message: `Will merge into existing tag: ${value} - ${
|
||||
validation.targetOccurrences
|
||||
} occurrence${validation.targetOccurrences !== 1 ? 's' : ''}`,
|
||||
severity: vscode.InputBoxValidationSeverity.Info,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
if (!finalNewTagName) {
|
||||
return; // User cancelled
|
||||
}
|
||||
}
|
||||
|
||||
// Clean the new name
|
||||
const cleanNewName = finalNewTagName.startsWith('#')
|
||||
? finalNewTagName.substring(1)
|
||||
: finalNewTagName;
|
||||
|
||||
// Final validation and merge confirmation for input box flow
|
||||
const finalValidation = TagEdit.validateTagRename(
|
||||
foam.tags,
|
||||
tagLabel,
|
||||
cleanNewName
|
||||
);
|
||||
|
||||
if (!finalValidation.isValid) {
|
||||
throw new Error(finalValidation.message);
|
||||
}
|
||||
|
||||
// Check for child tags and offer hierarchical rename
|
||||
const childTags = TagEdit.findChildTags(foam.tags, tagLabel);
|
||||
const hasChildren = childTags.length > 0;
|
||||
let useHierarchicalRename = false;
|
||||
|
||||
if (hasChildren) {
|
||||
const childList = childTags.map(tag => `• ${tag}`).join('\n');
|
||||
const choice = await vscode.window.showWarningMessage(
|
||||
`Tag "${tagLabel}" has ${childTags.length} child tag${
|
||||
childTags.length !== 1 ? 's' : ''
|
||||
}:\n\n${childList}\n\nHow would you like to proceed?`,
|
||||
{ modal: true },
|
||||
'Rename Only Parent',
|
||||
'Rename All (Parent + Children)'
|
||||
);
|
||||
|
||||
if (choice === 'Rename All (Parent + Children)') {
|
||||
useHierarchicalRename = true;
|
||||
} else if (choice !== 'Rename Only Parent') {
|
||||
return; // User cancelled
|
||||
}
|
||||
}
|
||||
|
||||
// Handle merge confirmation if needed (for input box flow)
|
||||
if (finalValidation.isMerge) {
|
||||
const confirmed = await vscode.window.showWarningMessage(
|
||||
`Tag "${cleanNewName}" already exists (${
|
||||
finalValidation.targetOccurrences
|
||||
} occurrence${
|
||||
finalValidation.targetOccurrences !== 1 ? 's' : ''
|
||||
}). Merge "${tagLabel}" (${finalValidation.sourceOccurrences} occurrence${
|
||||
finalValidation.sourceOccurrences !== 1 ? 's' : ''
|
||||
}) into it?`,
|
||||
{ modal: true },
|
||||
'Merge Tags'
|
||||
);
|
||||
|
||||
if (confirmed !== 'Merge Tags') {
|
||||
return; // User cancelled merge
|
||||
}
|
||||
}
|
||||
|
||||
// Perform the rename
|
||||
await performTagRename(foam, tagLabel, cleanNewName, useHierarchicalRename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the actual tag rename operation by applying workspace edits.
|
||||
*
|
||||
* This internal function generates all necessary text edits and applies them
|
||||
* to the workspace. It provides user feedback through VS Code notifications
|
||||
* and logs the operation results.
|
||||
*
|
||||
* @param foam The Foam instance containing workspace and tag information
|
||||
* @param oldTagLabel The current tag label to be renamed
|
||||
* @param newTagLabel The new tag label to rename to
|
||||
* @param useHierarchicalRename Whether to rename child tags as well
|
||||
* @throws Error if workspace edits cannot be applied
|
||||
* @internal
|
||||
*/
|
||||
async function performTagRename(
|
||||
foam: Foam,
|
||||
oldTagLabel: string,
|
||||
newTagLabel: string,
|
||||
useHierarchicalRename: boolean = false
|
||||
): Promise<void> {
|
||||
// Generate all the edits - use hierarchical method if requested
|
||||
const tagEditResult = useHierarchicalRename
|
||||
? TagEdit.createHierarchicalRenameEdits(foam.tags, oldTagLabel, newTagLabel)
|
||||
: TagEdit.createRenameTagEdits(foam.tags, oldTagLabel, newTagLabel);
|
||||
|
||||
if (tagEditResult.totalOccurrences === 0) {
|
||||
vscode.window.showWarningMessage(
|
||||
`No occurrences of tag "${oldTagLabel}" found.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert to VS Code WorkspaceEdit
|
||||
const workspaceEdit = toVsCodeWorkspaceEdit(
|
||||
tagEditResult.edits,
|
||||
foam.workspace
|
||||
);
|
||||
|
||||
// Apply the edits
|
||||
const success = await vscode.workspace.applyEdit(workspaceEdit);
|
||||
|
||||
if (success) {
|
||||
// Calculate unique file count from workspace edits
|
||||
const uniqueFiles = new Set(
|
||||
tagEditResult.edits.map(edit => edit.uri.toString())
|
||||
).size;
|
||||
const occurrences = tagEditResult.totalOccurrences;
|
||||
|
||||
Logger.info(
|
||||
`Successfully renamed tag "${oldTagLabel}" to "${newTagLabel}" (${occurrences} occurrences across ${uniqueFiles} files)`
|
||||
);
|
||||
|
||||
vscode.window.showInformationMessage(
|
||||
`Renamed tag "${oldTagLabel}" to "${newTagLabel}" (${occurrences} occurrence${
|
||||
occurrences !== 1 ? 's' : ''
|
||||
} across ${uniqueFiles} file${uniqueFiles !== 1 ? 's' : ''})`
|
||||
);
|
||||
} else {
|
||||
throw new Error('Failed to apply workspace edits');
|
||||
}
|
||||
}
|
||||
61
packages/foam-vscode/src/features/commands/search-tag.ts
Normal file
61
packages/foam-vscode/src/features/commands/search-tag.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import { TagItem } from '../panels/tags-explorer';
|
||||
|
||||
export const SEARCH_TAG_COMMAND = {
|
||||
command: 'foam-vscode.search-tag',
|
||||
title: 'Foam: Search Tag',
|
||||
};
|
||||
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
const foam = await foamPromise;
|
||||
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand(
|
||||
SEARCH_TAG_COMMAND.command,
|
||||
async (tagLabelOrItem?: string | TagItem) => {
|
||||
let tagLabel: string | undefined;
|
||||
|
||||
// Handle both string and TagItem parameters
|
||||
if (typeof tagLabelOrItem === 'string') {
|
||||
tagLabel = tagLabelOrItem;
|
||||
} else if (
|
||||
tagLabelOrItem &&
|
||||
typeof tagLabelOrItem === 'object' &&
|
||||
'tag' in tagLabelOrItem
|
||||
) {
|
||||
tagLabel = tagLabelOrItem.tag;
|
||||
}
|
||||
|
||||
if (!tagLabel) {
|
||||
// If no tag provided, show tag picker
|
||||
const allTags = Array.from(foam.tags.tags.keys()).sort();
|
||||
if (allTags.length === 0) {
|
||||
vscode.window.showInformationMessage('No tags found in workspace.');
|
||||
return;
|
||||
}
|
||||
|
||||
tagLabel = await vscode.window.showQuickPick(allTags, {
|
||||
title: 'Select a tag to search',
|
||||
placeHolder: 'Choose a tag to search for...',
|
||||
});
|
||||
|
||||
if (!tagLabel) {
|
||||
return; // User cancelled
|
||||
}
|
||||
}
|
||||
|
||||
// Use VS Code's built-in search with the tag pattern
|
||||
await vscode.commands.executeCommand('workbench.action.findInFiles', {
|
||||
query: `#${tagLabel}`,
|
||||
triggerSearch: true,
|
||||
matchWholeWord: false,
|
||||
isCaseSensitive: true,
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import linkDecorations from './document-decorator';
|
||||
import navigationProviders from './navigation-provider';
|
||||
import wikilinkDiagnostics from './wikilink-diagnostics';
|
||||
import refactor from './refactor';
|
||||
import workspaceSymbolProvider from './workspace-symbol-provider';
|
||||
import tagRenameProvider from './tag-rename-provider';
|
||||
|
||||
export const features: FoamFeature[] = [
|
||||
...Object.values(commands),
|
||||
@@ -23,4 +25,6 @@ export const features: FoamFeature[] = [
|
||||
preview,
|
||||
completionProvider,
|
||||
tagCompletionProvider,
|
||||
workspaceSymbolProvider,
|
||||
tagRenameProvider,
|
||||
];
|
||||
|
||||
@@ -13,6 +13,7 @@ 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 { FoamTags } from '../core/model/tags';
|
||||
|
||||
describe('Document navigation', () => {
|
||||
const parser = createMarkdownParser([]);
|
||||
@@ -33,9 +34,10 @@ describe('Document navigation', () => {
|
||||
const { uri, content } = await createFile('');
|
||||
const ws = createTestWorkspace().set(parser.parse(uri, content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const doc = await vscode.workspace.openTextDocument(toVsCodeUri(uri));
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const links = provider.provideDocumentLinks(doc);
|
||||
|
||||
expect(links.length).toEqual(0);
|
||||
@@ -47,9 +49,10 @@ describe('Document navigation', () => {
|
||||
);
|
||||
const ws = createTestWorkspace().set(parser.parse(uri, content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const doc = await vscode.workspace.openTextDocument(toVsCodeUri(uri));
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const links = provider.provideDocumentLinks(doc);
|
||||
|
||||
expect(links.length).toEqual(0);
|
||||
@@ -62,9 +65,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileA.uri, fileB.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const links = provider.provideDocumentLinks(doc);
|
||||
|
||||
expect(links.length).toEqual(0);
|
||||
@@ -75,9 +79,10 @@ describe('Document navigation', () => {
|
||||
const noteA = parser.parse(fileA.uri, fileA.content);
|
||||
const ws = createTestWorkspace().set(noteA);
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileA.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const links = provider.provideDocumentLinks(doc);
|
||||
|
||||
expect(links.length).toEqual(1);
|
||||
@@ -103,9 +108,10 @@ describe('Document navigation', () => {
|
||||
parser.parse(fileA.uri, fileA.content)
|
||||
);
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileA.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const definitions = await provider.provideDefinition(
|
||||
doc,
|
||||
new vscode.Position(0, 22)
|
||||
@@ -120,9 +126,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileB.uri, fileB.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const definitions = await provider.provideDefinition(
|
||||
doc,
|
||||
new vscode.Position(0, 22)
|
||||
@@ -147,9 +154,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileB.uri, fileB.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
|
||||
const definitions = await provider.provideDefinition(
|
||||
doc,
|
||||
@@ -170,9 +178,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileB.uri, fileB.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const definitions = await provider.provideDefinition(
|
||||
doc,
|
||||
new vscode.Position(0, 22)
|
||||
@@ -193,9 +202,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileB.uri, fileB.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
const definitions = await provider.provideDefinition(
|
||||
doc,
|
||||
new vscode.Position(3, 10)
|
||||
@@ -223,9 +233,10 @@ describe('Document navigation', () => {
|
||||
.set(parser.parse(fileC.uri, fileC.content))
|
||||
.set(parser.parse(fileD.uri, fileD.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileB.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
|
||||
const refs = await provider.provideReferences(
|
||||
doc,
|
||||
@@ -241,6 +252,91 @@ describe('Document navigation', () => {
|
||||
range: new vscode.Range(0, 23, 0, 23 + 9),
|
||||
});
|
||||
});
|
||||
|
||||
it('should provide references for tags', async () => {
|
||||
const fileA = await createFile('This file has #tag1 and #tag2.');
|
||||
const fileB = await createFile(
|
||||
'This file also has #tag1 and other content.'
|
||||
);
|
||||
const fileC = await createFile('This file has #tag2 and #tag3.');
|
||||
|
||||
const ws = createTestWorkspace()
|
||||
.set(parser.parse(fileA.uri, fileA.content))
|
||||
.set(parser.parse(fileB.uri, fileB.content))
|
||||
.set(parser.parse(fileC.uri, fileC.content));
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileA.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
|
||||
// Test references for #tag1 (position 15 is within the #tag1 text)
|
||||
const tag1Refs = await provider.provideReferences(
|
||||
doc,
|
||||
new vscode.Position(0, 15)
|
||||
);
|
||||
|
||||
expect(tag1Refs.length).toEqual(2); // #tag1 appears in fileA and fileB
|
||||
|
||||
const refUris = tag1Refs.map(ref => ref.uri);
|
||||
expect(refUris).toContainEqual(toVsCodeUri(fileA.uri));
|
||||
expect(refUris).toContainEqual(toVsCodeUri(fileB.uri));
|
||||
});
|
||||
|
||||
it('should provide references for tags with different positions', async () => {
|
||||
const fileA = await createFile(
|
||||
'Multiple #same-tag mentions #same-tag here.'
|
||||
);
|
||||
|
||||
const ws = createTestWorkspace().set(
|
||||
parser.parse(fileA.uri, fileA.content)
|
||||
);
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileA.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
|
||||
// Test references for #same-tag (clicking on first occurrence)
|
||||
const refs = await provider.provideReferences(
|
||||
doc,
|
||||
new vscode.Position(0, 10) // Position within first #same-tag
|
||||
);
|
||||
|
||||
expect(refs.length).toEqual(2); // Both occurrences of #same-tag
|
||||
|
||||
// Verify both ranges are correct
|
||||
const sortedRefs = refs.sort(
|
||||
(a, b) => a.range.start.character - b.range.start.character
|
||||
);
|
||||
|
||||
// First occurrence: "Multiple #same-tag mentions"
|
||||
expect(sortedRefs[0].range.start.character).toBeLessThan(
|
||||
sortedRefs[1].range.start.character
|
||||
);
|
||||
});
|
||||
|
||||
it('should not provide references when position is not on a tag', async () => {
|
||||
const fileA = await createFile('This file has #tag1 and normal text.');
|
||||
|
||||
const ws = createTestWorkspace().set(
|
||||
parser.parse(fileA.uri, fileA.content)
|
||||
);
|
||||
const graph = FoamGraph.fromWorkspace(ws);
|
||||
const tags = FoamTags.fromWorkspace(ws);
|
||||
|
||||
const { doc } = await showInEditor(fileA.uri);
|
||||
const provider = new NavigationProvider(ws, graph, parser, tags);
|
||||
|
||||
// Position on "normal text" (not on a tag or link)
|
||||
const refs = await provider.provideReferences(
|
||||
doc,
|
||||
new vscode.Position(0, 30)
|
||||
);
|
||||
|
||||
expect(refs).toBeUndefined();
|
||||
});
|
||||
|
||||
it.todo('should provide references for placeholders');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import { CREATE_NOTE_COMMAND } from './commands/create-note';
|
||||
import { commandAsURI } from '../utils/commands';
|
||||
import { Location } from '../core/model/location';
|
||||
import { getFoamDocSelectors } from '../services/editor';
|
||||
import { FoamTags } from '../core/model/tags';
|
||||
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
@@ -21,7 +22,8 @@ export default async function activate(
|
||||
const navigationProvider = new NavigationProvider(
|
||||
foam.workspace,
|
||||
foam.graph,
|
||||
foam.services.parser
|
||||
foam.services.parser,
|
||||
foam.tags
|
||||
);
|
||||
|
||||
context.subscriptions.push(
|
||||
@@ -61,11 +63,12 @@ export class NavigationProvider
|
||||
constructor(
|
||||
private workspace: FoamWorkspace,
|
||||
private graph: FoamGraph,
|
||||
private parser: ResourceParser
|
||||
private parser: ResourceParser,
|
||||
private tags: FoamTags
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Provide references for links and placeholders
|
||||
* Provide references for links, placeholders, and tags
|
||||
*/
|
||||
public provideReferences(
|
||||
document: vscode.TextDocument,
|
||||
@@ -75,21 +78,50 @@ export class NavigationProvider
|
||||
fromVsCodeUri(document.uri),
|
||||
document.getText()
|
||||
);
|
||||
|
||||
// Check if position is on a tag first
|
||||
const targetTag = resource.tags.find(tag =>
|
||||
Range.containsPosition(tag.range, position)
|
||||
);
|
||||
if (targetTag) {
|
||||
return this.getTagReferences(targetTag.label);
|
||||
}
|
||||
|
||||
// Check if position is on a link
|
||||
const targetLink: ResourceLink | undefined = resource.links.find(link =>
|
||||
Range.containsPosition(link.range, position)
|
||||
);
|
||||
if (!targetLink) {
|
||||
return;
|
||||
if (targetLink) {
|
||||
const uri = this.workspace.resolveLink(resource, targetLink);
|
||||
return this.graph
|
||||
.getBacklinks(uri)
|
||||
.map(
|
||||
connection =>
|
||||
new vscode.Location(
|
||||
toVsCodeUri(connection.source),
|
||||
toVsCodeRange(connection.link.range)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const uri = this.workspace.resolveLink(resource, targetLink);
|
||||
return;
|
||||
}
|
||||
|
||||
return this.graph.getBacklinks(uri).map(connection => {
|
||||
return new vscode.Location(
|
||||
toVsCodeUri(connection.source),
|
||||
toVsCodeRange(connection.link.range)
|
||||
/**
|
||||
* Get all references for a given tag label across the workspace
|
||||
*/
|
||||
private getTagReferences(tagLabel: string): vscode.Location[] {
|
||||
const references: vscode.Location[] = [];
|
||||
const tagLocations = this.tags.tags.get(tagLabel) ?? [];
|
||||
for (const tagLocation of tagLocations) {
|
||||
references.push(
|
||||
new vscode.Location(
|
||||
toVsCodeUri(tagLocation.uri),
|
||||
toVsCodeRange(tagLocation.range)
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
return references;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import { workspace, window } from 'vscode';
|
||||
import { createTestNote, createTestWorkspace } from '../../test/test-utils';
|
||||
import {
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
} from './utils/tree-view-utils';
|
||||
import { BaseTreeProvider } from './utils/base-tree-provider';
|
||||
import { isNone } from '../../core/utils';
|
||||
import { getWorkspaceDefaultScheme } from '../../services/editor';
|
||||
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
@@ -140,7 +141,7 @@ export class ConnectionsTreeDataProvider extends BaseTreeProvider<vscode.TreeIte
|
||||
|
||||
const resourceItems = [];
|
||||
for (const [uriString, items] of byResource.entries()) {
|
||||
const uri = URI.parse(uriString);
|
||||
const uri = URI.parse(uriString, getWorkspaceDefaultScheme());
|
||||
const item = uri.isPlaceholder()
|
||||
? new UriTreeItem(uri, {
|
||||
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import { createTestNote } from '../../test/test-utils';
|
||||
import { cleanWorkspace, closeEditors } from '../../test/test-utils-vscode';
|
||||
import { TagItem, TagsProvider } from './tags-explorer';
|
||||
|
||||
@@ -164,7 +164,7 @@ export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
|
||||
|
||||
refresh(): void {
|
||||
this.tags = [...this.foamTags.tags]
|
||||
.map(([tag, notes]) => ({ tag, notes }))
|
||||
.map(([tag, resources]) => ({ tag, notes: resources.map(r => r.uri) }))
|
||||
.sort((a, b) => a.tag.localeCompare(b.tag));
|
||||
super.refresh();
|
||||
}
|
||||
@@ -183,11 +183,13 @@ export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
|
||||
}
|
||||
|
||||
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;
|
||||
const uniqueUris = new Set<string>();
|
||||
walk(node, tag => {
|
||||
const tagLocations = this.foamTags.tags.get(tag) ?? [];
|
||||
tagLocations.forEach(location => uniqueUris.add(location.uri.toString()));
|
||||
return 0; // Return value not used when collecting URIs
|
||||
});
|
||||
return uniqueUris.size;
|
||||
}
|
||||
|
||||
createFolderTreeItem(
|
||||
@@ -205,8 +207,9 @@ export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
|
||||
node: Folder<string>
|
||||
): TagItem {
|
||||
const nChildren = this.countResourcesInSubtree(node);
|
||||
const resources = this.foamTags.tags.get(value) ?? [];
|
||||
return new TagItem(node, nChildren, resources, parent);
|
||||
const tagLocations = this.foamTags.tags.get(value) ?? [];
|
||||
const resourceUris = tagLocations.map(location => location.uri);
|
||||
return new TagItem(node, nChildren, resourceUris, parent);
|
||||
}
|
||||
|
||||
async getChildren(element?: TagItem): Promise<TagTreeItem[]> {
|
||||
@@ -219,20 +222,20 @@ export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
|
||||
const subtags = await super.getChildren(element);
|
||||
|
||||
// Compute the resources children
|
||||
const resourceTags: ResourceRangeTreeItem[] = (element?.notes ?? [])
|
||||
.map(uri => this.workspace.get(uri))
|
||||
.reduce((acc, note) => {
|
||||
const tags = note.tags.filter(t => t.label === element.tag);
|
||||
const items = tags.map(t =>
|
||||
ResourceRangeTreeItem.createStandardItem(
|
||||
this.workspace,
|
||||
note,
|
||||
t.range,
|
||||
'tag'
|
||||
)
|
||||
const resourceTags: ResourceRangeTreeItem[] = [];
|
||||
if (element) {
|
||||
const tagLocations = this.foamTags.tags.get(element.tag) ?? [];
|
||||
const resourceTagPromises = tagLocations.map(async tagLocation => {
|
||||
const note = this.workspace.get(tagLocation.uri);
|
||||
return ResourceRangeTreeItem.createStandardItem(
|
||||
this.workspace,
|
||||
note,
|
||||
tagLocation.range,
|
||||
'tag'
|
||||
);
|
||||
return [...acc, ...items];
|
||||
}, []);
|
||||
});
|
||||
resourceTags.push(...(await Promise.all(resourceTagPromises)));
|
||||
}
|
||||
const resources = (
|
||||
await groupRangesByResource(this.workspace, resourceTags)
|
||||
).map(item => {
|
||||
|
||||
@@ -19,7 +19,6 @@ export interface Folder<T> {
|
||||
export class FolderTreeItem<T> extends vscode.TreeItem {
|
||||
collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
|
||||
contextValue = 'folder';
|
||||
iconPath = new vscode.ThemeIcon('folder');
|
||||
|
||||
constructor(
|
||||
public node: Folder<T>,
|
||||
|
||||
@@ -43,6 +43,9 @@ export class UriTreeItem extends BaseTreeItem {
|
||||
}
|
||||
|
||||
export class ResourceTreeItem extends UriTreeItem {
|
||||
iconPath = vscode.ThemeIcon.File;
|
||||
contextValue = 'foam.resource';
|
||||
|
||||
constructor(
|
||||
public readonly resource: Resource,
|
||||
private readonly workspace: FoamWorkspace,
|
||||
@@ -62,8 +65,6 @@ export class ResourceTreeItem extends UriTreeItem {
|
||||
title: 'Go to location',
|
||||
};
|
||||
this.resourceUri = toVsCodeUri(resource.uri);
|
||||
this.iconPath = vscode.ThemeIcon.File;
|
||||
this.contextValue = 'foam.resource';
|
||||
}
|
||||
|
||||
async resolveTreeItem(): Promise<ResourceTreeItem> {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { default as markdownItFoamTags } from './tag-highlight';
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { createMarkdownParser } from '../../core/services/markdown-parser';
|
||||
@@ -385,7 +386,7 @@ content-card![[note-e#Section 2]]`);
|
||||
);
|
||||
});
|
||||
|
||||
it('should display a warning in case of cyclical inclusions', async () => {
|
||||
it.skip('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]]',
|
||||
['preview', 'note-a.md']
|
||||
|
||||
@@ -2,6 +2,8 @@ import {
|
||||
WIKILINK_EMBED_REGEX,
|
||||
WIKILINK_EMBED_REGEX_GROUPS,
|
||||
retrieveNoteConfig,
|
||||
parseImageParameters,
|
||||
generateImageStyles,
|
||||
} from './wikilink-embed';
|
||||
import * as config from '../../services/config';
|
||||
|
||||
@@ -58,6 +60,324 @@ describe('Wikilink Note Embedding', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Image Parameter Parsing', () => {
|
||||
it('should parse wikilinks with image sizing parameters', () => {
|
||||
// Width only
|
||||
const match1 = '![[image.png|300]]'.match(WIKILINK_EMBED_REGEX_GROUPS);
|
||||
expect(match1[0]).toEqual('![[image.png|300]]');
|
||||
expect(match1[1]).toEqual(undefined); // no modifier
|
||||
expect(match1[2]).toEqual('image.png');
|
||||
expect(match1[3]).toEqual('|300');
|
||||
|
||||
// Width and height
|
||||
const match2 = '![[image.png|300x200]]'.match(
|
||||
WIKILINK_EMBED_REGEX_GROUPS
|
||||
);
|
||||
expect(match2[0]).toEqual('![[image.png|300x200]]');
|
||||
expect(match2[1]).toEqual(undefined);
|
||||
expect(match2[2]).toEqual('image.png');
|
||||
expect(match2[3]).toEqual('|300x200');
|
||||
|
||||
// Percentage width
|
||||
const match3 = '![[image.png|50%]]'.match(WIKILINK_EMBED_REGEX_GROUPS);
|
||||
expect(match3[0]).toEqual('![[image.png|50%]]');
|
||||
expect(match3[1]).toEqual(undefined);
|
||||
expect(match3[2]).toEqual('image.png');
|
||||
expect(match3[3]).toEqual('|50%');
|
||||
});
|
||||
|
||||
it('should parse wikilinks with modifiers and image parameters', () => {
|
||||
const match = 'content![[image.png|300]]'.match(
|
||||
WIKILINK_EMBED_REGEX_GROUPS
|
||||
);
|
||||
expect(match[0]).toEqual('content![[image.png|300]]');
|
||||
expect(match[1]).toEqual('content');
|
||||
expect(match[2]).toEqual('image.png');
|
||||
expect(match[3]).toEqual('|300');
|
||||
});
|
||||
|
||||
it('should parse wikilinks with multiple parameters', () => {
|
||||
const match = '![[image.png|300|center]]'.match(
|
||||
WIKILINK_EMBED_REGEX_GROUPS
|
||||
);
|
||||
expect(match[0]).toEqual('![[image.png|300|center]]');
|
||||
expect(match[1]).toEqual(undefined);
|
||||
expect(match[2]).toEqual('image.png');
|
||||
expect(match[3]).toEqual('|300|center');
|
||||
});
|
||||
|
||||
it('should handle wikilinks without parameters (backward compatibility)', () => {
|
||||
const match = '![[image.png]]'.match(WIKILINK_EMBED_REGEX_GROUPS);
|
||||
expect(match[0]).toEqual('![[image.png]]');
|
||||
expect(match[1]).toEqual(undefined);
|
||||
expect(match[2]).toEqual('image.png');
|
||||
expect(match[3]).toEqual(undefined);
|
||||
});
|
||||
|
||||
it('should parse complex filenames with parameters', () => {
|
||||
const match = '![[folder/image-file.png|400px]]'.match(
|
||||
WIKILINK_EMBED_REGEX_GROUPS
|
||||
);
|
||||
expect(match[0]).toEqual('![[folder/image-file.png|400px]]');
|
||||
expect(match[1]).toEqual(undefined);
|
||||
expect(match[2]).toEqual('folder/image-file.png');
|
||||
expect(match[3]).toEqual('|400px');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseImageParameters Function', () => {
|
||||
it('should parse width-only parameters', () => {
|
||||
const result = parseImageParameters('image.png', '|300');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse width x height parameters', () => {
|
||||
const result = parseImageParameters('image.png', '|300x200');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
height: '200',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse percentage widths', () => {
|
||||
const result = parseImageParameters('image.png', '|50%');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '50%',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse width with units', () => {
|
||||
const result = parseImageParameters('image.png', '|400px');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '400px',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse width and alignment', () => {
|
||||
const result = parseImageParameters('image.png', '|300|center');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'center',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse width, alignment, and alt text', () => {
|
||||
const result = parseImageParameters(
|
||||
'image.png',
|
||||
'|300|left|My image description'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'left',
|
||||
alt: 'My image description',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse width and alt text (no alignment)', () => {
|
||||
const result = parseImageParameters(
|
||||
'image.png',
|
||||
'|300|My image description'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
alt: 'My image description',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle no parameters', () => {
|
||||
const result = parseImageParameters('image.png');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty parameters string', () => {
|
||||
const result = parseImageParameters('image.png', '');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle malformed parameters gracefully', () => {
|
||||
const result = parseImageParameters('image.png', '|');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse complex width x height with units', () => {
|
||||
const result = parseImageParameters('image.png', '|400px x 300px');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '400px',
|
||||
height: '300px',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle right alignment', () => {
|
||||
const result = parseImageParameters('image.png', '|300|right');
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'right',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle alt text with pipes', () => {
|
||||
const result = parseImageParameters(
|
||||
'image.png',
|
||||
'|300|center|Alt text with | pipes'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'center',
|
||||
alt: 'Alt text with | pipes',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateImageStyles Function', () => {
|
||||
const mockMd = {
|
||||
normalizeLink: (path: string) => path,
|
||||
} as any;
|
||||
|
||||
it('should generate basic image HTML without parameters', () => {
|
||||
const params = { filename: 'image.png' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual('<img src="image.png" alt="">');
|
||||
});
|
||||
|
||||
it('should generate image with width only', () => {
|
||||
const params = { filename: 'image.png', width: '300' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 300px; height: auto" alt="">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with width and height', () => {
|
||||
const params = { filename: 'image.png', width: '300', height: '200' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 300px; height: 200px" alt="">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with percentage width', () => {
|
||||
const params = { filename: 'image.png', width: '50%' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 50%; height: auto" alt="">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with width and units preserved', () => {
|
||||
const params = { filename: 'image.png', width: '400px' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 400px; height: auto" alt="">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with center alignment', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'center' as const,
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<div style="text-align: center;"><img src="image.png" style="width: 300px; height: auto" alt=""></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with left alignment', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'left' as const,
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<div style="text-align: left;"><img src="image.png" style="width: 300px; height: auto" alt=""></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with right alignment', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'right' as const,
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<div style="text-align: right;"><img src="image.png" style="width: 300px; height: auto" alt=""></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with alt text', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
alt: 'My image description',
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 300px; height: auto" alt="My image description">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should escape HTML in alt text', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
alt: 'Image with <script>alert("xss")</script>',
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" alt="Image with <script>alert("xss")</script>">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should generate image with width, alignment, and alt text', () => {
|
||||
const params = {
|
||||
filename: 'image.png',
|
||||
width: '300',
|
||||
align: 'center' as const,
|
||||
alt: 'Centered image',
|
||||
};
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<div style="text-align: center;"><img src="image.png" style="width: 300px; height: auto" alt="Centered image"></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle em units', () => {
|
||||
const params = { filename: 'image.png', width: '20em' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 20em; height: auto" alt="">'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle decimal values', () => {
|
||||
const params = { filename: 'image.png', width: '300.5' };
|
||||
const result = generateImageStyles(params, mockMd);
|
||||
expect(result).toEqual(
|
||||
'<img src="image.png" style="width: 300.5px; height: auto" alt="">'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Config Parsing', () => {
|
||||
it('should use preview.embedNoteType if an explicit modifier is not passed in', () => {
|
||||
jest
|
||||
|
||||
@@ -8,7 +8,7 @@ import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { Logger } from '../../core/utils/log';
|
||||
import { Resource, ResourceParser } from '../../core/model/note';
|
||||
import { getFoamVsCodeConfig } from '../../services/config';
|
||||
import { fromVsCodeUri, toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { fromVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { MarkdownLink } from '../../core/services/markdown-link';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { Position } from '../../core/model/position';
|
||||
@@ -25,7 +25,7 @@ export const WIKILINK_EMBED_REGEX =
|
||||
// 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+)))?!\[\[([^[\]]+?)\]\]/;
|
||||
/((?:\w+)|(?:(?:\w+)-(?:\w+)))?!\[\[([^|[\]]+?)(\|[^[\]]+?)?\]\]/;
|
||||
export const CONFIG_EMBED_NOTE_TYPE = 'preview.embedNoteType';
|
||||
let refsStack: string[] = [];
|
||||
|
||||
@@ -39,9 +39,8 @@ export const markdownItWikilinkEmbed = (
|
||||
regex: WIKILINK_EMBED_REGEX,
|
||||
replace: (wikilinkItem: string) => {
|
||||
try {
|
||||
const [, noteEmbedModifier, wikilink] = wikilinkItem.match(
|
||||
WIKILINK_EMBED_REGEX_GROUPS
|
||||
);
|
||||
const [, noteEmbedModifier, wikilink, parametersString] =
|
||||
wikilinkItem.match(WIKILINK_EMBED_REGEX_GROUPS);
|
||||
|
||||
if (isVirtualWorkspace()) {
|
||||
return `
|
||||
@@ -82,10 +81,11 @@ export const markdownItWikilinkEmbed = (
|
||||
noteEmbedModifier,
|
||||
parser,
|
||||
workspace,
|
||||
md
|
||||
md,
|
||||
parametersString
|
||||
);
|
||||
refsStack.pop();
|
||||
return content;
|
||||
return refsStack.length === 0 ? md.render(content) : content;
|
||||
} catch (e) {
|
||||
Logger.error(
|
||||
`Error while including ${wikilinkItem} into the current document of the Preview panel`,
|
||||
@@ -102,10 +102,11 @@ function getNoteContent(
|
||||
noteEmbedModifier: string | undefined,
|
||||
parser: ResourceParser,
|
||||
workspace: FoamWorkspace,
|
||||
md: markdownit
|
||||
md: markdownit,
|
||||
parametersString?: string
|
||||
): string {
|
||||
let content = `Embed for [[${includedNote.uri.path}]]`;
|
||||
let html: string;
|
||||
let toRender: string;
|
||||
|
||||
switch (includedNote.type) {
|
||||
case 'note': {
|
||||
@@ -126,7 +127,7 @@ function getNoteContent(
|
||||
: cardFormatter;
|
||||
|
||||
content = extractor(includedNote, parser, workspace);
|
||||
html = formatter(content, md);
|
||||
toRender = formatter(content, md);
|
||||
break;
|
||||
}
|
||||
case 'attachment':
|
||||
@@ -135,19 +136,23 @@ function getNoteContent(
|
||||
${md.renderInline('[[' + includedNote.uri.path + ']]')}<br/>
|
||||
Embed for attachments is not supported
|
||||
</div>`;
|
||||
html = md.render(content);
|
||||
toRender = md.render(content);
|
||||
break;
|
||||
case 'image':
|
||||
content = `<div class="embed-container-image">${md.render(
|
||||
`})`
|
||||
)}</div>`;
|
||||
html = md.render(content);
|
||||
case 'image': {
|
||||
const imageParams = parseImageParameters(
|
||||
includedNote.uri.path,
|
||||
parametersString
|
||||
);
|
||||
const imageHtml = generateImageStyles(imageParams, md);
|
||||
content = `<div class="embed-container-image">${imageHtml}</div>`;
|
||||
toRender = content;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
html = content;
|
||||
toRender = content;
|
||||
}
|
||||
|
||||
return html;
|
||||
return toRender;
|
||||
}
|
||||
|
||||
function withLinksRelativeToWorkspaceRoot(
|
||||
@@ -271,13 +276,126 @@ function contentExtractor(
|
||||
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>`
|
||||
);
|
||||
return `<div class="embed-container-note">\n\n${content}\n\n</div>`;
|
||||
}
|
||||
|
||||
function inlineFormatter(content: string, md: markdownit): string {
|
||||
return md.render(content);
|
||||
return content;
|
||||
}
|
||||
|
||||
interface ImageParameters {
|
||||
filename: string;
|
||||
width?: string;
|
||||
height?: string;
|
||||
align?: 'center' | 'left' | 'right';
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
function parseImageParameters(
|
||||
wikilink: string,
|
||||
parametersString?: string
|
||||
): ImageParameters {
|
||||
const result: ImageParameters = {
|
||||
filename: wikilink,
|
||||
};
|
||||
|
||||
if (!parametersString) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Remove the leading pipe and split by remaining pipes
|
||||
const params = parametersString.slice(1).split('|');
|
||||
|
||||
if (params.length === 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// First parameter is always size
|
||||
const sizeParam = params[0]?.trim();
|
||||
if (sizeParam) {
|
||||
// Parse size parameter: could be "300", "300x200", "50%", "300px", etc.
|
||||
// Check for width x height format (but not if it's just a unit like "px")
|
||||
const dimensionMatch = sizeParam.match(
|
||||
/^(\d+(?:\.\d+)?(?:px|%|em|rem|vw|vh)?)\s*x\s*(\d+(?:\.\d+)?(?:px|%|em|rem|vw|vh)?)$/i
|
||||
);
|
||||
if (dimensionMatch) {
|
||||
// Width x Height format
|
||||
result.width = dimensionMatch[1]?.trim();
|
||||
result.height = dimensionMatch[2]?.trim();
|
||||
} else {
|
||||
// Width only
|
||||
result.width = sizeParam;
|
||||
}
|
||||
}
|
||||
|
||||
// Second parameter could be alignment
|
||||
const alignParam = params[1]?.trim().toLowerCase();
|
||||
if (alignParam && ['center', 'left', 'right'].includes(alignParam)) {
|
||||
result.align = alignParam as 'center' | 'left' | 'right';
|
||||
} else if (alignParam) {
|
||||
// If not alignment, treat as alt text
|
||||
result.alt = params.slice(1).join('|').trim();
|
||||
}
|
||||
|
||||
// Third parameter onwards is alt text (if second wasn't alt text)
|
||||
if (result.align && params.length > 2) {
|
||||
result.alt = params.slice(2).join('|').trim();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function generateImageStyles(params: ImageParameters, md: markdownit): string {
|
||||
const { filename, width, height, align, alt } = params;
|
||||
|
||||
// Build CSS styles for the image
|
||||
const styles: string[] = [];
|
||||
|
||||
if (width) {
|
||||
styles.push(`width: ${addDefaultUnit(width)}`);
|
||||
|
||||
// If only width is specified, set height to auto to maintain aspect ratio
|
||||
if (!height) {
|
||||
styles.push('height: auto');
|
||||
}
|
||||
}
|
||||
|
||||
if (height) {
|
||||
styles.push(`height: ${addDefaultUnit(height)}`);
|
||||
}
|
||||
|
||||
const styleAttr = styles.length > 0 ? ` style="${styles.join('; ')}"` : '';
|
||||
const altAttr = alt ? ` alt="${escapeHtml(alt)}"` : ' alt=""';
|
||||
|
||||
// Generate the image HTML
|
||||
const imageHtml = `<img src="${md.normalizeLink(
|
||||
filename
|
||||
)}"${styleAttr}${altAttr}>`;
|
||||
|
||||
// Wrap with alignment if specified
|
||||
if (align) {
|
||||
return `<div style="text-align: ${align};">${imageHtml}</div>`;
|
||||
}
|
||||
|
||||
return imageHtml;
|
||||
}
|
||||
|
||||
function addDefaultUnit(value: string): string {
|
||||
// If no unit is specified and it's a pure number, add 'px'
|
||||
if (/^\d+(\.\d+)?$/.test(value)) {
|
||||
return value + 'px';
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function escapeHtml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
export { parseImageParameters, generateImageStyles };
|
||||
export default markdownItWikilinkEmbed;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* @unit-ready */
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { createTestNote } from '../../test/test-utils';
|
||||
|
||||
183
packages/foam-vscode/src/features/tag-rename-provider.ts
Normal file
183
packages/foam-vscode/src/features/tag-rename-provider.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { Foam } from '../core/model/foam';
|
||||
import { TagEdit } from '../core/services/tag-edit';
|
||||
import {
|
||||
fromVsCodeUri,
|
||||
toVsCodeRange,
|
||||
toVsCodeWorkspaceEdit,
|
||||
} from '../utils/vsc-utils';
|
||||
import { Logger } from '../core/utils/log';
|
||||
import { Position } from '../core/model/position';
|
||||
|
||||
/**
|
||||
* Activates the tag rename provider for native F2 rename support.
|
||||
*
|
||||
* This provider enables users to press F2 on any tag in markdown files
|
||||
* to trigger VS Code's built-in rename functionality, providing a native
|
||||
* experience for tag renaming that feels like renaming variables in code.
|
||||
*
|
||||
* @param context VS Code extension context for registering the provider
|
||||
* @param foamPromise Promise that resolves to the initialized Foam instance
|
||||
*/
|
||||
export default async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) {
|
||||
const foam = await foamPromise;
|
||||
const provider = new TagRenameProvider(foam);
|
||||
|
||||
context.subscriptions.push(
|
||||
vscode.languages.registerRenameProvider('markdown', provider)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* VS Code rename provider for Foam tags.
|
||||
*
|
||||
* This class implements the VS Code RenameProvider interface to enable
|
||||
* native F2 rename support for tags. It provides seamless integration
|
||||
* with VS Code's rename system while leveraging Foam's tag infrastructure.
|
||||
*/
|
||||
export class TagRenameProvider implements vscode.RenameProvider {
|
||||
constructor(private foam: Foam) {}
|
||||
|
||||
/**
|
||||
* Prepare a rename operation for VS Code's F2 rename functionality.
|
||||
*
|
||||
* This method is called when the user presses F2 or invokes "Rename Symbol"
|
||||
* from the context menu. It determines if the cursor is positioned on a tag
|
||||
* and returns the precise range and placeholder text for the rename operation.
|
||||
*
|
||||
* @param document The VS Code text document containing the tag
|
||||
* @param position The cursor position where F2 was pressed
|
||||
* @param token Cancellation token for the operation
|
||||
* @returns Range and placeholder for the tag if found, throws error otherwise
|
||||
* @throws Error if cursor is not positioned on a tag or tag range cannot be found
|
||||
*/
|
||||
prepareRename(
|
||||
document: vscode.TextDocument,
|
||||
position: vscode.Position,
|
||||
token: vscode.CancellationToken
|
||||
): vscode.ProviderResult<
|
||||
vscode.Range | { range: vscode.Range; placeholder: string }
|
||||
> {
|
||||
const fileUri = fromVsCodeUri(document.uri);
|
||||
const foamPosition = Position.create(position.line, position.character);
|
||||
const tagLabel = TagEdit.getTagAtPosition(
|
||||
this.foam.tags,
|
||||
fileUri,
|
||||
foamPosition
|
||||
);
|
||||
|
||||
if (!tagLabel) {
|
||||
// Not on a tag, reject the rename
|
||||
throw new Error('Cannot rename: cursor is not on a tag');
|
||||
}
|
||||
|
||||
// Find the exact range of this tag occurrence
|
||||
const tagLocations = this.foam.tags.tags.get(tagLabel) ?? [];
|
||||
for (const location of tagLocations) {
|
||||
if (location.uri.toString() !== fileUri.toString()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const range = location.range;
|
||||
const positionInRange =
|
||||
(position.line === range.start.line &&
|
||||
position.character >= range.start.character &&
|
||||
position.line === range.end.line &&
|
||||
position.character <= range.end.character) ||
|
||||
(position.line > range.start.line && position.line < range.end.line);
|
||||
|
||||
if (positionInRange) {
|
||||
return {
|
||||
range: toVsCodeRange(range),
|
||||
placeholder: tagLabel,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Cannot rename: tag range not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate workspace edits to perform the tag rename operation.
|
||||
*
|
||||
* This method is called after the user enters a new name in the rename dialog.
|
||||
* It validates the new name and generates all necessary text edits across the
|
||||
* entire workspace to rename every occurrence of the tag consistently.
|
||||
*
|
||||
* @param document The VS Code text document where rename was initiated
|
||||
* @param position The original cursor position where F2 was pressed
|
||||
* @param newName The new tag name entered by the user (may include # prefix)
|
||||
* @param token Cancellation token for the operation
|
||||
* @returns WorkspaceEdit containing all necessary changes across files
|
||||
* @throws Error if tag validation fails or rename operation cannot be completed
|
||||
*/
|
||||
provideRenameEdits(
|
||||
document: vscode.TextDocument,
|
||||
position: vscode.Position,
|
||||
newName: string,
|
||||
token: vscode.CancellationToken
|
||||
): vscode.ProviderResult<vscode.WorkspaceEdit> {
|
||||
const fileUri = fromVsCodeUri(document.uri);
|
||||
const foamPosition = Position.create(position.line, position.character);
|
||||
const oldTagLabel = TagEdit.getTagAtPosition(
|
||||
this.foam.tags,
|
||||
fileUri,
|
||||
foamPosition
|
||||
);
|
||||
|
||||
if (!oldTagLabel) {
|
||||
throw new Error('Cannot rename: cursor is not on a tag');
|
||||
}
|
||||
|
||||
// Clean the new name (remove # if user included it)
|
||||
const cleanNewName = newName.startsWith('#')
|
||||
? newName.substring(1)
|
||||
: newName;
|
||||
|
||||
// Validate the rename
|
||||
const validation = TagEdit.validateTagRename(
|
||||
this.foam.tags,
|
||||
oldTagLabel,
|
||||
cleanNewName
|
||||
);
|
||||
|
||||
if (!validation.isValid) {
|
||||
throw new Error(validation.message);
|
||||
}
|
||||
|
||||
// For F2 rename, we don't support merge confirmation dialogs
|
||||
// Direct users to use the command instead
|
||||
if (validation.isMerge) {
|
||||
throw new Error(
|
||||
`Tag "${cleanNewName}" already exists. Use "Foam: Rename Tag" command to merge tags.`
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// Generate all the edits
|
||||
const tagEditResult = TagEdit.createRenameTagEdits(
|
||||
this.foam.tags,
|
||||
oldTagLabel,
|
||||
cleanNewName
|
||||
);
|
||||
|
||||
// Convert to VS Code WorkspaceEdit
|
||||
const workspaceEdit = toVsCodeWorkspaceEdit(
|
||||
tagEditResult.edits,
|
||||
this.foam.workspace
|
||||
);
|
||||
|
||||
Logger.info(
|
||||
`Renaming tag "${oldTagLabel}" to "${cleanNewName}" (${tagEditResult.totalOccurrences} occurrences)`
|
||||
);
|
||||
|
||||
return workspaceEdit;
|
||||
} catch (error) {
|
||||
Logger.error('Error during tag rename operation:', error);
|
||||
throw new Error(`Failed to rename tag: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/* @unit-ready */
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { FoamWorkspaceSymbolProvider } from './workspace-symbol-provider';
|
||||
import { createTestNote, createTestWorkspace } from '../test/test-utils';
|
||||
|
||||
describe('FoamWorkspaceSymbolProvider Integration', () => {
|
||||
let provider: FoamWorkspaceSymbolProvider;
|
||||
let workspace: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
workspace = createTestWorkspace();
|
||||
provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
});
|
||||
|
||||
it('should integrate with VS Code workspace symbol search', async () => {
|
||||
// Create test notes with aliases
|
||||
const note1 = createTestNote({
|
||||
uri: '/test1.md',
|
||||
aliases: ['first alternative'],
|
||||
});
|
||||
|
||||
const note2 = createTestNote({
|
||||
uri: '/test2.md',
|
||||
aliases: ['second alternative'],
|
||||
});
|
||||
|
||||
workspace.set(note1);
|
||||
workspace.set(note2);
|
||||
|
||||
// Test the provider directly (simulating VS Code's call)
|
||||
const symbols = provider.provideWorkspaceSymbols('alt');
|
||||
|
||||
expect(symbols).toHaveLength(2);
|
||||
|
||||
const symbolNames = symbols.map(s => s.name);
|
||||
expect(symbolNames).toContain('first alternative');
|
||||
expect(symbolNames).toContain('second alternative');
|
||||
|
||||
// Verify symbol properties match VS Code expectations
|
||||
symbols.forEach(symbol => {
|
||||
expect(symbol).toBeInstanceOf(vscode.SymbolInformation);
|
||||
expect(symbol.kind).toBe(vscode.SymbolKind.String);
|
||||
expect(symbol.location).toBeInstanceOf(vscode.Location);
|
||||
expect(symbol.location.uri).toBeDefined();
|
||||
expect(symbol.location.range).toBeInstanceOf(vscode.Range);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle real-world alias formats from frontmatter', async () => {
|
||||
// Test with array format aliases
|
||||
const noteWithArrayAliases = createTestNote({
|
||||
uri: '/array-aliases.md',
|
||||
aliases: ['alias one', 'alias two'],
|
||||
});
|
||||
|
||||
// Test with comma-separated format aliases
|
||||
const noteWithCommaSeparated = createTestNote({
|
||||
uri: '/comma-aliases.md',
|
||||
aliases: ['first, second, third'],
|
||||
});
|
||||
|
||||
workspace.set(noteWithArrayAliases);
|
||||
workspace.set(noteWithCommaSeparated);
|
||||
|
||||
// Test searching for different parts
|
||||
const aliasOneResults = provider.provideWorkspaceSymbols('one');
|
||||
expect(aliasOneResults).toHaveLength(1);
|
||||
expect(aliasOneResults[0].name).toBe('alias one');
|
||||
|
||||
const commaResults = provider.provideWorkspaceSymbols('first');
|
||||
expect(commaResults).toHaveLength(1);
|
||||
expect(commaResults[0].name).toBe('first, second, third');
|
||||
});
|
||||
|
||||
it('should provide location information for navigation', async () => {
|
||||
const note = createTestNote({
|
||||
uri: '/location-test.md',
|
||||
aliases: ['test alias'],
|
||||
});
|
||||
|
||||
workspace.set(note);
|
||||
|
||||
const symbols = provider.provideWorkspaceSymbols('test');
|
||||
expect(symbols).toHaveLength(1);
|
||||
|
||||
const symbol = symbols[0];
|
||||
// The createTestNote function uses default ranges for aliases
|
||||
expect(symbol.location.range).toBeInstanceOf(vscode.Range);
|
||||
expect(symbol.containerName).toBe('location-test.md');
|
||||
});
|
||||
|
||||
it('should handle large workspace with many aliases efficiently (so we do not need to cache)', async () => {
|
||||
// Create many notes with aliases to test performance
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
const note = createTestNote({
|
||||
uri: `/note${i}.md`,
|
||||
aliases: [`alias number ${i}`, `alternative ${i}`],
|
||||
});
|
||||
workspace.set(note);
|
||||
}
|
||||
|
||||
// Performance test - should complete quickly as we have decided not to cache
|
||||
const start = Date.now();
|
||||
const symbols = provider.provideWorkspaceSymbols('alternative');
|
||||
const end = Date.now();
|
||||
|
||||
expect(symbols).toHaveLength(10000);
|
||||
expect(end - start).toBeLessThan(500); // Should complete in under 500ms
|
||||
});
|
||||
|
||||
it('should not interfere with existing markdown symbols', async () => {
|
||||
// This test verifies that our provider complements VS Code's built-in markdown symbols
|
||||
// rather than replacing them. We can't directly test VS Code's built-in provider,
|
||||
// but we can ensure our provider only returns aliases.
|
||||
|
||||
const note = createTestNote({
|
||||
uri: '/mixed-content.md',
|
||||
title: 'Main Title',
|
||||
aliases: ['only alias here'],
|
||||
sections: ['Section Heading'],
|
||||
});
|
||||
|
||||
workspace.set(note);
|
||||
|
||||
// Our provider should only return aliases, not sections or titles
|
||||
const symbols = provider.provideWorkspaceSymbols('');
|
||||
expect(symbols).toHaveLength(1);
|
||||
expect(symbols[0].name).toBe('only alias here');
|
||||
expect(symbols[0].kind).toBe(vscode.SymbolKind.String);
|
||||
|
||||
// Should not return sections (those are handled by VS Code's markdown provider)
|
||||
expect(symbols.find(s => s.name === 'Section Heading')).toBeUndefined();
|
||||
expect(symbols.find(s => s.name === 'Main Title')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,242 @@
|
||||
import { FoamWorkspaceSymbolProvider } from './workspace-symbol-provider';
|
||||
import { FoamWorkspace } from '../core/model/workspace';
|
||||
import { Resource } from '../core/model/note';
|
||||
import { URI } from '../core/model/uri';
|
||||
import { Range } from '../core/model/range';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
describe('FoamWorkspaceSymbolProvider', () => {
|
||||
describe('matchesQuery', () => {
|
||||
it('should match empty query', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
const result = provider.provideWorkspaceSymbols('');
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should match subsequence in alias title', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
expect(provider.matchesQuery('alt', 'alternative title')).toBe(true);
|
||||
expect(provider.matchesQuery('altit', 'alternative title')).toBe(true);
|
||||
expect(provider.matchesQuery('title', 'alternative title')).toBe(true);
|
||||
expect(provider.matchesQuery('tit', 'alternative title')).toBe(true);
|
||||
});
|
||||
|
||||
it('should not match wrong order', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
|
||||
expect(provider.matchesQuery('title alt', 'alternative title')).toBe(
|
||||
false
|
||||
);
|
||||
expect(provider.matchesQuery('zyx', 'alternative title')).toBe(false);
|
||||
});
|
||||
|
||||
it('should be case insensitive', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
|
||||
expect(provider.matchesQuery('ALT', 'alternative title')).toBe(true);
|
||||
expect(provider.matchesQuery('alt', 'ALTERNATIVE TITLE')).toBe(true);
|
||||
expect(provider.matchesQuery('AlT', 'Alternative Title')).toBe(true);
|
||||
});
|
||||
|
||||
it('should match exact strings', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
|
||||
expect(
|
||||
provider.matchesQuery('alternative title', 'alternative title')
|
||||
).toBe(true);
|
||||
expect(provider.matchesQuery('', 'alternative title')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('provideWorkspaceSymbols', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
|
||||
it('should return empty array when workspace is empty', () => {
|
||||
const provider = new FoamWorkspaceSymbolProvider(new FoamWorkspace());
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('test');
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when no aliases match', () => {
|
||||
const workspace = new FoamWorkspace();
|
||||
const provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
|
||||
const resource: Resource = {
|
||||
uri: URI.file('/test.md'),
|
||||
type: 'note',
|
||||
title: 'Test Note',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'different alias',
|
||||
range: Range.create(0, 0, 0, 10),
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
workspace.set(resource);
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('notfound');
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return matching aliases from single resource', () => {
|
||||
const workspace = new FoamWorkspace();
|
||||
const provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
|
||||
const aliasRange = Range.create(2, 0, 2, 20);
|
||||
const resource: Resource = {
|
||||
uri: URI.file('/test.md'),
|
||||
type: 'note',
|
||||
title: 'Test Note',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'alternative title',
|
||||
range: aliasRange,
|
||||
},
|
||||
{
|
||||
title: 'another name',
|
||||
range: aliasRange,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
workspace.set(resource);
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('alt');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].name).toBe('alternative title');
|
||||
expect(result[0].kind).toBe(vscode.SymbolKind.String);
|
||||
expect(result[0].containerName).toBe('test.md');
|
||||
});
|
||||
|
||||
it('should return matching aliases from multiple resources', () => {
|
||||
const workspace = new FoamWorkspace();
|
||||
const provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
|
||||
const aliasRange = Range.create(2, 0, 2, 20);
|
||||
|
||||
const resource1: Resource = {
|
||||
uri: URI.file('/note1.md'),
|
||||
type: 'note',
|
||||
title: 'Note 1',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'alternative one',
|
||||
range: aliasRange,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
|
||||
const resource2: Resource = {
|
||||
uri: URI.file('/note2.md'),
|
||||
type: 'note',
|
||||
title: 'Note 2',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'alternative two',
|
||||
range: aliasRange,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
|
||||
workspace.set(resource1);
|
||||
workspace.set(resource2);
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('alt');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map(s => s.name)).toContain('alternative one');
|
||||
expect(result.map(s => s.name)).toContain('alternative two');
|
||||
expect(result.map(s => s.containerName)).toContain('note1.md');
|
||||
expect(result.map(s => s.containerName)).toContain('note2.md');
|
||||
});
|
||||
|
||||
it('should return all aliases when query is empty', () => {
|
||||
const workspace = new FoamWorkspace();
|
||||
const provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
|
||||
const aliasRange = Range.create(2, 0, 2, 20);
|
||||
const resource: Resource = {
|
||||
uri: URI.file('/test.md'),
|
||||
type: 'note',
|
||||
title: 'Test Note',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'first alias',
|
||||
range: aliasRange,
|
||||
},
|
||||
{
|
||||
title: 'second alias',
|
||||
range: aliasRange,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
workspace.set(resource);
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map(s => s.name)).toContain('first alias');
|
||||
expect(result.map(s => s.name)).toContain('second alias');
|
||||
});
|
||||
|
||||
it('should create SymbolInformation with correct properties', () => {
|
||||
const workspace = new FoamWorkspace();
|
||||
const provider = new FoamWorkspaceSymbolProvider(workspace);
|
||||
|
||||
const aliasRange = Range.create(2, 5, 2, 25);
|
||||
const resource: Resource = {
|
||||
uri: URI.file('/path/to/note.md'),
|
||||
type: 'note',
|
||||
title: 'Test Note',
|
||||
properties: {},
|
||||
sections: [],
|
||||
tags: [],
|
||||
aliases: [
|
||||
{
|
||||
title: 'test alias',
|
||||
range: aliasRange,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
definitions: [],
|
||||
};
|
||||
workspace.set(resource);
|
||||
|
||||
const result = provider.provideWorkspaceSymbols('test');
|
||||
expect(result).toHaveLength(1);
|
||||
|
||||
const symbol = result[0];
|
||||
expect(symbol.name).toBe('test alias');
|
||||
expect(symbol.kind).toBe(vscode.SymbolKind.String);
|
||||
expect(symbol.containerName).toBe('note.md');
|
||||
expect(symbol.location.uri.toString()).toContain('/path/to/note.md');
|
||||
expect(symbol.location.range.start.line).toBe(2);
|
||||
expect(symbol.location.range.start.character).toBe(5);
|
||||
expect(symbol.location.range.end.line).toBe(2);
|
||||
expect(symbol.location.range.end.character).toBe(25);
|
||||
});
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user