Skip to content

Commit

Permalink
[ #60 , extract tasks ]
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Nov 25, 2023
1 parent dfe0cb5 commit e88ded8
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions __mocks__/content/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ tags: tag1, tag2, tag3
# Welcome

[link](blog0.mdx)

- [] uncompleted task 1
- [ ] uncompleted task 2

- [x] completed task 1
- [X] completed task 2
37 changes: 37 additions & 0 deletions src/lib/parseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function parseFile(source: string, options?: ParsingOptions) {
// Links
const links = extractWikiLinks(ast, options);

const tasks = extractTasks(ast);
metadata.tasks = tasks;

return {
metadata,
links,
Expand Down Expand Up @@ -155,6 +158,40 @@ export const extractWikiLinks = (ast: Root, options?: ParsingOptions) => {
return wikiLinks;
};

export interface Task {
description: string;
checked: boolean;
}

export const extractTasks = (ast: Root) => {
const nodes = selectAll("*", ast);
const tasks: Task[] = [];
nodes.map((node: any) => {
if (node.type === "listItem") {
const description = recursivelyExtractText(node).trim();
const checked = node.checked;
if (checked !== null) {
tasks.push({
description,
checked,
});
}
}
});

return tasks;
};

function recursivelyExtractText(node) {

Check failure on line 185 in src/lib/parseFile.ts

View workflow job for this annotation

GitHub Actions / Run tests on Node 16

Parameter 'node' implicitly has an 'any' type.

Check failure on line 185 in src/lib/parseFile.ts

View workflow job for this annotation

GitHub Actions / Run tests on Node 18

Parameter 'node' implicitly has an 'any' type.
if (node.value) {
return node.value;
} else if (node.children) {
return node.children.map(recursivelyExtractText).join(" ");
} else {
return "";
}
}

// links = extractWikiLinks({
// source,
// // TODO pass slug instead of file path as hrefs/srcs are sluggified too
Expand Down
72 changes: 72 additions & 0 deletions src/tests/extractTasks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { extractTasks, processAST } from "../lib/parseFile";

const getTasksFromSource = (source: string) => {
const ast = processAST(source, {});
const tasks = extractTasks(ast);
return tasks;
};

describe("extractTasks", () => {
test("should extract uncompleted tasks from body", () => {
const tasks = getTasksFromSource(
"- [] uncompleted task 1\n- [ ] uncompleted task 2"
);
const expectedTasks = [
{ description: "uncompleted task 2", checked: false },
];
expect(tasks).toEqual(expectedTasks);
});

test("should extract completed tasks from body", () => {
const tasks = getTasksFromSource(
"- [x] completed task 1\n- [X] completed task 2"
);
const expectedTasks = [
{ description: "completed task 1", checked: true },
{ description: "completed task 2", checked: true },
];
expect(tasks).toEqual(expectedTasks);
});

test("should handle mixed completed and uncompleted tasks", () => {
const tasks = getTasksFromSource(
"- [x] completed task\n- [ ] uncompleted task"
);
const expectedTasks = [
{ description: "completed task", checked: true },
{ description: "uncompleted task", checked: false },
];
expect(tasks).toEqual(expectedTasks);
});

test("should handle tasks with leading and trailing spaces", () => {
const tasks = getTasksFromSource(
"- [x] completed task \n- [ ] uncompleted task "
);
const expectedTasks = [
{ description: "completed task", checked: true },
{ description: "uncompleted task", checked: false },
];
expect(tasks).toEqual(expectedTasks);
});

test("should handle tasks with different checkbox formats", () => {
const tasks = getTasksFromSource(
"- [x] task 1\n- [X] task 2\n- [ ] task 3"
);
const expectedTasks = [
{ description: "task 1", checked: true },
{ description: "task 2", checked: true },
{ description: "task 3", checked: false },
];
expect(tasks).toEqual(expectedTasks);
});

test("should handle tasks with special characters", () => {
const tasks = getTasksFromSource("- [x] task with $pecial character$");
const expectedTasks = [
{ description: "task with $pecial character$", checked: true },
];
expect(tasks).toEqual(expectedTasks);
});
});
10 changes: 10 additions & 0 deletions src/tests/parseFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tags: a, b, c
[[blog/Some Other Link]]
[[blog/Some Other Link|Page Alias]]
![[Some Image.png]]
- [ ] uncompleted task
- [x] completed task
`;

describe("parseFile", () => {
Expand All @@ -18,6 +20,10 @@ describe("parseFile", () => {
title: "Hello World",
authors: ["John Doe", "Jane Doe"],
tags: ["a", "b", "c"],
tasks: [
{ description: "uncompleted task", checked: false },
{ description: "completed task", checked: true },
],
};
const expectedLinks = [
{
Expand Down Expand Up @@ -63,6 +69,10 @@ describe("parseFile", () => {
title: "Hello World",
authors: ["John Doe", "Jane Doe"],
tags: ["a", "b", "c"],
tasks: [
{ description: "uncompleted task", checked: false },
{ description: "completed task", checked: true },
],
};
const expectedLinks = [
{
Expand Down
14 changes: 14 additions & 0 deletions src/tests/process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ describe("Can parse a file and get file info", () => {
expect(fileInfo.metadata).toEqual({
title: "Homepage",
tags: ["tag1", "tag2", "tag3"],
tasks: [
{
checked: false,
description: "uncompleted task 2",
},
{
checked: true,
description: "completed task 1",
},
{
checked: true,
description: "completed task 2",
},
],
});
expect(fileInfo.links).toEqual([
{
Expand Down

0 comments on commit e88ded8

Please sign in to comment.