Skip to content

Commit

Permalink
added emoji support, fixes: #29
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Sep 27, 2020
1 parent 36be303 commit d6195bc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This workflow has additional options that you can use to customize it for your u
| `gh_token` | your GitHub token with repo scope | Use this to configure the token of the user that commits the workflow result to GitHub | No |
| `comment_tag_name` | `BLOG-POST-LIST` | Allows you to override the default comment tag name (`<!-- BLOG-POST-LIST:START --><!-- BLOG-POST-LIST:END -->`), if you want to show multiple instances of the action on the same repo, see advanced usage for more info | No |
| `disable_sort` | `false` | Disables the sorting of the list based on publish date | No |
| `template` | `default` | Allows you to change the structure of the posts list by using the `$title`, `$url`, `$newline`, and `$date` variables. By default this workflow uses markdown list format to render the posts, you can override this behavior using this option. Eg: `[$title]($url) ` will give you a space-separated list of posts | No |
| `template` | `default` | Allows you to change the structure of the posts list by using the `$title`, `$url`, `$newline`, and `$date` variables. By default this workflow uses markdown list format to render the posts, you can override this behavior using this option. Eg: `[$title]($url) ` will give you a space-separated list of posts. You can alo use `$randomEmoji` or `$emojiKey` with emoji as parameters to display emojis on your posts list see [issue comment](https://github.com/gautamkrishnar/blog-post-workflow/issues/29#issuecomment-699622596), example: `$newline - $randomEmoji(💯,🔥,💫,🚀,🌮) [$title]($url)` | No |
| `date_format` | `UTC:ddd mmm dd yyyy h:MM TT` | Allows you to change the format of the date or time displayed when using the $date in the template option. This uses NPM dateformat library, please read the library [documentation](https://www.npmjs.com/package/dateformat#named-formats) for the supported formats | No |
| `user_agent` | `rss-parser` | Allows you to customize the user agent used by the RSS feed crawler | No |
| `accept_header` | `application/rss+xml` | Allows you to customize the accept header of the http requests | No |
Expand Down
36 changes: 36 additions & 0 deletions blog-post-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const core = require('@actions/core');
const fs = require('fs');
const dateFormat = require('dateformat');
const exec = require('./exec');
const rand = require('random-seed');

/**
* Builds the new readme by replacing the readme's <!-- BLOG-POST-LIST:START --><!-- BLOG-POST-LIST:END --> tags
Expand Down Expand Up @@ -111,6 +112,24 @@ const updateAndParseCompoundParams = (sourceWithParam, obj) => {
}
};

/**
* Returns parsed parameterised templates as array or return null
* @param template
* @param keyName
* @return {null|string[]}
*/
const getParameterisedTemplate = (template, keyName) => {
if (template.indexOf('$' + keyName) > -1 && template.match(new RegExp('\\$' + keyName + '\\((.)*\\)', 'g'))) {
return template.match(new RegExp('\\$' + keyName + '\\((.)*\\)', 'g'))[0]
.replace('$'+ keyName +'(','')
.replace(')','')
.split(',')
.map(item=> item.trim());
} else {
return null;
}
};

core.setSecret(GITHUB_TOKEN);
const COMMENT_FILTERS = core
.getInput('filter_comments')
Expand Down Expand Up @@ -245,6 +264,8 @@ Promise.allSettled(promiseArray).then((results) => {
try {
const readmeData = fs.readFileSync(README_FILE_PATH, 'utf8');
const template = core.getInput('template');
const randEmojiArr = getParameterisedTemplate(template, 'randomEmoji');
const constEmojiArr = getParameterisedTemplate(template, 'emojiKey');
const postListMarkdown = postsArray.reduce((acc, cur, index) => {
if (template === 'default') {
// Default template: - [$title]($url)
Expand All @@ -263,6 +284,21 @@ Promise.allSettled(promiseArray).then((results) => {
const replaceValue = cur[tag] ? cur[tag] : '';
content = content.replace(new RegExp('\\$' + tag + '\\b', 'g'), replaceValue);
});

// Emoji implementation: Random
if (randEmojiArr) {
// For making randomness unique for each repos
const seed = (process.env.GITHUB_REPOSITORY ? process.env.GITHUB_REPOSITORY : 'example') + index;
const emoji = randEmojiArr[rand.create(seed).range(randEmojiArr.length)];
content = content.replace(/\$randomEmoji\((.)*\)/g, emoji);
}

// Emoji implementation: Static
if (constEmojiArr) {
// using modulus
content = content.replace(/\$emojiKey\((.)*\)/g, constEmojiArr[index % constEmojiArr.length]);
}

return acc + content;
}
}, '');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@actions/core": "^1.2.5",
"dateformat": "^3.0.3",
"process": "latest",
"random-seed": "^0.3.0",
"rss-parser": "^3.9.0"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3506,7 +3506,7 @@ json-schema@0.2.3:
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
integrity sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=

json-stringify-safe@~5.0.1:
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
Expand Down Expand Up @@ -4823,6 +4823,13 @@ quote-stream@^1.0.1, quote-stream@~1.0.2:
minimist "^1.1.3"
through2 "^2.0.0"

random-seed@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/random-seed/-/random-seed-0.3.0.tgz#d945f2e1f38f49e8d58913431b8bf6bb937556cd"
integrity sha1-2UXy4fOPSejViRNDG4v2u5N1Vs0=
dependencies:
json-stringify-safe "^5.0.1"

randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5, randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down

0 comments on commit d6195bc

Please sign in to comment.