From 51695171bde9a2064e99696527d09042005a028e Mon Sep 17 00:00:00 2001 From: Simon Mollweide Date: Sun, 17 Dec 2017 22:09:41 +0100 Subject: [PATCH] feat(convert-markdown): Convert markdown inline code blocks to cli --- src/convertMarkdown/index.js | 3 ++- .../__mocks__/default.md | 2 +- .../__mocks__/no-title.md | 2 +- .../__mocks__/default.md | 6 +++++ src/convertMarkdownInlineCodeToCli/index.js | 22 +++++++++++++++++++ .../index.spec.js | 10 +++++++++ src/symbols/index.js | 3 +++ 7 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/convertMarkdownInlineCodeToCli/__mocks__/default.md create mode 100644 src/convertMarkdownInlineCodeToCli/index.js create mode 100644 src/convertMarkdownInlineCodeToCli/index.spec.js diff --git a/src/convertMarkdown/index.js b/src/convertMarkdown/index.js index 6dbb1cd..bad41fa 100644 --- a/src/convertMarkdown/index.js +++ b/src/convertMarkdown/index.js @@ -2,6 +2,7 @@ const chalk = require('chalk'); const resolve = require('../resolve'); const convertMarkdownCodeToCli = require('../convertMarkdownCodeToCli'); +const convertMarkdownInlineCodeToCli = require('../convertMarkdownInlineCodeToCli'); /* readmeData @@ -79,7 +80,7 @@ const extractLinksFromReadme = readmeData => { } return { - text: convertMarkdownCodeToCli(text), + text: convertMarkdownInlineCodeToCli(convertMarkdownCodeToCli(text)), links, }; }; diff --git a/src/convertMarkdownCodeToCli/__mocks__/default.md b/src/convertMarkdownCodeToCli/__mocks__/default.md index ea51cc7..f10eadd 100644 --- a/src/convertMarkdownCodeToCli/__mocks__/default.md +++ b/src/convertMarkdownCodeToCli/__mocks__/default.md @@ -1,4 +1,4 @@ ```html // Key and Value -react.impulsCoach.components.teaserOverview.title = This is an example +react.components.title = This is an example ```; diff --git a/src/convertMarkdownCodeToCli/__mocks__/no-title.md b/src/convertMarkdownCodeToCli/__mocks__/no-title.md index 33e18b8..50d68fd 100644 --- a/src/convertMarkdownCodeToCli/__mocks__/no-title.md +++ b/src/convertMarkdownCodeToCli/__mocks__/no-title.md @@ -1,4 +1,4 @@ ``` // Key and Value -react.impulsCoach.components.teaserOverview.title = This is an example +react.components.title = This is an example ```; diff --git a/src/convertMarkdownInlineCodeToCli/__mocks__/default.md b/src/convertMarkdownInlineCodeToCli/__mocks__/default.md new file mode 100644 index 0000000..f47130b --- /dev/null +++ b/src/convertMarkdownInlineCodeToCli/__mocks__/default.md @@ -0,0 +1,6 @@ +Contribute buddy collect all readme files in the project and split them into sections and topics by using the `h1` and `h2` headlines. These sections and topics will be displayed in the cli before start (and after install) if you didn't read them before and nothing had changed. The reading history will be stored in your filesystem (user directory). + +```html +// Key and Value +react.components.title = This is an example +``` diff --git a/src/convertMarkdownInlineCodeToCli/index.js b/src/convertMarkdownInlineCodeToCli/index.js new file mode 100644 index 0000000..0f225d0 --- /dev/null +++ b/src/convertMarkdownInlineCodeToCli/index.js @@ -0,0 +1,22 @@ +const symbols = require('../symbols'); + +/** + * @param {string} markdown - the readme files data + * @param {Object} di - dependency injection + * @returns {string} markdown - replaced readme file data + **/ +function convertMarkdownInlineCodeToCli(markdown) { + const reg = /(`)([^`]*)(`)/g; + let results; + + // eslint-disable-next-line + while ((results = reg.exec(markdown)) !== null) { + if (results[0] !== '``' && results[0].indexOf('\n') < 0) { + markdown = markdown.replace(results[0], symbols.codeInline.msg(results[2])); + } + } + + return markdown; +} + +module.exports = convertMarkdownInlineCodeToCli; diff --git a/src/convertMarkdownInlineCodeToCli/index.spec.js b/src/convertMarkdownInlineCodeToCli/index.spec.js new file mode 100644 index 0000000..9ced9ba --- /dev/null +++ b/src/convertMarkdownInlineCodeToCli/index.spec.js @@ -0,0 +1,10 @@ +/* eslint quote-props: 0*/ +const fs = require('fs'); +const convertMarkdownInlineCodeToCli = require('./index'); + +describe('convertMarkdownInlineCodeToCli', () => { + it('default', () => { + const data = fs.readFileSync('./src/convertMarkdownInlineCodeToCli/__mocks__/default.md', 'utf8'); + expect(typeof convertMarkdownInlineCodeToCli(data)).toBe('string'); + }); +}); diff --git a/src/symbols/index.js b/src/symbols/index.js index bc340ab..53f9709 100644 --- a/src/symbols/index.js +++ b/src/symbols/index.js @@ -41,6 +41,9 @@ const symbols = { bottomLeft: ` ${chalk.gray.bgWhite(' │')}`, bottomRight: `${chalk.bgWhite(' ')} `, }, + codeInline: { + msg: text => chalk.black.bgWhite(text), + }, }; module.exports = symbols;