-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Liquid front matter separately from the rest of the content #518
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export type FrontMatter = { | ||
[key: string]: unknown; | ||
metadata?: Record<string, unknown>[]; | ||
}; | ||
|
||
export const frontMatterFence = '---'; | ||
|
||
/** | ||
* Temporary workaround to enable parsing YAML metadata from potentially | ||
* Liquid-aware source files | ||
* @param content Input string which could contain Liquid-style substitution syntax (which clashes with YAML | ||
* object syntax) | ||
* @returns String with `{}` escaped, ready to be parsed with `js-yaml` | ||
*/ | ||
export const escapeLiquidSubstitutionSyntax = (content: string): string => | ||
content.replace(/{{/g, '(({{').replace(/}}/g, '}}))'); | ||
|
||
/** | ||
* Inverse of a workaround defined above. | ||
* @see `escapeLiquidSubstitutionSyntax` | ||
* @param escapedContent Input string with `{}` escaped with backslashes | ||
* @returns Unescaped string | ||
*/ | ||
export const unescapeLiquidSubstitutionSyntax = (escapedContent: string): string => | ||
escapedContent.replace(/\(\({{/g, '{{').replace(/}}\)\)/g, '}}'); | ||
|
||
export const countLineAmount = (str: string) => str.split(/\r?\n/).length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {dump} from 'js-yaml'; | ||
|
||
import {FrontMatter, frontMatterFence} from './common'; | ||
|
||
export const serializeFrontMatter = (frontMatter: FrontMatter) => { | ||
const dumped = dump(frontMatter, {lineWidth: -1}).trim(); | ||
|
||
// This empty object check is a bit naive | ||
// The other option would be to check if all own fields are `undefined`, | ||
// since we exploit passing in `undefined` to remove a field quite a bit | ||
if (dumped === '{}') { | ||
return ''; | ||
} | ||
|
||
return `${frontMatterFence}\n${dumped}\n${frontMatterFence}\n`; | ||
}; | ||
|
||
export const emplaceSerializedFrontMatter = ( | ||
frontMatterStrippedContent: string, | ||
frontMatter: string, | ||
) => `${frontMatter}${frontMatterStrippedContent}`; | ||
|
||
export const emplaceFrontMatter = (frontMatterStrippedContent: string, frontMatter: FrontMatter) => | ||
emplaceSerializedFrontMatter(frontMatterStrippedContent, serializeFrontMatter(frontMatter)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import {YAMLException, load} from 'js-yaml'; | ||
|
||
import {log} from '../log'; | ||
|
||
import { | ||
FrontMatter, | ||
countLineAmount, | ||
escapeLiquidSubstitutionSyntax, | ||
frontMatterFence, | ||
unescapeLiquidSubstitutionSyntax, | ||
} from './common'; | ||
import {transformFrontMatterValues} from './transformValues'; | ||
|
||
type ParseExistingMetadataReturn = { | ||
frontMatter: FrontMatter; | ||
frontMatterStrippedContent: string; | ||
frontMatterLineCount: number; | ||
}; | ||
|
||
const matchMetadata = (fileContent: string) => { | ||
if (!fileContent.startsWith(frontMatterFence)) { | ||
return null; | ||
} | ||
|
||
// Search by format: | ||
// --- | ||
// metaName1: metaValue1 | ||
// metaName2: meta value2 | ||
// incorrectMetadata | ||
// --- | ||
const regexpMetadata = '(?<=-{3}\\r?\\n)((.*\\r?\\n)*?)(?=-{3}\\r?\\n)'; | ||
// Search by format: | ||
// --- | ||
// main content 123 | ||
const regexpFileContent = '-{3}\\r?\\n((.*[\r?\n]*)*)'; | ||
|
||
const regexpParseFileContent = new RegExp(`${regexpMetadata}${regexpFileContent}`, 'gm'); | ||
|
||
return regexpParseFileContent.exec(fileContent); | ||
}; | ||
|
||
const duplicateKeysCompatibleLoad = (yaml: string, filePath: string | undefined) => { | ||
try { | ||
return load(yaml); | ||
} catch (e) { | ||
if (e instanceof YAMLException) { | ||
const duplicateKeysDeprecationWarning = ` | ||
In ${filePath ?? '(unknown)'}: Encountered a YAML parsing exception when processing file metadata: ${e.reason}. | ||
It's highly possible the input file contains duplicate mapping keys. | ||
Will retry processing with necessary compatibility flags. | ||
Please note that this behaviour is DEPRECATED and WILL be removed in a future version | ||
without further notice, so the build WILL fail when supplied with YAML-incompatible meta. | ||
` | ||
.replace(/^\s+/gm, '') | ||
.replace(/\n/g, ' ') | ||
.trim(); | ||
|
||
log.warn(duplicateKeysDeprecationWarning); | ||
|
||
return load(yaml, {json: true}); | ||
} | ||
|
||
throw e; | ||
} | ||
}; | ||
|
||
export const separateAndExtractFrontMatter = ( | ||
fileContent: string, | ||
filePath?: string, | ||
): ParseExistingMetadataReturn => { | ||
const matches = matchMetadata(fileContent); | ||
|
||
if (matches && matches.length > 0) { | ||
const [, metadata, , metadataStrippedContent] = matches; | ||
|
||
return { | ||
frontMatter: transformFrontMatterValues( | ||
duplicateKeysCompatibleLoad( | ||
escapeLiquidSubstitutionSyntax(metadata), | ||
filePath, | ||
) as FrontMatter, | ||
(v) => (typeof v === 'string' ? unescapeLiquidSubstitutionSyntax(v) : v), | ||
), | ||
frontMatterStrippedContent: metadataStrippedContent, | ||
frontMatterLineCount: countLineAmount(metadata), | ||
}; | ||
} | ||
|
||
return { | ||
frontMatter: {}, | ||
frontMatterStrippedContent: fileContent, | ||
frontMatterLineCount: 0, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './extract'; | ||
export * from './emplace'; | ||
export * from './transformValues'; | ||
export {countLineAmount} from './common'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {FrontMatter} from './common'; | ||
|
||
export const transformFrontMatterValues = ( | ||
frontMatter: FrontMatter, | ||
valueMapper: (v: unknown) => unknown, | ||
): FrontMatter => { | ||
const transformInner = (something: unknown): unknown => { | ||
if (typeof something === 'object' && something !== null) { | ||
return Object.fromEntries( | ||
Object.entries(something).map(([k, v]) => [k, transformInner(v)]), | ||
); | ||
} | ||
|
||
if (Array.isArray(something)) { | ||
return something.map((el) => transformInner(el)); | ||
} | ||
|
||
return valueMapper(something); | ||
}; | ||
|
||
return transformInner(frontMatter) as FrontMatter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Liquid substitutions in front matter (formerly metadata) should not fail even when variables contain reserved characters (YAML multiline syntax) 1`] = ` | ||
"--- | ||
multiline: \\">- This should break, right?\\\\n\\\\tRight?\\" | ||
--- | ||
Content" | ||
`; | ||
|
||
exports[`Liquid substitutions in front matter (formerly metadata) should not fail even when variables contain reserved characters (curly braces) 1`] = ` | ||
"--- | ||
braces: '{}' | ||
--- | ||
Content" | ||
`; | ||
|
||
exports[`Liquid substitutions in front matter (formerly metadata) should not fail even when variables contain reserved characters (double quotes) 1`] = ` | ||
"--- | ||
quotes: '\\"When you arise in the morning, think of what a precious privilege it is to be alive - to breathe, to think, to enjoy, to love.\\" — Marcus Aurelius (allegedly)' | ||
--- | ||
Content" | ||
`; | ||
|
||
exports[`Liquid substitutions in front matter (formerly metadata) should not fail even when variables contain reserved characters (single quotes) 1`] = ` | ||
"--- | ||
quotes: This isn't your typical substitution. It has single quotes. | ||
--- | ||
Content" | ||
`; | ||
|
||
exports[`Liquid substitutions in front matter (formerly metadata) should not fail even when variables contain reserved characters (square brackets) 1`] = ` | ||
"--- | ||
brackets: '[]' | ||
--- | ||
Content" | ||
`; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the problem we are solving in this PR valid only for meta or for all yaml pages too?