Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: horizontal line folded as frontmatter #886

Merged
merged 1 commit into from Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,8 @@
import { NoteUtils } from "@dendronhq/common-all";
import vscode from "vscode";
import { MDUtilsV5, ProcMode } from "@dendronhq/engine-server";
import vscode, { FoldingRangeKind } from "vscode";
import visit from "unist-util-visit";
import _ from "lodash";
import { VSCodeUtils } from "../utils";

export default class FrontmatterFoldingRangeProvider
implements vscode.FoldingRangeProvider
Expand All @@ -12,15 +15,25 @@ export default class FrontmatterFoldingRangeProvider
public async provideFoldingRanges(
document: vscode.TextDocument
): Promise<vscode.FoldingRange[]> {
const content = document.getText();
const fmMatch = content.match(NoteUtils.RE_FM);
if (!fmMatch) {
return [];
}
const fmContent = fmMatch[0];
// we know fmContent cannot be null since fmMatch exists.
const fmContentLength = fmContent.match(/^/gm)!.length;
// there is fmContentLength lines, so the index of the last line is fmContentLength - 1
return [new vscode.FoldingRange(0, fmContentLength - 1)];
const proc = MDUtilsV5.procRemarkParse({
mode: ProcMode.NO_DATA,
parseOnly: true,
});
const parsed = proc.parse(document.getText());
let range: vscode.FoldingRange | undefined;
visit(parsed, ["yaml"], (node) => {
if (_.isUndefined(node.position)) return false; // Should never happen
range = new vscode.FoldingRange(
VSCodeUtils.point2VSCodePosition(node.position.start).line,
VSCodeUtils.point2VSCodePosition(node.position.end).line,
FoldingRangeKind.Region
);

// Found the frontmatter already, stop traversing
return false;
});

if (_.isUndefined(range)) return [];
return [range];
}
}
@@ -1,11 +1,16 @@
import { vault2Path } from "@dendronhq/common-server";
import { ENGINE_HOOKS } from "@dendronhq/engine-test-utils";
import { NoteTestUtilsV4 } from "@dendronhq/common-test-utils";
import path from "path";
import * as vscode from "vscode";
import FrontmatterFoldingRangeProvider from "../../features/FrontmatterFoldingRangeProvider";
import { VSCodeUtils } from "../../utils";
import { expect } from "../testUtilsv2";
import { runLegacySingleWorkspaceTest, setupBeforeAfter } from "../testUtilsV3";
import {
runLegacyMultiWorkspaceTest,
runLegacySingleWorkspaceTest,
setupBeforeAfter,
} from "../testUtilsV3";

async function provide(editor: vscode.TextEditor) {
const doc = editor?.document as vscode.TextDocument;
Expand Down Expand Up @@ -33,7 +38,40 @@ suite("FrontmatterFoldingRangeProvider", function () {
vscode.Uri.file(path.join(vaultDir, "foo.md"))
);
const foldingRange = (await provide(editor!)) as vscode.FoldingRange[];
expect(foldingRange).toEqual([new vscode.FoldingRange(0, 6)]);
expect(foldingRange).toEqual([
new vscode.FoldingRange(0, 6, vscode.FoldingRangeKind.Region),
]);
done();
},
});
});

test("with horizontal line", (done) => {
runLegacyMultiWorkspaceTest({
ctx,
preSetupHook: async ({ wsRoot, vaults }) => {
await NoteTestUtilsV4.createNote({
wsRoot,
vault: vaults[0],
fname: "foo",
body: [
"Doloremque illo exercitationem error ab. Dicta architecto quis voluptatem. Numquam in est voluptatem quia impedit iusto repellendus magnam.",
"",
"---",
"",
"Aperiam in cupiditate temporibus id voluptas qui. Qui doloremque error odio eligendi quia. Quis ipsa aliquid voluptatem sunt.",
].join("\n"),
});
},
onInit: async ({ wsRoot, vaults }) => {
const vaultDir = vault2Path({ vault: vaults[0], wsRoot });
const editor = await VSCodeUtils.openFileInEditor(
vscode.Uri.file(path.join(vaultDir, "foo.md"))
);
const foldingRange = (await provide(editor!)) as vscode.FoldingRange[];
expect(foldingRange).toEqual([
new vscode.FoldingRange(0, 6, vscode.FoldingRangeKind.Region),
]);
done();
},
});
Expand Down