-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Duplicate code in readMetadata.js & versionFallback.js #725 * Putting back package-lock.json * Rename lib/server/utilsMetadata.js -> lib/server/metadataUtils.js * Update splitHeader + extractMetadata * Update prettier
- Loading branch information
Showing
7 changed files
with
100 additions
and
101 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
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,66 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// split markdown header | ||
function splitHeader(content) { | ||
// New line characters need to handle all operating systems. | ||
const lines = content.split(/\r?\n/); | ||
if (lines[0] !== '---') { | ||
return {}; | ||
} | ||
let i = 1; | ||
for (; i < lines.length - 1; ++i) { | ||
if (lines[i] === '---') { | ||
break; | ||
} | ||
} | ||
return { | ||
header: lines.slice(1, i + 1).join('\n'), | ||
content: lines.slice(i + 1).join('\n'), | ||
}; | ||
} | ||
|
||
// Extract markdown metadata header | ||
function extractMetadata(content) { | ||
const metadata = {}; | ||
const both = splitHeader(content); | ||
|
||
// if no content returned, then that means there was no header, and both.header is the content | ||
if (!both.content) { | ||
if (!both.header) { | ||
// if no both returned, then that means there was no header and no content => we return the current content of the file | ||
return {metadata, rawContent: content}; | ||
} | ||
return {metadata, rawContent: both.header}; | ||
} | ||
|
||
// New line characters => to handle all operating systems. | ||
const lines = both.header.split(/\r?\n/); | ||
|
||
// Loop that add to metadata the current content of the fields of the header | ||
// Like the format: | ||
// id: | ||
// title: | ||
// original_id: | ||
for (let i = 0; i < lines.length - 1; ++i) { | ||
const keyvalue = lines[i].split(':'); | ||
const key = keyvalue[0].trim(); | ||
let value = keyvalue | ||
.slice(1) | ||
.join(':') | ||
.trim(); | ||
try { | ||
value = JSON.parse(value); | ||
} catch (e) {} | ||
metadata[key] = value; | ||
} | ||
return {metadata, rawContent: both.content}; | ||
} | ||
|
||
module.exports = { | ||
extractMetadata, | ||
}; |
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