Skip to content

Commit 85be465

Browse files
committed
fix: respect the config ordering used when adding a new unit
1 parent 9185aeb commit 85be465

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

extensions/vscode/src/commands/tutorialkit.add.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { cmd } from '.';
22
import { Node, NodeType } from '../models/Node';
33
import * as vscode from 'vscode';
44
import { FILES_FOLDER, SOLUTION_FOLDER } from '../models/tree/constants';
5+
import { updateNodeMetadataInVFS } from '../models/tree/update';
56

67
let kebabCase: (string: string) => string;
78
let capitalize: (string: string) => string;
@@ -56,10 +57,16 @@ async function getUnitName(unitType: NodeType, unitNumber: number) {
5657
async function createUnitFolder(parent: Node, unitType: NodeType) {
5758
const unitNumber = parent.children.length + 1;
5859
const unitName = await getUnitName(unitType, unitNumber);
59-
const unitFolderPath = `${unitNumber}-${kebabCase(unitName)}`;
60+
const unitFolderPath = parent.order ? kebabCase(unitName) : `${unitNumber}-${kebabCase(unitName)}`;
61+
6062
const metaFile = unitType === 'lesson' ? 'content.mdx' : 'meta.md';
6163
const metaFilePath = vscode.Uri.joinPath(parent.path, unitFolderPath, metaFile);
6264

65+
if (parent.order) {
66+
parent.pushChild(unitFolderPath);
67+
await updateNodeMetadataInVFS(parent);
68+
}
69+
6370
await vscode.workspace.fs.writeFile(
6471
metaFilePath,
6572
new TextEncoder().encode(`---\ntype: ${unitType}\ntitle: ${unitName}\n---\n`),

extensions/vscode/src/models/Node.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ export class Node {
5353
readonly path: vscode.Uri,
5454
private _customName?: string,
5555
) {}
56+
57+
pushChild(folderPath: string) {
58+
this.childCount += 1;
59+
60+
if (this.order) {
61+
this.order.set(folderPath, this.order.size);
62+
63+
switch (this.metadata?.type) {
64+
case 'chapter': {
65+
this.metadata.lessons!.push(folderPath);
66+
break;
67+
}
68+
case 'tutorial': {
69+
this.metadata.parts!.push(folderPath);
70+
break;
71+
}
72+
case 'part': {
73+
this.metadata.chapters!.push(folderPath);
74+
break;
75+
}
76+
}
77+
}
78+
}
5679
}
5780

5881
export type Metadata = PartSchema | ChapterSchema | LessonSchema | TutorialSchema;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
import grayMatter from 'gray-matter';
3+
import { Node } from '../Node';
4+
5+
export async function updateNodeMetadataInVFS(node: Node) {
6+
if (!node.metadata || !node.metadataFilePath) {
7+
return;
8+
}
9+
10+
const filePath = node.metadataFilePath;
11+
const document = vscode.workspace.textDocuments.find((document) => document.uri.toString() === filePath.toString());
12+
13+
const content = document ? document.getText() : await readContentAsString(filePath);
14+
15+
const parsedContent = grayMatter(content);
16+
const frontMatterEnd = content.length - parsedContent.content.length;
17+
const newMetadata = grayMatter.stringify('', node.metadata);
18+
19+
if (document) {
20+
const edit = new vscode.WorkspaceEdit();
21+
const range = new vscode.Range(document.positionAt(0), document.positionAt(frontMatterEnd));
22+
23+
edit.replace(filePath, range, newMetadata, { needsConfirmation: false, label: `Updated ${node.name}` });
24+
25+
await vscode.workspace.applyEdit(edit);
26+
} else {
27+
const newContent = new TextEncoder().encode(newMetadata + parsedContent.content);
28+
await vscode.workspace.fs.writeFile(filePath, newContent);
29+
}
30+
}
31+
32+
async function readContentAsString(filePath: vscode.Uri) {
33+
const binContent = await vscode.workspace.fs.readFile(filePath);
34+
35+
return new TextDecoder().decode(binContent);
36+
}

0 commit comments

Comments
 (0)