Skip to content

Commit

Permalink
Added support for plus sign as a list marker
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Apr 19, 2021
1 parent 8122cc5 commit 0cb7fac
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
4 changes: 4 additions & 0 deletions jest/global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ async function prepareVault() {
}

module.exports = async () => {
if (process.env.SKIP_OBSIDIAN) {
return;
}

await prepareObsidian();
await prepareVault();

Expand Down
75 changes: 75 additions & 0 deletions src/__tests__/list_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ListUtils } from "../list_utils";

interface EditorMockParams {
text: string;
cursor: { line: number; ch: number };
}

function makeEditor(params: EditorMockParams) {
let text = params.text;
let cursor = { ...params.cursor };

const editor = {
getCursor: () => cursor,
getLine: (l: number) => text.split("\n")[l],
lastLine: () => text.split("\n").length - 1,
lineCount: () => text.split("\n").length,
isFolded: (l: number) => false,
};

return editor;
}

function makeListUtils() {
const logger: any = {
bind: jest.fn().mockReturnValue(jest.fn()),
};
const obsidianUtils: any = {
getObsidianTabsSettigns: jest
.fn()
.mockReturnValue({ useTab: true, tabSize: 4 }),
};

const listUtils = new ListUtils(logger, obsidianUtils);

return listUtils;
}

test("parse list with dash bullet", () => {
const listUtils = makeListUtils();
const editor = makeEditor({
text: "- qwe",
cursor: { line: 0, ch: 0 },
});

const list = listUtils.parseList(editor as any);

expect(list).toBeDefined();
expect(list.print()).toBe(`- qwe`);
});

test("parse list with asterisk bullet", () => {
const listUtils = makeListUtils();
const editor = makeEditor({
text: "* qwe",
cursor: { line: 0, ch: 0 },
});

const list = listUtils.parseList(editor as any);

expect(list).toBeDefined();
expect(list.print()).toBe(`* qwe`);
});

test("parse list with plus bullet", () => {
const listUtils = makeListUtils();
const editor = makeEditor({
text: "+ qwe",
cursor: { line: 0, ch: 0 },
});

const list = listUtils.parseList(editor as any);

expect(list).toBeDefined();
expect(list.print()).toBe(`+ qwe`);
});
10 changes: 6 additions & 4 deletions src/list_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Logger } from "./logger";
import { ObsidianUtils } from "./obsidian_utils";
import { IList, List, Root } from "./root";

const bulletSign = "-*+";

export class ListUtils {
constructor(private logger: Logger, private obsidianUtils: ObsidianUtils) {}

getListLineInfo(line: string, indentSign: string) {
const prefixRe = new RegExp(`^(?:${indentSign})*([-*]) `);
const prefixRe = new RegExp(`^(?:${indentSign})*([${bulletSign}]) `);
const matches = prefixRe.exec(line);

if (!matches) {
Expand Down Expand Up @@ -159,9 +161,9 @@ export class ListUtils {

const line = editor.getLine(cursor.line);

const withTabsRe = /^\t+[-*] /;
const withSpacesRe = /^[ ]+[-*] /;
const mayBeWithSpacesRe = /^[ ]*[-*] /;
const withTabsRe = new RegExp(`^\t+[${bulletSign}] `);
const withSpacesRe = new RegExp(`^[ ]+[${bulletSign}] `);
const mayBeWithSpacesRe = new RegExp(`^[ ]*[${bulletSign}] `);

if (withTabsRe.test(line)) {
d("detected tab on current line");
Expand Down

0 comments on commit 0cb7fac

Please sign in to comment.