Skip to content

Commit

Permalink
Merge pull request #224 from estruyf/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf authored Jan 10, 2022
2 parents feff69d + 0039fc1 commit 511fd48
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 50 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log

## [5.10.0] - 2022-01-10

### 🎨 Enhancements

- [#218](https://github.com/estruyf/vscode-front-matter/issues/218): Add support for creating `mdx` files from templates and content types. This introduced a new setting: `frontMatter.content.defaultFileType`.
- [#220](https://github.com/estruyf/vscode-front-matter/issues/220): Add support DateTime updates in `mdx` files when the `mdx extension` is not installed.

### 🐞 Fixes

- [#221](https://github.com/estruyf/vscode-front-matter/issues/221): Automatic DateTime switch from on text change to on save to prevent multiple updates.

## [5.9.0] - 2022-01-01 - 🎇🎆

### 🎨 Enhancements
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Front Matter",
"description": "Front Matter is a CMS that runs within Visual Studio Code. It gives you the power and control of a full-blown CMS while also providing you the flexibility and speed of the static site generator of your choice like: Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...",
"icon": "assets/frontmatter-teal-128x128.png",
"version": "5.9.0",
"version": "5.10.0",
"preview": false,
"publisher": "eliostruyf",
"galleryBanner": {
Expand Down Expand Up @@ -97,6 +97,16 @@
"markdownDescription": "Specify if you want to automatically update the modified date of your article/page. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.content.autoupdatedate)",
"scope": "Content"
},
"frontMatter.content.defaultFileType": {
"type": "string",
"default": "md",
"enum": [
"md",
"mdx"
],
"markdownDescription": "Specify the default file type for the content to create. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.content.defaultfiletype)",
"scope": "Content"
},
"frontMatter.content.defaultSorting": {
"type": "string",
"default": "",
Expand Down Expand Up @@ -397,6 +407,15 @@
"type": "string",
"description": "Define the type of field"
},
"fileType": {
"type": "string",
"default": "",
"enum": [
"md",
"mdx"
],
"description": "Specifies the type of content you want to create."
},
"fields": {
"type": "array",
"description": "Define the fields of the content type",
Expand Down
67 changes: 40 additions & 27 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import { parseWinPath } from '../helpers/parseWinPath';


export class Article {

private static prevContent = "";

/**
* Insert taxonomy
*
Expand Down Expand Up @@ -119,7 +116,37 @@ export class Article {
return;
}

const article = ArticleHelper.getFrontMatter(editor);
const updatedArticle = this.setLastModifiedDateInner(editor.document);

if (typeof updatedArticle === "undefined") {
return;
}

ArticleHelper.update(
editor,
updatedArticle as matter.GrayMatterFile<string>
);
}

public static async setLastModifiedDateOnSave(
document: vscode.TextDocument
): Promise<vscode.TextEdit[]> {
const updatedArticle = this.setLastModifiedDateInner(document);

if (typeof updatedArticle === "undefined") {
return [];
}

const update = ArticleHelper.generateUpdate(document, updatedArticle);

return [update];
}

private static setLastModifiedDateInner(
document: vscode.TextDocument
): matter.GrayMatterFile<string> | undefined {
const article = ArticleHelper.getFrontMatterFromDocument(document);

if (!article) {
return;
}
Expand All @@ -128,8 +155,7 @@ export class Article {
const dateField = Settings.get(SETTING_MODIFIED_FIELD) as string || DefaultFields.LastModified;
try {
cloneArticle.data[dateField] = Article.formatDate(new Date());

ArticleHelper.update(editor, cloneArticle);
return cloneArticle;
} catch (e: any) {
Notifications.error(`Something failed while parsing the date format. Check your "${CONFIG_KEY}${SETTING_DATE_FORMAT}" setting.`);
}
Expand Down Expand Up @@ -238,30 +264,17 @@ export class Article {

/**
* Article auto updater
* @param fileChanges
* @param event
*/
public static async autoUpdate(fileChanges: vscode.TextDocumentChangeEvent) {
const txtChanges = fileChanges.contentChanges.map(c => c.text);
const editor = vscode.window.activeTextEditor;

if (txtChanges.length > 0 && editor && ArticleHelper.isMarkdownFile()) {
const autoUpdate = Settings.get(SETTING_AUTO_UPDATE_DATE);
public static async autoUpdate(event: vscode.TextDocumentWillSaveEvent) {
const document = event.document;
if (document && ArticleHelper.isMarkdownFile(document)) {
const autoUpdate = Settings.get(SETTING_AUTO_UPDATE_DATE);

if (autoUpdate) {
const article = ArticleHelper.getFrontMatter(editor);
if (!article) {
return;
}

if (article.content === Article.prevContent) {
return;
}

Article.prevContent = article.content;

Article.setLastModifiedDate();
if (autoUpdate) {
event.waitUntil(Article.setLastModifiedDateOnSave(document));
}
}
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/commands/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Notifications } from "../helpers/Notifications";
import { Template } from "./Template";
import { Folders } from "./Folders";
import { Settings } from "../helpers";
import { SETTINGS_CONTENT_DEFAULT_FILETYPE } from "../constants";

export class Project {

Expand All @@ -27,6 +28,7 @@ categories: []
public static async init(sampleTemplate: boolean = true) {
try {
Settings.createTeamSettings();
const fileType = Settings.get<string>(SETTINGS_CONTENT_DEFAULT_FILETYPE);

const folder = Template.getSettings();
const templatePath = Project.templatePath();
Expand All @@ -35,7 +37,7 @@ categories: []
return;
}

const article = Uri.file(join(templatePath.fsPath, "article.md"));
const article = Uri.file(join(templatePath.fsPath, `article.${fileType}`));

if (!fs.existsSync(templatePath.fsPath)) {
await workspace.fs.createDirectory(templatePath);
Expand Down
9 changes: 6 additions & 3 deletions src/commands/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Questions } from './../helpers/Questions';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { SETTING_TEMPLATES_FOLDER, SETTING_TEMPLATES_PREFIX } from '../constants';
import { SETTINGS_CONTENT_DEFAULT_FILETYPE, SETTING_TEMPLATES_FOLDER, SETTING_TEMPLATES_PREFIX } from '../constants';
import { ArticleHelper, Settings } from '../helpers';
import { Article } from '.';
import { Notifications } from '../helpers/Notifications';
Expand All @@ -12,6 +12,7 @@ import { Folders } from './Folders';
import { ContentType } from '../helpers/ContentType';
import { ContentType as IContentType } from '../models';
import { PagesListener } from '../listeners';
import { extname } from 'path';

export class Template {

Expand Down Expand Up @@ -50,6 +51,7 @@ export class Template {
public static async generate() {
const folder = Template.getSettings();
const editor = vscode.window.activeTextEditor;
const fileType = Settings.get<string>(SETTINGS_CONTENT_DEFAULT_FILETYPE);

if (folder && editor && ArticleHelper.isMarkdownFile()) {
const article = ArticleHelper.getFrontMatter(editor);
Expand Down Expand Up @@ -83,7 +85,7 @@ export class Template {
if (templatePath) {
let fileContents = ArticleHelper.stringifyFrontMatter(keepContents === "no" ? "" : clonedArticle.content, clonedArticle.data);

const templateFile = path.join(templatePath.fsPath, `${titleValue}.md`);
const templateFile = path.join(templatePath.fsPath, `${titleValue}.${fileType}`);
fs.writeFileSync(templateFile, fileContents, { encoding: "utf-8" });

Notifications.info(`Template created and is now available in your ${folder} folder.`);
Expand Down Expand Up @@ -140,7 +142,8 @@ export class Template {
contentType = contentTypes?.find(t => t.name === templateData.data.type);
}

let newFilePath: string | undefined = ArticleHelper.createContent(contentType, folderPath, titleValue);
const fileExtension = extname(template.fsPath).replace(".", "");
let newFilePath: string | undefined = ArticleHelper.createContent(contentType, folderPath, titleValue, fileExtension);
if (!newFilePath) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const SETTINGS_CONTENT_WYSIWYG = "content.wysiwyg";
export const SETTINGS_CONTENT_SORTING_DEFAULT = "content.defaultSorting";
export const SETTINGS_MEDIA_SORTING_DEFAULT = "content.defaultSorting";

export const SETTINGS_CONTENT_DEFAULT_FILETYPE = "content.defaultFileType";

export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart";
export const SETTINGS_DASHBOARD_MEDIA_SNIPPET = "dashboard.mediaSnippet";

Expand Down
7 changes: 3 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ export async function activate(context: vscode.ExtensionContext) {
triggerShowDraftStatus();

// Listener for file edit changes
editDebounce = debounceCallback();
subscriptions.push(vscode.workspace.onDidChangeTextDocument(triggerFileChange));
subscriptions.push(vscode.workspace.onWillSaveTextDocument(handleAutoDateUpdate));

// Listener for file saves
subscriptions.push(vscode.workspace.onDidSaveTextDocument((doc: vscode.TextDocument) => {
Expand Down Expand Up @@ -231,8 +230,8 @@ export async function activate(context: vscode.ExtensionContext) {

export function deactivate() {}

const triggerFileChange = (e: vscode.TextDocumentChangeEvent) => {
editDebounce(() => Article.autoUpdate(e), 1000);
const handleAutoDateUpdate = (e: vscode.TextDocumentWillSaveEvent) => {
Article.autoUpdate(e);
};

const triggerShowDraftStatus = () => {
Expand Down
Loading

0 comments on commit 511fd48

Please sign in to comment.