Feature/toml (#88)
* Restore toml support * Fix get all entries on test backend
This commit is contained in:
parent
dcd0c92d5f
commit
d019fd915f
@ -15,6 +15,10 @@
|
|||||||
content:
|
content:
|
||||||
'{\n"title": "This is a JSON front matter post",\n"image": "/nf-logo.png",\n"date": "2015-02-15T00:00:00.000Z"\n}\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n',
|
'{\n"title": "This is a JSON front matter post",\n"image": "/nf-logo.png",\n"date": "2015-02-15T00:00:00.000Z"\n}\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n',
|
||||||
},
|
},
|
||||||
|
'2015-02-16-this-is-a-toml-frontmatter-post.md': {
|
||||||
|
content:
|
||||||
|
'+++\ntitle = "This is a TOML front matter post"\nimage = "/nf-logo.png"\n"date" = "2015-02-16T00:00:00.000Z"\n+++\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n',
|
||||||
|
},
|
||||||
'2015-02-14-this-is-a-post-with-a-different-extension.other': {
|
'2015-02-14-this-is-a-post-with-a-different-extension.other': {
|
||||||
content:
|
content:
|
||||||
'---\ntitle: This post should not appear because the extension is different\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n',
|
'---\ntitle: This post should not appear because the extension is different\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n',
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@staticcms/core",
|
"name": "@staticcms/core",
|
||||||
"version": "1.0.0-beta1",
|
"version": "1.0.0-beta2",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "Static CMS core application.",
|
"description": "Static CMS core application.",
|
||||||
"repository": "https://github.com/StaticJsCMS/static-cms",
|
"repository": "https://github.com/StaticJsCMS/static-cms",
|
||||||
@ -45,6 +45,7 @@
|
|||||||
"@emotion/css": "11.10.0",
|
"@emotion/css": "11.10.0",
|
||||||
"@emotion/react": "11.10.4",
|
"@emotion/react": "11.10.4",
|
||||||
"@emotion/styled": "11.10.4",
|
"@emotion/styled": "11.10.4",
|
||||||
|
"@ltd/j-toml": "1.35.2",
|
||||||
"@mui/icons-material": "5.10.6",
|
"@mui/icons-material": "5.10.6",
|
||||||
"@mui/material": "5.10.10",
|
"@mui/material": "5.10.10",
|
||||||
"@mui/system": "5.4.1",
|
"@mui/system": "5.4.1",
|
||||||
|
@ -283,7 +283,14 @@ export default class TestBackend implements BackendClass {
|
|||||||
extension: string,
|
extension: string,
|
||||||
depth: number,
|
depth: number,
|
||||||
): Promise<ImplementationEntry[]> {
|
): Promise<ImplementationEntry[]> {
|
||||||
return this.entriesByFolder(folder, extension, depth);
|
const files = folder ? getFolderFiles(window.repoFiles, folder, extension, depth) : [];
|
||||||
|
|
||||||
|
const entries = files.map(f => ({
|
||||||
|
data: f.content as string,
|
||||||
|
file: { path: f.path, id: f.path },
|
||||||
|
}));
|
||||||
|
|
||||||
|
return Promise.resolve(entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
getMediaDisplayURL(_displayURL: DisplayURL): Promise<string> {
|
getMediaDisplayURL(_displayURL: DisplayURL): Promise<string> {
|
||||||
|
18
core/src/formats/TomlFormatter.ts
Normal file
18
core/src/formats/TomlFormatter.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import toml from '@ltd/j-toml';
|
||||||
|
|
||||||
|
import { FileFormatter } from './FileFormatter';
|
||||||
|
|
||||||
|
class TomlFormatter extends FileFormatter {
|
||||||
|
fromFile(content: string) {
|
||||||
|
return toml.parse(content) as object;
|
||||||
|
}
|
||||||
|
|
||||||
|
toFile(data: object): string {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
return toml.stringify(data as any, {
|
||||||
|
newline: '\n',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new TomlFormatter();
|
@ -1,25 +1,29 @@
|
|||||||
import YamlFormatter from './YamlFormatter';
|
import YamlFormatter from './YamlFormatter';
|
||||||
|
import TomlFormatter from './TomlFormatter';
|
||||||
import JsonFormatter from './JsonFormatter';
|
import JsonFormatter from './JsonFormatter';
|
||||||
import { FrontmatterInfer, frontmatterJSON, frontmatterYAML } from './frontmatter';
|
import { FrontmatterInfer, frontmatterJSON, frontmatterTOML, frontmatterYAML } from './frontmatter';
|
||||||
|
|
||||||
import type { Delimiter } from './frontmatter';
|
import type { Delimiter } from './frontmatter';
|
||||||
import type { Collection, Entry, Format } from '../interface';
|
import type { Collection, Entry, Format } from '../interface';
|
||||||
import type { FileFormatter } from './FileFormatter';
|
import type { FileFormatter } from './FileFormatter';
|
||||||
|
|
||||||
export const frontmatterFormats = ['yaml-frontmatter', 'json-frontmatter'];
|
export const frontmatterFormats = ['yaml-frontmatter', 'toml-frontmatter', 'json-frontmatter'];
|
||||||
|
|
||||||
export const formatExtensions = {
|
export const formatExtensions = {
|
||||||
yml: 'yml',
|
yml: 'yml',
|
||||||
yaml: 'yml',
|
yaml: 'yml',
|
||||||
|
toml: 'toml',
|
||||||
json: 'json',
|
json: 'json',
|
||||||
frontmatter: 'md',
|
frontmatter: 'md',
|
||||||
'json-frontmatter': 'md',
|
'json-frontmatter': 'md',
|
||||||
|
'toml-frontmatter': 'md',
|
||||||
'yaml-frontmatter': 'md',
|
'yaml-frontmatter': 'md',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const extensionFormatters: Record<string, FileFormatter> = {
|
export const extensionFormatters: Record<string, FileFormatter> = {
|
||||||
yml: YamlFormatter,
|
yml: YamlFormatter,
|
||||||
yaml: YamlFormatter,
|
yaml: YamlFormatter,
|
||||||
|
toml: TomlFormatter,
|
||||||
json: JsonFormatter,
|
json: JsonFormatter,
|
||||||
md: FrontmatterInfer,
|
md: FrontmatterInfer,
|
||||||
markdown: FrontmatterInfer,
|
markdown: FrontmatterInfer,
|
||||||
@ -30,9 +34,11 @@ function formatByName(name: Format, customDelimiter?: Delimiter): FileFormatter
|
|||||||
const fileFormatter: Record<string, FileFormatter> = {
|
const fileFormatter: Record<string, FileFormatter> = {
|
||||||
yml: YamlFormatter,
|
yml: YamlFormatter,
|
||||||
yaml: YamlFormatter,
|
yaml: YamlFormatter,
|
||||||
|
toml: TomlFormatter,
|
||||||
json: JsonFormatter,
|
json: JsonFormatter,
|
||||||
frontmatter: FrontmatterInfer,
|
frontmatter: FrontmatterInfer,
|
||||||
'json-frontmatter': frontmatterJSON(customDelimiter),
|
'json-frontmatter': frontmatterJSON(customDelimiter),
|
||||||
|
'toml-frontmatter': frontmatterTOML(customDelimiter),
|
||||||
'yaml-frontmatter': frontmatterYAML(customDelimiter),
|
'yaml-frontmatter': frontmatterYAML(customDelimiter),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import matter from 'gray-matter';
|
import matter from 'gray-matter';
|
||||||
|
|
||||||
import YamlFormatter from './YamlFormatter';
|
import YamlFormatter from './YamlFormatter';
|
||||||
|
import TomlFormatter from './TomlFormatter';
|
||||||
import JsonFormatter from './JsonFormatter';
|
import JsonFormatter from './JsonFormatter';
|
||||||
import { FileFormatter } from './FileFormatter';
|
import { FileFormatter } from './FileFormatter';
|
||||||
|
|
||||||
const Languages = {
|
const Languages = {
|
||||||
YAML: 'yaml',
|
YAML: 'yaml',
|
||||||
|
TOML: 'toml',
|
||||||
JSON: 'json',
|
JSON: 'json',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
@ -15,6 +17,12 @@ export type Delimiter = string | [string, string];
|
|||||||
type Format = { language: Language; delimiters: Delimiter };
|
type Format = { language: Language; delimiters: Delimiter };
|
||||||
|
|
||||||
const parsers = {
|
const parsers = {
|
||||||
|
toml: {
|
||||||
|
parse: (input: string) => TomlFormatter.fromFile(input),
|
||||||
|
stringify: (metadata: object) => {
|
||||||
|
return TomlFormatter.toFile(metadata);
|
||||||
|
},
|
||||||
|
},
|
||||||
json: {
|
json: {
|
||||||
parse: (input: string) => {
|
parse: (input: string) => {
|
||||||
let JSONinput = input.trim();
|
let JSONinput = input.trim();
|
||||||
@ -54,6 +62,8 @@ function inferFrontmatterFormat(str: string) {
|
|||||||
switch (firstLine) {
|
switch (firstLine) {
|
||||||
case '---':
|
case '---':
|
||||||
return getFormatOpts(Languages.YAML);
|
return getFormatOpts(Languages.YAML);
|
||||||
|
case '+++':
|
||||||
|
return getFormatOpts(Languages.TOML);
|
||||||
case '{':
|
case '{':
|
||||||
return getFormatOpts(Languages.JSON);
|
return getFormatOpts(Languages.JSON);
|
||||||
default:
|
default:
|
||||||
@ -68,6 +78,7 @@ export function getFormatOpts(format?: Language, customDelimiter?: Delimiter) {
|
|||||||
|
|
||||||
const formats: { [key in Language]: Format } = {
|
const formats: { [key in Language]: Format } = {
|
||||||
yaml: { language: Languages.YAML, delimiters: '---' },
|
yaml: { language: Languages.YAML, delimiters: '---' },
|
||||||
|
toml: { language: Languages.TOML, delimiters: '+++' },
|
||||||
json: { language: Languages.JSON, delimiters: ['{', '}'] },
|
json: { language: Languages.JSON, delimiters: ['{', '}'] },
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -126,6 +137,10 @@ export class FrontmatterFormatter extends FileFormatter {
|
|||||||
|
|
||||||
export const FrontmatterInfer = new FrontmatterFormatter();
|
export const FrontmatterInfer = new FrontmatterFormatter();
|
||||||
|
|
||||||
|
export function frontmatterTOML(customDelimiter?: Delimiter) {
|
||||||
|
return new FrontmatterFormatter(Languages.TOML, customDelimiter);
|
||||||
|
}
|
||||||
|
|
||||||
export function frontmatterYAML(customDelimiter?: Delimiter) {
|
export function frontmatterYAML(customDelimiter?: Delimiter) {
|
||||||
return new FrontmatterFormatter(Languages.YAML, customDelimiter);
|
return new FrontmatterFormatter(Languages.YAML, customDelimiter);
|
||||||
}
|
}
|
||||||
|
@ -1614,6 +1614,11 @@
|
|||||||
resolved "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz"
|
resolved "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz"
|
||||||
integrity sha512-Hcv+nVC0kZnQ3tD9GVu5xSMR4VVYOteQIr/hwFPVEvPdlXqgGEuRjiheChHgdM+JyqdgNcmzZOX/tnl0JOiI7A==
|
integrity sha512-Hcv+nVC0kZnQ3tD9GVu5xSMR4VVYOteQIr/hwFPVEvPdlXqgGEuRjiheChHgdM+JyqdgNcmzZOX/tnl0JOiI7A==
|
||||||
|
|
||||||
|
"@ltd/j-toml@1.35.2":
|
||||||
|
version "1.35.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/@ltd/j-toml/-/j-toml-1.35.2.tgz#bd3532c9db4cb7d585d492b3eaf5f33c57981a9e"
|
||||||
|
integrity sha512-vhRlbbbp61PE/209BO14wRvgldF34lplcmVaNZnKU2QRwOfc+0NhD3S0hdMBRw1d3cvU9EC67tuV7MNRiVDNyg==
|
||||||
|
|
||||||
"@mapbox/jsonlint-lines-primitives@~2.0.2":
|
"@mapbox/jsonlint-lines-primitives@~2.0.2":
|
||||||
version "2.0.2"
|
version "2.0.2"
|
||||||
resolved "https://registry.npmjs.org/@mapbox/jsonlint-lines-primitives/-/jsonlint-lines-primitives-2.0.2.tgz"
|
resolved "https://registry.npmjs.org/@mapbox/jsonlint-lines-primitives/-/jsonlint-lines-primitives-2.0.2.tgz"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user