Skip to content

Commit

Permalink
Added custom tags feature, fixes: #28
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Sep 21, 2020
1 parent b35653a commit c8bb3a8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ inputs:
description: "Allows you to customize the accept header of the http request"
default: "application/rss+xml"
required: false
custom_tags:
description: "Allows you to use the custom tags from your feed items in your template"
default: ""
required: false
commit_message:
description: "Commit message used while committing to the repo"
default: "Updated with the latest blog posts"
Expand Down
71 changes: 55 additions & 16 deletions blog-post-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,30 @@ const commitReadme = async () => {
const userAgent = core.getInput('user_agent');
const acceptHeader = core.getInput('accept_header');

let parser = new Parser({headers: {'User-Agent': userAgent, 'Accept': acceptHeader}});

// Total no of posts to display on readme, all sources combined, default: 5
const TOTAL_POST_COUNT = Number.parseInt(core.getInput('max_post_count'));
// Readme path, default: ./README.md
const README_FILE_PATH = core.getInput('readme_path');
const GITHUB_TOKEN = core.getInput('gh_token');

// Filter parameters
const FILTER_PARAMS = {
stackoverflow: 'Comment by $author',
stackexchange: 'Comment by $author',
};
// Custom tags
const CUSTOM_TAGS = {};

/**
* Updates FILTER_PARAMS object with filter parameters
* @param sourceWithParam filter source with param eg: stackoverflow/Comment by $author/
* Compound parameter parser, Updates obj with compound parameters and returns item name
* @param sourceWithParam filter source with compound param eg: stackoverflow/Comment by $author/
* @param obj object to update
* @return {string} actual source name eg: stackoverflow
*/
const updateAndParseParams = (sourceWithParam) => {
const param = sourceWithParam.split('/'); // Reading params
const updateAndParseCompoundParams = (sourceWithParam, obj) => {
const param = sourceWithParam.split('/'); // Reading params ['stackoverflow','Comment by $author', '']
if (param.length === 3) {
Object.assign(FILTER_PARAMS, {[param[0]]: param[1]});
Object.assign(obj, {[param[0]]: param[1]});
return param[0];// Returning source name
} else {
return sourceWithParam;
Expand All @@ -114,14 +117,22 @@ const COMMENT_FILTERS = core
.trim()
.split(',')
.map((item)=>{
const str = item.trim();
if (str.startsWith('stackoverflow') || str.startsWith('stackexchange')) {
return updateAndParseParams(item);
item = item.trim();
if (item.startsWith('stackoverflow') || item.startsWith('stackexchange')) {
return updateAndParseCompoundParams(item, FILTER_PARAMS);
} else {
return str;
return item;
}
});

core.getInput('custom_tags')
.trim()
.split(',')
.forEach((item)=> {
item = item.trim();
updateAndParseCompoundParams(item, CUSTOM_TAGS); // Creates custom tag object
});

const promiseArray = []; // Runner
const runnerNameArray = []; // To show the error/success message
let postsArray = []; // Array to store posts
Expand Down Expand Up @@ -151,6 +162,19 @@ const ignoreStackExchangeComments = (item) => !(COMMENT_FILTERS.indexOf('stackex
item.link.includes('stackexchange.com') &&
item.title.startsWith(FILTER_PARAMS.stackexchange.replace(/\$author/g, item.author)));

const customTagArgs = Object.keys(CUSTOM_TAGS).map(
item => [CUSTOM_TAGS[item], item]);

let parser = new Parser({
'headers': {
'User-Agent': userAgent,
'Accept': acceptHeader
},
customFields: {
item: [...customTagArgs]
}
});

feedList.forEach((siteUrl) => {
runnerNameArray.push(siteUrl);
promiseArray.push(new Promise((resolve, reject) => {
Expand All @@ -174,10 +198,18 @@ feedList.forEach((siteUrl) => {
if (!item.link) {
reject('Cannot read response->item->link');
}
// Custom tags
let customTags = {};
Object.keys(CUSTOM_TAGS).forEach((tag)=> {
if (item[tag]) {
Object.assign(customTags, {[tag]: item[tag]});
}
});
return {
title: item.title.trim(),
url: item.link.trim(),
date: new Date(item.pubDate.trim())
date: new Date(item.pubDate.trim()),
...customTags
};
});
resolve(posts);
Expand Down Expand Up @@ -220,11 +252,18 @@ Promise.allSettled(promiseArray).then((results) => {
} else {
// Building with custom template
const date = dateFormat(cur.date, core.getInput('date_format')); // Formatting date
return acc + template
.replace(/\$title/g, cur.title)
.replace(/\$url/g, cur.url)
.replace(/\$date/g, date)
let content = template
.replace(/\$title\b/g, cur.title)
.replace(/\$url\b/g, cur.url)
.replace(/\$date\b/g, date)
.replace(/\$newline/g, '\n');

// Setting Custom tags to the template
Object.keys(CUSTOM_TAGS).forEach((tag)=> {
const replaceValue = cur[tag] ? cur[tag] : '';
content = content.replace(new RegExp('\\$' + tag + '\\b', 'g'), replaceValue);
});
return acc + content;
}
}, '');
const newReadme = buildReadme(readmeData, postListMarkdown);
Expand Down
1 change: 1 addition & 0 deletions local-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fs.writeFile(path.join(__dirname, 'test', 'Readme.md'), template, () => {
process.env.INPUT_TEMPLATE = 'default';
process.env.INPUT_DATE_FORMAT = 'UTC:ddd mmm dd yyyy h:MM TT';
process.env.TEST_MODE = 'true';
process.env.INPUT_CUSTOM_TAGS = '';
const testFile = process.env.DIST ? './dist/blog-post-workflow' :'./blog-post-workflow';
console.log('Testing: ', testFile);
require(testFile);
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_TEST_ENV = {
INPUT_ACCEPT_HEADER: 'application/rss+xml',
INPUT_GH_TOKEN: 'secret-test',
INPUT_DATE_FORMAT: 'UTC:ddd mmm dd yyyy h:MM TT',
INPUT_CUSTOM_TAGS: '',
TEST_MODE: 'true'
};

Expand Down Expand Up @@ -88,4 +89,19 @@ describe('Blog post workflow tests', function () {
const newReadme = fs.readFileSync(path.join(__dirname, 'test' , README_FILE), 'utf-8');
assert.equal(snapshot, newReadme);
});
it('Generated readme without custom elements should match the snapshot',async function () {
const README_FILE = 'Readme.custom-tags.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_CUSTOM_TAGS: 'testingTag/testingTag/,testingTag2/testingTag2/',
INPUT_TEMPLATE: '$title $url $testingTag $testingTag2 $newline'
};
await exec('node', [TEST_FILE],{env: envObj});
const snapshot = fs.readFileSync(path.join(__dirname, 'test' , README_FILE + '.snap'), 'utf-8');
const newReadme = fs.readFileSync(path.join(__dirname, 'test' , README_FILE), 'utf-8');
assert.equal(snapshot, newReadme);
});
});
12 changes: 12 additions & 0 deletions test/Readme.custom-tags.md.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->God Mode in browsers: document.designMode = "on" https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo world
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 Gautam krishna.R https://dev.to/gautamkrishnar/hi-im-gautam-krishnar hello apple
DuckDuckGoThe search engine redefined https://dev.to/gautamkrishnar/duckduckgo-the-search-engine-redefined-4c7d
<!-- BLOG-POST-LIST:END -->

# Other contents
Test content
3 changes: 3 additions & 0 deletions test/sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
<testingTag>hello</testingTag>
<testingTag2>apple</testingTag2>
<category>introductions</category>
</item>
<item>
<title>God Mode in browsers: document.designMode = "on"</title>
<author>Gautam krishna.R</author>
<pubDate>Thu, 16 Jul 2020 10:40:11 +0000</pubDate>
<testingTag>world</testingTag>
<link>https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo</link>
<guid>https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo</guid>
<description>&lt;p&gt;Just type &lt;code&gt;document.designMode = "on"&lt;/code&gt; on you favourite browser devtools and see the magic. &lt;/p&gt;
Expand Down

0 comments on commit c8bb3a8

Please sign in to comment.