Skip to content

Commit

Permalink
Merge remote-tracking branch 'tchayen/master' into id-based-links
Browse files Browse the repository at this point in the history
  • Loading branch information
wgslr committed May 22, 2020
2 parents 0560a9c + 96cdb2a commit a1fb2b1
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 400 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.0] - 2020-05-22

### Changed

- `column` setting is now divided into `openColumn` and `showColumn`.

## [0.2.3] - 2020-05-22

### Fixed
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ You can open any example in the `examples` directory and try yourself by pressin

This extension contributes the following settings:

### `markdown-links.column`
### `markdown-links.showColumn`

Controls in which column should the graph appear. Refer to [Column values](###column-values). Defaults to `beside`.

### `markdown-links.openColumn`

Controls in which column should clicked files open. Refer to [Column values](###column-values). Defaults to `one`.

### Column values

- `active` – in the currently focused column.
- `beside` – other than the current.
Expand Down
2 changes: 1 addition & 1 deletion examples/functionality/With spaces - should work too.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# With spaces - should work too

[Next](next).md
[Next](next.md)
8 changes: 8 additions & 0 deletions examples/functionality/frontmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Test
id: 123
---

# Frontmatter

[README](README.md)
3 changes: 3 additions & 0 deletions examples/functionality/wiki-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Wiki

[[Wiki style link]].
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "markdown-links",
"displayName": "Markdown Links",
"description": "Adds command - Show Graph - that displays a graph of local links between markdown files in the current working directory.",
"version": "0.2.3",
"version": "0.3.0",
"publisher": "tchayen",
"engines": {
"vscode": "^1.45.0"
Expand Down Expand Up @@ -35,7 +35,12 @@
"configuration": {
"title": "Markdown Links",
"properties": {
"markdown-links.column": {
"markdown-links.showColumn": {
"type": "string",
"default": "beside",
"description": "- active – in the currently focused column.\n- beside – (default) other than the current.\n- one, two, three, four, five, six, seven, eight, nine – respective editor columns."
},
"markdown-links.openColumn": {
"type": "string",
"default": "one",
"description": "- active – in the currently focused column.\n- beside – other than the current.\n- one (default), two, three, four, five, six, seven, eight, nine – respective editor columns."
Expand Down Expand Up @@ -73,8 +78,9 @@
"dependencies": {
"@types/md5": "^2.2.0",
"md5": "^2.2.1",
"remark-frontmatter": "^2.0.0",
"remark-parse": "^8.0.2",
"remark-wiki-link": "0.0.4",
"remark-wiki-link": "^0.0.4",
"unified": "^9.0.0"
}
}
51 changes: 34 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ import { TextDecoder } from "util";
import * as path from "path";
import * as unified from "unified";
import * as markdown from "remark-parse";
import * as md5 from "md5";
import * as wikiLinkPlugin from "remark-wiki-link";
import * as frontmatter from "remark-frontmatter";
import * as md5 from "md5";

type Edge = {
source: string;
target: string;
};

type Node = {
id: string;
path: string;
label: string;
};

// the first capturing group must return the id
const ID_REGEX = /(?:^|[^[])(\d{4}-\d{2}-\d{2}N\d+)/m;

const id = (path: string): string => md5(path);

let nodes: Node[] = [];
let edges: Edge[] = [];
let idToPath: Record<string, string> = {};
Expand Down Expand Up @@ -64,13 +68,21 @@ const idResolver = (id: string): string[] => {
}
};

const parser = unified()
.use(markdown)
.use(wikiLinkPlugin, { pageResolver: idResolver })
.use(frontmatter);

const parseFile = async (filePath: string) => {
const buffer = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath));
const content = new TextDecoder("utf-8").decode(buffer);
const ast: MarkdownNode = unified()
.use(markdown)
.use(wikiLinkPlugin, { pageResolver: idResolver })
.parse(content);
const ast: MarkdownNode = parser.parse(content);

console.log(filePath, ast);

// TODO:
// - parse that YAML
// - wiki links?

let title: string | null = null;
if (
Expand All @@ -92,7 +104,7 @@ const parseFile = async (filePath: string) => {
if (index !== -1) {
nodes[index].label = title;
} else {
nodes.push({ path: filePath, label: title });
nodes.push({ id: id(filePath), path: filePath, label: title });
}

// remove edges based on an old version of this file
Expand All @@ -103,12 +115,11 @@ const parseFile = async (filePath: string) => {
for (const link of links) {
let target = link;
if (!path.isAbsolute(link)) {
target = path.normalize(
`${filePath.split("/").slice(0, -1).join("/")}/${link}`
);
const parentDirectory = filePath.split("/").slice(0, -1).join("/");
target = path.normalize(`${parentDirectory}/${link}`);
}

edges.push({ source: filePath, target });
edges.push({ source: id(filePath), target: id(target) });
}
};

Expand Down Expand Up @@ -167,7 +178,7 @@ const parseDirectoryForLinks = async (directory: string) => {
return await parseDirectory(directory, parseFile);
};

const exists = (path: string) => !!nodes.find((node) => node.path === path);
const exists = (id: string) => !!nodes.find((node) => node.id === id);

const filterNonExistingEdges = () => {
edges = edges.filter((edge) => exists(edge.source) && exists(edge.target));
Expand All @@ -187,6 +198,11 @@ const settingToValue: { [key: string]: vscode.ViewColumn | undefined } = {
nine: 9,
};

const getColumnSetting = (key: string) => {
const column = vscode.workspace.getConfiguration("markdown-links")[key];
return settingToValue[column] || vscode.ViewColumn.One;
};

const watch = (
context: vscode.ExtensionContext,
panel: vscode.WebviewPanel
Expand Down Expand Up @@ -259,10 +275,12 @@ const watch = (

panel.webview.onDidReceiveMessage(
(message) => {
if (message.type) {
if (message.type === "click") {
const openPath = vscode.Uri.file(message.payload.path);
const column = getColumnSetting("openColumn");

vscode.workspace.openTextDocument(openPath).then((doc) => {
vscode.window.showTextDocument(doc, vscode.ViewColumn.One);
vscode.window.showTextDocument(doc, column);
});
}
},
Expand All @@ -276,15 +294,14 @@ const watch = (
};

export function activate(context: vscode.ExtensionContext) {
const { column } = vscode.workspace.getConfiguration("markdown-links");
const setting = settingToValue[column] || vscode.ViewColumn.One;

context.subscriptions.push(
vscode.commands.registerCommand("markdown-links.showGraph", async () => {
const column = getColumnSetting("showColumn");

const panel = vscode.window.createWebviewPanel(
"markdownLinks",
"Markdown Links",
setting,
column,
{
enableScripts: true,
retainContextWhenHidden: true,
Expand Down
12 changes: 6 additions & 6 deletions static/webview.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@

const map = new Map();
for (const node of previous) {
map.set(node.path, node.label);
map.set(node.id, node.label);
}

for (const node of next) {
const found = map.get(node.path);
const found = map.get(node.id);
if (!found || found !== node.title) {
return false;
}
Expand All @@ -86,7 +86,7 @@

const set = new Set();
for (const edge of previous) {
set.add(`${edge.source.path}-${edge.target.path}`);
set.add(`${edge.source.id}-${edge.target.id}`);
}

for (const edge of next) {
Expand Down Expand Up @@ -144,7 +144,7 @@
"link",
d3
.forceLink(linksData)
.id((d) => d.path)
.id((d) => d.id)
.distance(70)
)
.force("center", d3.forceCenter(width / 2, height / 2))
Expand Down Expand Up @@ -185,7 +185,7 @@
};

const restart = () => {
node = node.data(nodesData, (d) => d.path);
node = node.data(nodesData, (d) => d.id);
node.exit().remove();
node = node
.enter()
Expand All @@ -194,7 +194,7 @@
.on("click", onClick)
.merge(node);

link = link.data(linksData, (d) => `${d.source.path}-${d.target.path}`);
link = link.data(linksData, (d) => `${d.source.id}-${d.target.id}`);
link.exit().remove();
link = link
.enter()
Expand Down
35 changes: 15 additions & 20 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "src",
"noImplicitAny": false,
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
},
"exclude": [
"node_modules",
".vscode-test"
]
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": ["es6"],
"sourceMap": true,
"rootDir": "src",
"noImplicitAny": false,
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
},
"exclude": ["node_modules", ".vscode-test"]
}
Loading

0 comments on commit a1fb2b1

Please sign in to comment.