-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: liquid markdown front matter separately from the rest of the co…
…ntent
- Loading branch information
1 parent
324d200
commit 7230d7f
Showing
13 changed files
with
480 additions
and
42 deletions.
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.