Skip to content

Commit

Permalink
feat: add tests for metadata transformations with substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
brotheroftux committed Jul 12, 2024
1 parent 8275680 commit bee51f5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/services/metadata/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {FileMetadata, serializeMetadata} from './parse';

// IMO, we should just always apply this at the end of the whole processing pipeline,
// not when dumping meta/front matter
const normalizeLineEndings = (input: string): string => input.replace(/\r?\n/g, carriageReturn);
export const normalizeLineEndings = (input: string): string =>
input.replace(/\r?\n/g, carriageReturn);

export const emplaceMetadata = (metadataStrippedContent: string, metadata: FileMetadata) =>
normalizeLineEndings(`${serializeMetadata(metadata)}${metadataStrippedContent}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Front matter (metadata) transformations do not break when a property key contains Liquid-style variable substitutions 1`] = `
{
"(({{ key-name }}))": "This one's key employs a different whitespace style in a substitution.",
"(({{key-name}}))": "This one's key only consists of a substitution.",
"(({{key-name}}))-prop": "This one's key starts with a substitution.",
"prop-(({{key-name}}))": "This one has a substitution in a key name.",
}
`;

exports[`Front matter (metadata) transformations do not break when a property key contains Liquid-style variable substitutions 2`] = `
"---
prop-{{key-name}}: This one has a substitution in a key name.
{{key-name}}-prop: This one's key starts with a substitution.
{{key-name}}: This one's key only consists of a substitution.
{{ key-name }}: This one's key employs a different whitespace style in a substitution.
---
Blah.
"
`;

exports[`Front matter (metadata) transformations do not break when a property value contains Liquid-style variable substitutions 1`] = `
{
"prop1": "This is a metadata property with a (({{substitution}})) in it.",
"prop2": "This one contains (({{multiple}})) (({{substitutions}})).",
"prop3": "This one has (({{substitutions}})) of (({{ different }})) (({{ styles}})).",
"prop4": "This one has a (({{substitution}})) as well, but the string literal is single-quoted.",
"prop5": "This one has no quotes at (({{all}})).",
"prop6": "(({{this}})) starts with a substitution.",
"prop7": "(({{this}})) one is a multiline (({{property}})).",
}
`;

exports[`Front matter (metadata) transformations do not break when a property value contains Liquid-style variable substitutions 2`] = `
"---
prop1: This is a metadata property with a {{substitution}} in it.
prop2: This one contains {{multiple}} {{substitutions}}.
prop3: This one has {{substitutions}} of {{ different }} {{ styles}}.
prop4: >-
This one has a {{substitution}} as well, but the string literal is
single-quoted.
prop5: This one has no quotes at {{all}}.
prop6: {{this}} starts with a substitution.
prop7: {{this}} one is a multiline {{property}}.
---
Blah.
"
`;
28 changes: 28 additions & 0 deletions tests/integrations/services/liquidInFrontMatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {readFile} from 'fs/promises';
import {parseExistingMetadata} from 'services/metadata/parse';
import {emplaceMetadata} from 'services/metadata/utils';

const propValuesMockPath = 'mocks/fileContent/metadata/substitutionsInMetadataPropertyValues.md';
const propKeysMockPath = 'mocks/fileContent/metadata/substitutionsInMetadataPropertyKeys.md';

describe('Front matter (metadata) transformations', () => {
it('do not break when a property value contains Liquid-style variable substitutions', async () => {
const fileContent = await readFile(propValuesMockPath, {encoding: 'utf-8'});

const {metadata, metadataStrippedContent} = parseExistingMetadata(fileContent);
const processedContent = emplaceMetadata(metadataStrippedContent, metadata);

expect(metadata).toMatchSnapshot();
expect(processedContent).toMatchSnapshot();
});

it('do not break when a property key contains Liquid-style variable substitutions', async () => {
const fileContent = await readFile(propKeysMockPath, {encoding: 'utf-8'});

const {metadata, metadataStrippedContent} = parseExistingMetadata(fileContent);
const processedContent = emplaceMetadata(metadataStrippedContent, metadata);

expect(metadata).toMatchSnapshot();
expect(processedContent).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
prop-{{key-name}}: This one has a substitution in a key name.
{{key-name}}-prop: This one's key starts with a substitution.
{{key-name}}: This one's key only consists of a substitution.
{{ key-name }}: This one's key employs a different whitespace style in a substitution.
---
Blah.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
prop1: "This is a metadata property with a {{substitution}} in it."
prop2: "This one contains {{multiple}} {{substitutions}}."
prop3: "This one has {{substitutions}} of {{ different }} {{ styles}}."
prop4: 'This one has a {{substitution}} as well, but the string literal is single-quoted.'
prop5: This one has no quotes at {{all}}.
prop6: {{this}} starts with a substitution.
prop7: >-
{{this}} one is a multiline {{property}}.
---
Blah.

0 comments on commit bee51f5

Please sign in to comment.