Compare commits

...

2 Commits

Author SHA1 Message Date
Jani Eväkallio
08cfbed2ba Add WikiDocumentLinkProvider 2020-07-13 22:52:09 +01:00
Jani Eväkallio
26b162d540 Add NoteLink.position to keep track of link ranges 2020-07-13 22:45:05 +01:00
6 changed files with 97 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ import wikiLinkPlugin from 'remark-wiki-link';
import visit, { CONTINUE, EXIT } from 'unist-util-visit';
import { Node, Parent } from 'unist';
import * as path from 'path';
import { Link, Note, NoteGraph } from './note-graph';
import { Note, NoteLink, NoteGraph } from './note-graph';
import { dropExtension } from './utils';
let processor: unified.Processor | null = null;
@@ -29,13 +29,13 @@ export function createNoteFromMarkdown(uri: string, markdown: string): Note {
}
return title === id ? CONTINUE : EXIT;
});
const links: Link[] = [];
const links: NoteLink[] = [];
visit(tree, node => {
if (node.type === 'wikiLink') {
links.push({
from: id,
to: node.value as string,
text: node.value as string,
position: node.position!
});
}
});

View File

@@ -1,4 +1,5 @@
import { Graph, Edge } from 'graphlib';
import { Position } from 'unist';
type ID = string;
@@ -11,6 +12,7 @@ export interface Link {
export interface NoteLink {
to: ID;
text: string;
position: Position;
}
export class Note {

View File

@@ -1,5 +1,10 @@
import { NoteGraph, Note } from '../src/note-graph';
const position = {
start: { line: 0, column: 0},
end: { line: 0, column: 0}
};
describe('Note graph', () => {
it('Adds notes to graph', () => {
const graph = new NoteGraph();
@@ -22,7 +27,7 @@ describe('Note graph', () => {
new Note(
'page-b',
'page-b',
[{ to: 'page-a', text: 'go' }],
[{ to: 'page-a', text: 'go', position }],
'/page-b.md',
''
)
@@ -44,7 +49,7 @@ describe('Note graph', () => {
new Note(
'page-b',
'page-b',
[{ to: 'page-a', text: 'go' }],
[{ to: 'page-a', text: 'go', position }],
'/page-b.md',
''
)
@@ -73,7 +78,7 @@ describe('Note graph', () => {
new Note(
'page-a',
'page-a',
[{ to: 'non-existing', text: 'does not exist' }],
[{ to: 'non-existing', text: 'does not exist', position }],
'/path-b.md',
''
)
@@ -88,7 +93,7 @@ describe('Note graph', () => {
new Note(
'page-b',
'page-b',
[{ to: 'page-a', text: 'go' }],
[{ to: 'page-a', text: 'go', position }],
'/page-b.md',
''
)
@@ -118,7 +123,7 @@ describe('Note graph', () => {
new Note(
'page-b',
'page-b',
[{ to: 'page-c', text: 'go' }],
[{ to: 'page-c', text: 'go', position }],
'/path-2b.md',
''
)

View File

@@ -11,10 +11,10 @@ import { createNoteFromMarkdown, createFoam, FoamConfig } from "foam-core";
import { features } from "./features";
export function activate(context: ExtensionContext) {
const foamPromise = bootstrap(getConfig())
const foamPromise = bootstrap(getConfig());
features.forEach(f => {
f.activate(context, foamPromise);
})
});
}
const bootstrap = async (config: FoamConfig) => {
@@ -34,8 +34,5 @@ const bootstrap = async (config: FoamConfig) => {
};
const getConfig = () => {
return {}
}
return {};
};

View File

@@ -1,7 +1,9 @@
import createReferences from './wikilink-reference-generation'
import createReferences from './wikilink-reference-generation';
import createWikiDocumentLinkProvider from './wiki-document-link-provider';
import { FoamFeature } from '../types'
export const features: FoamFeature[] = [
createReferences
]
createReferences,
createWikiDocumentLinkProvider
];

View File

@@ -0,0 +1,73 @@
import {
DocumentLink,
DocumentLinkProvider,
ExtensionContext,
languages,
Range,
TextDocument,
Uri,
} from 'vscode';
import { Foam, NoteGraph, createNoteFromMarkdown } from 'foam-core';
export const LINK_SELECTOR = {
scheme: 'file',
language: 'markdown',
};
export const LINK_PREFIX = '[[';
export const LINK_SUFFIX = ']]';
const positionToLinkRange = (p) => {
return new Range(
p.start.line - 1,
p.start.column + LINK_PREFIX.length - 1,
p.end.line - 1,
p.end.column - LINK_SUFFIX.length
);
};
class WikiDocumentLinkProvider implements DocumentLinkProvider {
notes: NoteGraph;
constructor(_notes: NoteGraph) {
this.notes = _notes;
}
public provideDocumentLinks(
document: TextDocument
): DocumentLink[] | undefined {
const note = createNoteFromMarkdown(document.fileName, document.getText());
// update note in the graph
this.notes.setNote(note);
return note.links.map((link, index) => {
const p = link.position;
const to = this.notes.getNote(link.to);
if (to) {
const uri = Uri.parse(to.path);
const linkRange = positionToLinkRange(link.position);
const docLink = new DocumentLink(linkRange, uri);
docLink.tooltip = to.title;
return docLink;
}
}).filter(Boolean);
}
}
const feature = {
activate: async function createWikiDocumentLinkProvider(
_context: ExtensionContext,
foamPromise: Promise<Foam>
) {
languages.registerDocumentLinkProvider(
LINK_SELECTOR,
new WikiDocumentLinkProvider((await foamPromise).notes)
);
}
};
export default feature;