Skip to content

Commit d6195bc

Browse files
added emoji support, fixes: #29
1 parent 36be303 commit d6195bc

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This workflow has additional options that you can use to customize it for your u
4343
| `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 |
4444
| `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 |
4545
| `disable_sort` | `false` | Disables the sorting of the list based on publish date | No |
46-
| `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 |
46+
| `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 |
4747
| `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 |
4848
| `user_agent` | `rss-parser` | Allows you to customize the user agent used by the RSS feed crawler | No |
4949
| `accept_header` | `application/rss+xml` | Allows you to customize the accept header of the http requests | No |

blog-post-workflow.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const core = require('@actions/core');
44
const fs = require('fs');
55
const dateFormat = require('dateformat');
66
const exec = require('./exec');
7+
const rand = require('random-seed');
78

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

115+
/**
116+
* Returns parsed parameterised templates as array or return null
117+
* @param template
118+
* @param keyName
119+
* @return {null|string[]}
120+
*/
121+
const getParameterisedTemplate = (template, keyName) => {
122+
if (template.indexOf('$' + keyName) > -1 && template.match(new RegExp('\\$' + keyName + '\\((.)*\\)', 'g'))) {
123+
return template.match(new RegExp('\\$' + keyName + '\\((.)*\\)', 'g'))[0]
124+
.replace('$'+ keyName +'(','')
125+
.replace(')','')
126+
.split(',')
127+
.map(item=> item.trim());
128+
} else {
129+
return null;
130+
}
131+
};
132+
114133
core.setSecret(GITHUB_TOKEN);
115134
const COMMENT_FILTERS = core
116135
.getInput('filter_comments')
@@ -245,6 +264,8 @@ Promise.allSettled(promiseArray).then((results) => {
245264
try {
246265
const readmeData = fs.readFileSync(README_FILE_PATH, 'utf8');
247266
const template = core.getInput('template');
267+
const randEmojiArr = getParameterisedTemplate(template, 'randomEmoji');
268+
const constEmojiArr = getParameterisedTemplate(template, 'emojiKey');
248269
const postListMarkdown = postsArray.reduce((acc, cur, index) => {
249270
if (template === 'default') {
250271
// Default template: - [$title]($url)
@@ -263,6 +284,21 @@ Promise.allSettled(promiseArray).then((results) => {
263284
const replaceValue = cur[tag] ? cur[tag] : '';
264285
content = content.replace(new RegExp('\\$' + tag + '\\b', 'g'), replaceValue);
265286
});
287+
288+
// Emoji implementation: Random
289+
if (randEmojiArr) {
290+
// For making randomness unique for each repos
291+
const seed = (process.env.GITHUB_REPOSITORY ? process.env.GITHUB_REPOSITORY : 'example') + index;
292+
const emoji = randEmojiArr[rand.create(seed).range(randEmojiArr.length)];
293+
content = content.replace(/\$randomEmoji\((.)*\)/g, emoji);
294+
}
295+
296+
// Emoji implementation: Static
297+
if (constEmojiArr) {
298+
// using modulus
299+
content = content.replace(/\$emojiKey\((.)*\)/g, constEmojiArr[index % constEmojiArr.length]);
300+
}
301+
266302
return acc + content;
267303
}
268304
}, '');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@actions/core": "^1.2.5",
3535
"dateformat": "^3.0.3",
3636
"process": "latest",
37+
"random-seed": "^0.3.0",
3738
"rss-parser": "^3.9.0"
3839
},
3940
"devDependencies": {

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,7 @@ [email protected]:
35063506
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
35073507
integrity sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=
35083508

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

4826+
random-seed@^0.3.0:
4827+
version "0.3.0"
4828+
resolved "https://registry.yarnpkg.com/random-seed/-/random-seed-0.3.0.tgz#d945f2e1f38f49e8d58913431b8bf6bb937556cd"
4829+
integrity sha1-2UXy4fOPSejViRNDG4v2u5N1Vs0=
4830+
dependencies:
4831+
json-stringify-safe "^5.0.1"
4832+
48264833
randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5, randombytes@^2.1.0:
48274834
version "2.1.0"
48284835
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"

0 commit comments

Comments
 (0)