Skip to content

Commit

Permalink
🐛 Escape markdown characters in social network questions answers (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 authored and kefranabg committed Nov 15, 2019
1 parent 6ef2be8 commit d96e310
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"inquirer": "~7.0.0",
"load-json-file": "^6.0.0",
"lodash": "^4.17.11",
"ora": "4.0.3",
"markdown-escape": "^1.0.2",
"node-fetch": "^2.6.0",
"ora": "4.0.3",
"yargs": "^14.0.0"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/questions/author-patreon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { cleanSocialNetworkUsername } = require('../utils')

module.exports = () => ({
type: 'input',
message: '❤️ Patreon username (use empty value to skip)',
name: 'authorPatreonUsername'
name: 'authorPatreonUsername',
filter: cleanSocialNetworkUsername
})
3 changes: 2 additions & 1 deletion src/questions/author-patreon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('askPatreonUsername', () => {
expect(result).toEqual({
type: 'input',
message: '❤️ Patreon username (use empty value to skip)',
name: 'authorPatreonUsername'
name: 'authorPatreonUsername',
filter: expect.any(Function)
})
})
})
9 changes: 6 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const boxen = require('boxen')
const path = require('path')
const getReposName = require('git-repo-name')
const fetch = require('node-fetch')
const escapeMarkdown = require('markdown-escape')
const { execSync } = require('child_process')

const END_MSG = `README.md was successfully generated.
Expand Down Expand Up @@ -118,12 +119,14 @@ const getDefaultAnswers = questions =>
)

/**
* Clean social network username by removing the @ prefix
* Clean social network username by removing the @ prefix and
* escaping markdown characters
*
* @param input social network username input
* @returns {*} input without the prefix
* @returns {*} escaped input without the prefix
*/
const cleanSocialNetworkUsername = input => input.replace(/^@/, '')
const cleanSocialNetworkUsername = input =>
escapeMarkdown(input.replace(/^@/, ''))

/**
* Get author's website from Github API
Expand Down
15 changes: 12 additions & 3 deletions src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,20 @@ describe('utils', () => {

describe('cleanSocialNetworkUsername', () => {
it('should remove prefixed @', () => {
expect(cleanSocialNetworkUsername('@Slashgear_')).toEqual('Slashgear_')
expect(cleanSocialNetworkUsername('@Slashgear')).toEqual('Slashgear')
})

it('should return the same string when string is not prefixed', () => {
expect(cleanSocialNetworkUsername('Slashgear_')).toEqual('Slashgear_')
it('should escape markdown characters', () => {
expect(cleanSocialNetworkUsername('Slashgear__')).toEqual(
'Slashgear\\_\\_'
)
expect(cleanSocialNetworkUsername('Slashgear**')).toEqual(
'Slashgear\\*\\*'
)
})

it('should return the same string when string is not prefixed or contains markdown chars', () => {
expect(cleanSocialNetworkUsername('Slashgear')).toEqual('Slashgear')
})
})

Expand Down

0 comments on commit d96e310

Please sign in to comment.