Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/eight-dots-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@tinacms/schema-tools": minor
"@tinacms/graphql": minor
---

Collections - added ability to make yaml max line width configurable.
This was previously hard coded to 80 but now defaults to being disabled `-1`.
This is configurable though the collection's `yamlMaxLineWidth` prop.
1 change: 1 addition & 0 deletions packages/@tinacms/graphql/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ export class Database {
{
frontmatterFormat: collection?.frontmatterFormat,
frontmatterDelimiters: collection?.frontmatterDelimiters,
yamlMaxLineWidth: collection?.yamlMaxLineWidth,
}
);
};
Expand Down
57 changes: 57 additions & 0 deletions packages/@tinacms/graphql/src/database/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,62 @@ Content with components stored in frontmatter`;
expect(result).toContain('author = "Test Author"');
expect(result).toContain('Content here');
});

it('should not wrap long strings with folded scalar syntax by default (lineWidth: -1)', () => {
const content = {
title: 'Test Title',
longString:
'This is a very long string that exceeds eighty characters and would normally be wrapped with folded scalar syntax if lineWidth was set to 80',
_relativePath: 'test.md',
_id: 'test.md',
_template: 'post',
_collection: 'posts',
$_body: 'Content here',
};

const result = stringifyFile(content, '.md', false);

// Should NOT contain folded scalar syntax
expect(result).not.toContain('>-');
// Should contain the full string on one line in the frontmatter
expect(result).toContain('longString:');
});

it('should wrap long strings when lineWidth is explicitly set to 80', () => {
const content = {
title: 'Test Title',
longString:
'This is a very long string that exceeds 80 characters and should be wrapped with folded scalar syntax when lineWidth is set to 80',
_relativePath: 'test.md',
_id: 'test.md',
_template: 'post',
_collection: 'posts',
$_body: 'Content here',
};

const result = stringifyFile(content, '.md', false, {
yamlMaxLineWidth: 80,
});

// Should contain folded scalar syntax
expect(result).toContain('>-');
});

it('should handle lineWidth for standalone YAML files', () => {
const content = {
title: 'Test Title',
longString:
'This is a very long string that exceeds 80 characters and would normally be wrapped but should not be with default lineWidth of -1',
_relativePath: 'test.yaml',
_id: 'test.yaml',
_template: 'config',
_collection: 'configs',
};

const result = stringifyFile(content, '.yaml', false);

// Should NOT contain folded scalar syntax
expect(result).not.toContain('>-');
});
});
});
17 changes: 15 additions & 2 deletions packages/@tinacms/graphql/src/database/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const stringifyFile = (
markdownParseConfig?: {
frontmatterFormat?: ContentFrontmatterFormat;
frontmatterDelimiters?: [string, string] | string;
yamlMaxLineWidth?: number;
}
): string => {
const {
Expand Down Expand Up @@ -110,12 +111,21 @@ export const stringifyFile = (
case '.markdown':
case '.mdx':
case '.md':
const yamlMaxLineWidth = markdownParseConfig?.yamlMaxLineWidth ?? -1;
const enginesWithYaml = {
...matterEngines,
yaml: {
parse: (val) => yaml.safeLoad(val) as object,
stringify: (val) =>
yaml.safeDump(val, { lineWidth: yamlMaxLineWidth }),
},
};
const ok = matter.stringify(
typeof $_body === 'undefined' ? '' : `\n${$_body}`,
strippedContent,
{
language: markdownParseConfig?.frontmatterFormat ?? 'yaml',
engines: matterEngines,
engines: enginesWithYaml,
delimiters: markdownParseConfig?.frontmatterDelimiters ?? '---',
}
);
Expand All @@ -124,7 +134,9 @@ export const stringifyFile = (
return JSON.stringify(strippedContent, null, 2);
case '.yaml':
case '.yml':
return yaml.safeDump(strippedContent);
return yaml.safeDump(strippedContent, {
lineWidth: markdownParseConfig?.yamlMaxLineWidth ?? -1,
});
case '.toml':
return toml.stringify(strippedContent as any);
default:
Expand All @@ -138,6 +150,7 @@ export const parseFile = <T extends object>(
yupSchema: (args: typeof yup) => yup.ObjectSchema<any>,
markdownParseConfig?: {
frontmatterFormat?: ContentFrontmatterFormat;
lineWidth?: number;
frontmatterDelimiters?: [string, string] | string;
}
): T => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: Comprehensive Test Post by Mr Bob Northwind
content: >
This is a comprehensive test post created by Mr Bob Northwind for his
Northwind company.
content: |
This is a comprehensive test post created by Mr Bob Northwind for his Northwind company.
published: true
rating: 10
publishDate: 2023-12-01T00:00:00.000Z
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: Create Collection Test Post by Mr Bob Northwind
content: >
This is a comprehensive test post created by Mr Bob Northwind for his
Northwind company.
content: |
This is a comprehensive test post created by Mr Bob Northwind for his Northwind company.
published: true
rating: 10
publishDate: 2023-12-01T00:00:00.000Z
Expand Down
7 changes: 7 additions & 0 deletions packages/@tinacms/schema-tools/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@ interface BaseCollection {
* The delimiters used to parse the frontmatter.
*/
frontmatterDelimiters?: [string, string] | string;
/**
* The max line width used when serializing frontmatter for markdown and for content in YAML files.
* This sets the line length that reformats long strings with folded scalar syntax (>-).
* Setting to -1 disables line wrapping.
* @default -1
*/
yamlMaxLineWidth?: number;
match?: {
include?: string;
exclude?: string;
Expand Down
Loading