Github user nikhilkh commented on a diff in the pull request:
https://github.com/apache/cordova-docs/pull/523#discussion_r54315457
--- Diff: tools/bin/util.js ---
@@ -22,45 +22,93 @@ module.exports = function () {
var fs = require("fs");
var path = require("path");
- return {
+ function stripFrontMatter(text) {
+
+ // get and replace front matter if it's there
+ // NOTE:
+ // String.replace() replaces only the first occurrence
+ // of a string, which is what we want
+ var rawFrontMatterString = getRawFrontMatterString(text);
+ if (rawFrontMatterString !== null) {
+ return text.replace(rawFrontMatterString, "");
+ }
- stripFrontMatter: function(text) {
+ return text;
+ }
- var marker = "---";
+ function getFrontMatterString(text) {
+ var rawFrontMatterString = getRawFrontMatterString(text);
+ if (rawFrontMatterString !== null) {
- var firstMarker = text.indexOf(marker);
- if (firstMarker === (-1)) {
- return text;
- }
+ // strip out front matter markers
+ var frontMatterString =
rawFrontMatterString.replace(/^---\s*$/gm, "")
+ return frontMatterString;
+ }
- var secondMarker = text.indexOf(marker, firstMarker +
marker.length);
- var start = secondMarker + marker.length;
+ return null;
+ }
- return text.slice(start);
- },
+ function setFrontMatterString(text, frontMatterString) {
+ var textOnly = stripFrontMatter(text);
+ var newText = "---\n" + frontMatterString + "---\n\n" + textOnly;
+ return newText;
+ }
- listdirsSync: function (root) {
- return fs.readdirSync(root).filter(function(fileName) {
- return fs.statSync(path.join(root,
fileName)).isDirectory();
- });
- },
+ function getRawFrontMatterString(text) {
+ // NOTE:
+ // [\s\S] matches all characters
+ // *? non-greedy *-match
+ var match = text.match(/^(---\s*\n[\s\S]*?\n---\s*\n)[\s\S]*$/);
--- End diff --
This might not work with Windows line endings. Here's another approach to
doing this: https://github.com/jxson/front-matter/blob/master/index.js
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]