mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-07-06 09:51:29 +02:00
#6 - TOML support added
This commit is contained in:
@@ -4,6 +4,7 @@ import * as fs from 'fs';
|
||||
import { TaxonomyType } from "../models";
|
||||
import { CONFIG_KEY, SETTING_TAXONOMY_TAGS, SETTING_TAXONOMY_CATEGORIES, EXTENSION_NAME } from '../constants';
|
||||
import { ArticleHelper, SettingsHelper, FilesHelper } from '../helpers';
|
||||
import { FMLanguage, TomlEngine } from '../helpers/TomlEngine';
|
||||
|
||||
export class Settings {
|
||||
|
||||
@@ -98,7 +99,10 @@ export class Settings {
|
||||
const txtData = mdFile.getText();
|
||||
if (txtData) {
|
||||
try {
|
||||
const article = matter(txtData);
|
||||
const article = matter(txtData, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
});
|
||||
if (article && article.data) {
|
||||
const { data } = article;
|
||||
const mdTags = data["tags"];
|
||||
@@ -214,7 +218,10 @@ export class Settings {
|
||||
const mdFile = fs.readFileSync(file.path, { encoding: "utf8" });
|
||||
if (mdFile) {
|
||||
try {
|
||||
const article = matter(mdFile);
|
||||
const article = matter(mdFile, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
});
|
||||
if (article && article.data) {
|
||||
const { data } = article;
|
||||
let taxonomies: string[] = data[matterProp];
|
||||
@@ -228,7 +235,10 @@ export class Settings {
|
||||
}
|
||||
data[matterProp] = [...new Set(taxonomies)].sort();
|
||||
// Update the file
|
||||
fs.writeFileSync(file.path, matter.stringify(article.content, article.data), { encoding: "utf8" });
|
||||
fs.writeFileSync(file.path, matter.stringify(article.content, article.data, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
}), { encoding: "utf8" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ export const SETTING_SLUG_PREFIX = "taxonomy.slugPrefix";
|
||||
export const SETTING_SLUG_SUFFIX = "taxonomy.slugSuffix";
|
||||
|
||||
export const SETTING_INDENT_ARRAY = "taxonomy.indentArrays";
|
||||
export const SETTING_REMOVE_QUOTES = "taxonomy.noPropertyValueQuotes";
|
||||
export const SETTING_REMOVE_QUOTES = "taxonomy.noPropertyValueQuotes";
|
||||
|
||||
export const SETTING_FRONTMATTER_TYPE = "taxonomy.frontMatterType";
|
||||
@@ -2,8 +2,10 @@ import * as vscode from 'vscode';
|
||||
import * as matter from "gray-matter";
|
||||
import { stopWords } from '../constants/stopwords-en';
|
||||
import { charMap } from '../constants/charMap';
|
||||
import { CONFIG_KEY, SETTING_INDENT_ARRAY, SETTING_DATE_FORMAT, SETTING_REMOVE_QUOTES } from '../constants';
|
||||
import { CONFIG_KEY, SETTING_INDENT_ARRAY, SETTING_DATE_FORMAT, SETTING_REMOVE_QUOTES, SETTING_FRONTMATTER_TYPE } from '../constants';
|
||||
import { DumpOptions } from 'js-yaml';
|
||||
import { TomlEngine, FMLanguage } from './TomlEngine';
|
||||
|
||||
|
||||
export class ArticleHelper {
|
||||
|
||||
@@ -12,8 +14,13 @@ export class ArticleHelper {
|
||||
*
|
||||
* @param editor
|
||||
*/
|
||||
public static getFrontMatter(editor: vscode.TextEditor) {
|
||||
const article = matter(editor.document.getText());
|
||||
public static getFrontMatter(editor: vscode.TextEditor) {
|
||||
const language = FMLanguage()
|
||||
let article: matter.GrayMatterFile<string> | null = matter(editor.document.getText(), {
|
||||
...TomlEngine,
|
||||
language
|
||||
});
|
||||
|
||||
if (article && article.data) {
|
||||
return article;
|
||||
}
|
||||
@@ -30,8 +37,10 @@ export class ArticleHelper {
|
||||
const config = vscode.workspace.getConfiguration(CONFIG_KEY);
|
||||
const indentArray = config.get(SETTING_INDENT_ARRAY) as boolean;
|
||||
const removeQuotes = config.get(SETTING_REMOVE_QUOTES) as string[];
|
||||
|
||||
|
||||
let newMarkdown = matter.stringify(article.content, article.data, ({
|
||||
...TomlEngine,
|
||||
language: FMLanguage(),
|
||||
noArrayIndent: !indentArray
|
||||
} as DumpOptions as any));
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as toml from '@iarna/toml';
|
||||
import { CONFIG_KEY, SETTING_FRONTMATTER_TYPE } from '../constants';
|
||||
|
||||
export const FMLanguage = () => {
|
||||
const config = vscode.workspace.getConfiguration(CONFIG_KEY);
|
||||
const language = config.get(SETTING_FRONTMATTER_TYPE) as string || "YAML";
|
||||
return language.toLowerCase();
|
||||
};
|
||||
|
||||
export const TomlEngine = {
|
||||
engines: {
|
||||
toml: {
|
||||
parse: (value: string) => {
|
||||
return toml.parse(value);
|
||||
},
|
||||
stringify: (value: any) => {
|
||||
return toml.stringify(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user