Skip to content

Commit

Permalink
Added item_exec parameter fixes:#34
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Oct 10, 2020
1 parent 0778600 commit 0d45a71
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"camelcase": true,
"curly": true,
"eqeqeq": true,
"evil": true,
"freeze": true,
"indent": 2,
"newcap": true,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This workflow has additional options that you can use to customize it for your u
| `filter_comments` | `medium,stackoverflow/Comment by $author/,stackexchange/Comment by $author/` | Comma separated list of platforms you want to enable the comment filter.<br/><br/>**Available filters**<ul><li>`medium`: Allows you to filter out the Medium comments</li><li>`stackoverflow/Comment by $author/`: Allows you to filter out the StackOverflow comments. Argument to this filter is optional, it defaults to 'Comment by $author'. If you use any language other than English on StackOverflow, you can use this argument to customize it. See [#16](https://github.com/gautamkrishnar/blog-post-workflow/issues/16) for more info.</li><li>`stackexchange/Comment by $author/`: Allows you to filter out the StackExchange comments. Argument to this filter follows the same format as `stackoverflow` filter's argument.</li></ul> | No |
| `custom_tags` | `""` | Allows you to use the custom tags from your feed items in your template. Format: `variableName/tagName/,variableName/tagName/`. Please see the [issue comment](https://github.com/gautamkrishnar/blog-post-workflow/issues/28#issuecomment-696024087) for more details | No |
| `title_max_length` | `""` | Allows you to trim the title in the posts list, excess text will be appended with an ellipsis `...` | No |
| `item_exec` | `""` | Allows you execute custom JavaScript code on each post item fetched from the xml to do advanced text manipulation. Please see the [issue comment](https://github.com/gautamkrishnar/blog-post-workflow/issues/34#issuecomment-706582788) as an example | No |
| `commit_message` | `Updated with the latest blog posts` | Allows you to customize the commit message | No |
| `committer_username` | `blog-post-bot` | Allows you to customize the committer username | No |
| `committer_email` | `[email protected]` | Allows you to customize the committer email | No |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ inputs:
description: "Allows you to trim the title in the posts list"
default: ""
required: false
item_exec:
description: "Allows you to execute custom JavaScript code on each item to do advanced text manipulation"
default: ""
required: false
commit_message:
description: "Commit message used while committing to the repo"
default: "Updated with the latest blog posts"
Expand Down
17 changes: 16 additions & 1 deletion blog-post-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const TOTAL_POST_COUNT = Number.parseInt(core.getInput('max_post_count'));
const TITLE_MAX_LENGTH = core.getInput('title_max_length') ?
Number.parseInt(core.getInput('title_max_length')) : null;

// Advanced content modification parameter, default: ""
const ITEM_EXEC = core.getInput('item_exec');

// Readme path, default: ./README.md
const README_FILE_PATH = core.getInput('readme_path');
const GITHUB_TOKEN = core.getInput('gh_token');
Expand Down Expand Up @@ -234,12 +237,24 @@ feedList.forEach((siteUrl) => {
item.title = item.title.trim().slice(0, TITLE_MAX_LENGTH) === item.title.trim() ?
item.title.trim() : item.title.trim().slice(0, TITLE_MAX_LENGTH) + '...';
}
return {
const post = {
title: item.title.trim(),
url: item.link.trim(),
date: new Date(item.pubDate.trim()),
...customTags
};
// Advanced content manipulation using javascript code
if (ITEM_EXEC) {
try {
eval(ITEM_EXEC);
} catch (e) {
core.error('Failure in executing `item_exec` parameter');
core.error(e);
process.exit(1);
}
}

return post;
});
resolve(posts);
}
Expand Down
1 change: 1 addition & 0 deletions local-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fs.writeFile(path.join(__dirname, 'test', 'Readme.md'), template, () => {
process.env.TEST_MODE = 'true';
process.env.INPUT_CUSTOM_TAGS = '';
process.env.INPUT_TITLE_MAX_LENGTH = '';
process.env.INPUT_ITEM_EXEC = '';
const testFile = process.env.DIST ? './dist/blog-post-workflow' :'./blog-post-workflow';
console.log('Testing: ', testFile);
require(testFile);
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const DEFAULT_TEST_ENV = {
INPUT_DATE_FORMAT: 'UTC:ddd mmm dd yyyy h:MM TT',
INPUT_CUSTOM_TAGS: '',
INPUT_TITLE_MAX_LENGTH: '',
INPUT_ITEM_EXEC: '',
TEST_MODE: 'true'
};

Expand Down Expand Up @@ -133,4 +134,16 @@ describe('Blog post workflow tests', function () {
};
await runAndCompareSnap(README_FILE, envObj);
});

it('Generated readme with advanced manipulation via JS should match the snapshot',async function () {
const README_FILE = 'Readme.exec.md';
fs.writeFileSync(path.join(__dirname, 'test', README_FILE), TEMPLATE);
const envObj = {
...process.env,
...DEFAULT_TEST_ENV,
INPUT_README_PATH: path.join(__dirname, 'test', README_FILE),
INPUT_ITEM_EXEC: 'post.title=post.title.replace("Gautam",""); post.title=post.title.replace("browser","");'
};
await runAndCompareSnap(README_FILE, envObj);
});
});
13 changes: 13 additions & 0 deletions test/Readme.exec.md.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->
- [God Mode in s: document.designMode = "on"](https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo)
- [Skipping the Chrome "Your connection is not private" warning](https://dev.to/gautamkrishnar/quickbits-1-skipping-the-chrome-your-connection-is-not-private-warning-4kp1)
- [Microsoft Student PartnersGeek is the new rockstar](https://dev.to/gautamkrishnar/microsoft-student-partners--geek-is-the-new-rockstar)
- [An Introduction to NumPy](https://dev.to/gautamkrishnar/an-introduction-to-numpy)
- [Hi, I'm krishna.R](https://dev.to/gautamkrishnar/hi-im-gautam-krishnar)
- [DuckDuckGoThe search engine redefined](https://dev.to/gautamkrishnar/duckduckgo-the-search-engine-redefined-4c7d)
<!-- BLOG-POST-LIST:END -->

# Other contents
Test content

0 comments on commit 0d45a71

Please sign in to comment.