#333 - collection and published field support

This commit is contained in:
Elio Struyf
2022-05-13 08:18:42 +02:00
parent 4c97993c5f
commit e78069ad17
2 changed files with 36 additions and 6 deletions
+7
View File
@@ -33,8 +33,15 @@ export default function usePages(pages: Page[]) {
// Framework specific actions
if (framework?.toLowerCase() === "jekyll") {
pagesToShow = pagesToShow.map(page => {
// https://jekyllrb.com/docs/posts/#drafts
const filePath = parseWinPath(page.fmFilePath);
page.draft = filePath.indexOf(`/_drafts/`) > -1;
// Published field: https://jekyllrb.com/docs/front-matter/#predefined-global-variables
if (typeof page.published !== "undefined") {
page.draft = !page.published;
}
return page;
});
}
+29 -6
View File
@@ -1,10 +1,12 @@
import { existsSync, readFileSync } from "fs";
import jsyaml = require("js-yaml");
import { join, resolve } from "path";
import { commands, Uri } from "vscode";
import { Folders } from "../commands/Folders";
import { COMMAND_NAME } from "../constants";
import { FrameworkDetectors } from "../constants/FrameworkDetectors";
import { Framework } from "../models";
import { Logger } from "./Logger";
export class FrameworkDetector {
@@ -78,13 +80,32 @@ export class FrameworkDetector {
return undefined;
}
public static checkDefaultSettings(framework: Framework) {
const wsFolder = Folders.getWorkspaceFolder();
public static checkDefaultSettings(framework: Framework) {
if (framework.name.toLowerCase() === "jekyll") {
const draftsPath = join(wsFolder?.fsPath || "", "_drafts");
const postsPath = join(wsFolder?.fsPath || "", "_posts");
FrameworkDetector.jekyll();
}
}
private static jekyll() {
try {
const wsFolder = Folders.getWorkspaceFolder();
const jekyllConfig = join(wsFolder?.fsPath || "", '_config.yml');
let collectionDir = "";
if (existsSync(jekyllConfig)) {
const content = readFileSync(jekyllConfig, "utf8");
// Convert YAML to JSON
const config = jsyaml.safeLoad(content);
if (config.collections_dir) {
collectionDir = config.collections_dir;
}
}
const draftsPath = join(wsFolder?.fsPath || "", collectionDir, "_drafts");
const postsPath = join(wsFolder?.fsPath || "", collectionDir, "_posts");
if (existsSync(draftsPath)) {
const folderUri = Uri.file(draftsPath);
commands.executeCommand(COMMAND_NAME.registerFolder, {
@@ -92,7 +113,7 @@ export class FrameworkDetector {
path: folderUri
});
}
if (existsSync(postsPath)) {
const folderUri = Uri.file(postsPath);
commands.executeCommand(COMMAND_NAME.registerFolder, {
@@ -100,6 +121,8 @@ export class FrameworkDetector {
path: folderUri
});
}
} catch (e) {
Logger.error(`Something failed while processing your Jekyll configuration. ${(e as Error).message}`);
}
}
}