mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-06 08:52:47 +02:00
#10 - Fix for TOML delimiter
This commit is contained in:
@@ -4,7 +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';
|
||||
import { TomlEngine, getFmLanguage, getFormatOpts } from '../helpers/TomlEngine';
|
||||
|
||||
export class Settings {
|
||||
|
||||
@@ -91,6 +91,10 @@ export class Settings {
|
||||
const progressNr = allMdFiles.length/100;
|
||||
progress.report({ increment: 0});
|
||||
|
||||
// Get language options
|
||||
const language = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
|
||||
let i = 0;
|
||||
for (const file of allMdFiles) {
|
||||
progress.report({ increment: (++i/progressNr) });
|
||||
@@ -101,7 +105,7 @@ export class Settings {
|
||||
try {
|
||||
const article = matter(txtData, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
...langOpts
|
||||
});
|
||||
if (article && article.data) {
|
||||
const { data } = article;
|
||||
@@ -217,10 +221,12 @@ export class Settings {
|
||||
progress.report({ increment: (++i/progressNr) });
|
||||
const mdFile = fs.readFileSync(file.path, { encoding: "utf8" });
|
||||
if (mdFile) {
|
||||
const language = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
try {
|
||||
const article = matter(mdFile, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
...langOpts
|
||||
});
|
||||
if (article && article.data) {
|
||||
const { data } = article;
|
||||
@@ -237,7 +243,7 @@ export class Settings {
|
||||
// Update the file
|
||||
fs.writeFileSync(file.path, matter.stringify(article.content, article.data, {
|
||||
...TomlEngine,
|
||||
language: FMLanguage()
|
||||
...langOpts
|
||||
}), { encoding: "utf8" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@ 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, SETTING_FRONTMATTER_TYPE } from '../constants';
|
||||
import { CONFIG_KEY, SETTING_INDENT_ARRAY, SETTING_REMOVE_QUOTES } from '../constants';
|
||||
import { DumpOptions } from 'js-yaml';
|
||||
import { TomlEngine, FMLanguage } from './TomlEngine';
|
||||
|
||||
import { TomlEngine, getFmLanguage, getFormatOpts } from './TomlEngine';
|
||||
|
||||
export class ArticleHelper {
|
||||
|
||||
@@ -15,10 +14,11 @@ export class ArticleHelper {
|
||||
* @param editor
|
||||
*/
|
||||
public static getFrontMatter(editor: vscode.TextEditor) {
|
||||
const language = FMLanguage()
|
||||
const language: string = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
let article: matter.GrayMatterFile<string> | null = matter(editor.document.getText(), {
|
||||
...TomlEngine,
|
||||
language
|
||||
...langOpts
|
||||
});
|
||||
|
||||
if (article && article.data) {
|
||||
@@ -37,10 +37,12 @@ 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[];
|
||||
const language = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
|
||||
let newMarkdown = matter.stringify(article.content, article.data, ({
|
||||
...TomlEngine,
|
||||
language: FMLanguage(),
|
||||
...langOpts,
|
||||
noArrayIndent: !indentArray
|
||||
} as DumpOptions as any));
|
||||
|
||||
|
||||
@@ -2,12 +2,22 @@ import * as vscode from 'vscode';
|
||||
import * as toml from '@iarna/toml';
|
||||
import { CONFIG_KEY, SETTING_FRONTMATTER_TYPE } from '../constants';
|
||||
|
||||
export const FMLanguage = () => {
|
||||
export const getFmLanguage = (): string => {
|
||||
const config = vscode.workspace.getConfiguration(CONFIG_KEY);
|
||||
const language = config.get(SETTING_FRONTMATTER_TYPE) as string || "YAML";
|
||||
return language.toLowerCase();
|
||||
};
|
||||
|
||||
export const getFormatOpts = (format: string): { language: string, delimiters: string | [string, string] | undefined } => {
|
||||
const formats: { [prop: string]: { language: string, delimiters: string | [string, string] | undefined }} = {
|
||||
yaml: { language: 'yaml', delimiters: '---' },
|
||||
toml: { language: 'toml', delimiters: '+++' },
|
||||
json: { language: 'json', delimiters: ['{', '}'] },
|
||||
};
|
||||
|
||||
return formats[format];
|
||||
};
|
||||
|
||||
export const TomlEngine = {
|
||||
engines: {
|
||||
toml: {
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from './ArticleHelper';
|
||||
export * from './FilesHelper';
|
||||
export * from './SettingsHelper';
|
||||
export * from './StringHelpers';
|
||||
export * from './TomlEngine';
|
||||
|
||||
Reference in New Issue
Block a user