diff --git a/action.yml b/action.yml index 570dd54..da5c0ed 100644 --- a/action.yml +++ b/action.yml @@ -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" diff --git a/blog-post-workflow.js b/blog-post-workflow.js index 5350c38..4cbced3 100644 --- a/blog-post-workflow.js +++ b/blog-post-workflow.js @@ -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; @@ -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 @@ -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) => { @@ -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); @@ -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); diff --git a/local-run.js b/local-run.js index 7c14607..4bfa110 100644 --- a/local-run.js +++ b/local-run.js @@ -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); diff --git a/test.js b/test.js index b45bff1..79f4888 100644 --- a/test.js +++ b/test.js @@ -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' }; @@ -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); + }); }); diff --git a/test/Readme.custom-tags.md.snap b/test/Readme.custom-tags.md.snap new file mode 100644 index 0000000..e4e46db --- /dev/null +++ b/test/Readme.custom-tags.md.snap @@ -0,0 +1,12 @@ +# Readme test +Post list example: +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 Partners – Geek 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 +DuckDuckGo – The search engine redefined https://dev.to/gautamkrishnar/duckduckgo-the-search-engine-redefined-4c7d + + +# Other contents +Test content diff --git a/test/sample.xml b/test/sample.xml index f3e6a22..07dd883 100644 --- a/test/sample.xml +++ b/test/sample.xml @@ -27,12 +27,15 @@ <p>Nice to meet you.</p> + hello + apple introductions God Mode in browsers: document.designMode = "on" Gautam krishna.R Thu, 16 Jul 2020 10:40:11 +0000 + world https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo <p>Just type <code>document.designMode = "on"</code> on you favourite browser devtools and see the magic. </p>