-
Notifications
You must be signed in to change notification settings - Fork 38
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
0ac0e1f
commit 45a919b
Showing
11 changed files
with
284 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,26 @@ | ||
import {dump} from 'js-yaml'; | ||
|
||
import {FrontMatter, frontMatterFence, unescapeLiquidSubstitutionSyntax} from './common'; | ||
|
||
export const serializeFrontMatter = (frontMatter: FrontMatter) => { | ||
const dumped = unescapeLiquidSubstitutionSyntax( | ||
dump(frontMatter, {forceQuotes: true, 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}`; | ||
}; | ||
|
||
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,89 @@ | ||
import {YAMLException, load} from 'js-yaml'; | ||
|
||
import {log} from '../log'; | ||
|
||
import { | ||
FrontMatter, | ||
countLineAmount, | ||
escapeLiquidSubstitutionSyntax, | ||
frontMatterFence, | ||
} from './common'; | ||
|
||
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: duplicateKeysCompatibleLoad( | ||
escapeLiquidSubstitutionSyntax(metadata), | ||
filePath, | ||
) as FrontMatter, | ||
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
Oops, something went wrong.