Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests, and support for nested lists #11

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: node_js
node_js:
- 'node'
- '6'
branches:
only:
- gh-pages
28 changes: 22 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ module.exports = exports = markdown2confluence
// http://blogs.atlassian.com/2011/11/why-we-removed-wiki-markup-editor-in-confluence-4/

var MAX_CODE_LINE = 20
var SPACE = ' '

function Renderer() {}

var rawRenderer = marked.Renderer

var langArr = 'actionscript3 bash csharp coldfusion cpp css delphi diff erlang groovy java javafx javascript perl php none powershell python ruby scala sql vb html/xml'.split(/\s+/)
var langMap = {
shell: 'bash',
html: 'html',
shell: 'bash',
html: 'html',
xml: 'xml'
}
for (var i = 0, x; x = langArr[i++];) {
Expand Down Expand Up @@ -58,26 +59,41 @@ _.extend(Renderer.prototype, rawRenderer.prototype, {
}
, link: function(href, title, text) {
var arr = [href]
if (title) {
arr.push(title)
}
if (text) {
arr.unshift(text)
}
return '[' + arr.join('|') + ']'
}
, list: function(body, ordered) {
var arr = _.filter(_.trim(body).split('\n'), function(line) {
var type = ordered ? '#' : '*'
var parsedBody = _.trim(body)
.replace(/([a-z0-9])([\*\#]){1} {1}/ig, '$1\n$2 ')
.split('\n')

var arr = _.filter(parsedBody, function(line) {
return line
})
var type = ordered ? '#' : '*'
return _.map(arr, function(line) {
return type + ' ' + line
var lineStart = type
if (!(_.startsWith(line, '#') || _.startsWith(line, '*'))) {
lineStart += SPACE
}
return lineStart + line
}).join('\n') + '\n\n'

}
, listitem: function(body, ordered) {
return body + '\n'
}
, image: function(href, title, text) {
return '!' + href + '!'
var arr = [href]
if (text) {
arr.push('alt=' + text)
}
return '!' + arr.join('|') + '!'
}
, table: function(header, body) {
return header + body + '\n'
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "convert markdown to confluence markup",
"main": "index.js",
"scripts": {
"test": "node test.js",
"test": "jest test.js",
"build": "npm run build-html && npm run build-js",
"build-html": "jade -p node_modules/jade-gist/. < browser/index.jade > browser/index.html",
"build-js": "webpack browser/app.js --output-filename=browser/bundle.js",
Expand All @@ -24,7 +24,9 @@
"bin": {
"markdown2confluence": "bin/markdown2confluence.js"
},
"devDependencies": {},
"devDependencies": {
"jest": "^18.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/chunpu/markdown2confluence.git"
Expand Down
177 changes: 174 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,179 @@ var pairs = [
, ['### h3', 'h3. h3\n\n']
]

pairs.forEach(function(arr, i) {
assert.equal(md2conflu(arr[0]), arr[1], i + ': ' + arr[0] + ' = ' + arr[1])
test('basic', function() {
pairs.forEach(function(arr, i) {
expect(md2conflu(arr[0])).toBe(arr[1])
})
})

console.log('all pass!')

test('header 1', function() {
expect(md2conflu('# Hello')).toBe('h1. Hello\n\n')
})

test('heading 2', function() {
expect(md2conflu('## Hello')).toBe('h2. Hello\n\n')
})

test('heading 3', function() {
expect(md2conflu('### Hello')).toBe('h3. Hello\n\n')
})

test('heading 4', function() {
expect(md2conflu('#### Hello')).toBe('h4. Hello\n\n')
})

describe('unordered list', function() {
test('basic', function() {
var list = ''.concat(
'- this\n',
'- is\n',
'- a\n',
'- list\n'
)
var confluence_list = list.replace(/-/g, '*') + '\n'
expect(md2conflu(list)).toBe(confluence_list)
})
test('nested', function() {
var markdownList = ''.concat(
'- this\n',
'- is\n',
' - nested\n',
' - deep nested\n',
'- a\n',
' - nested\n',
'- list\n'
)
var confluence_list = ''.concat(
'* this\n',
'* is\n',
'** nested\n',
'*** deep nested\n',
'* a\n',
'** nested\n',
'* list\n\n'
)
expect(md2conflu(markdownList)).toBe(confluence_list)
})
})

describe('ordered list', function() {
test('basic', function() {
var list = ''.concat(
'1. this\n',
'1. is\n',
'1. a\n',
'1. list\n'
)
var confluence_list = list.replace(/1\./g, '#') + '\n'
expect(md2conflu(list)).toBe(confluence_list)
})
test('nested', function() {
var markdownList = ''.concat(
'1. this\n',
'1. is\n',
' 1. nested\n',
' 1. deep nested\n',
'1. a\n',
' 1. nested\n',
'1. list\n'
)
var confluence_list = ''.concat(
'# this\n',
'# is\n',
'## nested\n',
'### deep nested\n',
'# a\n',
'## nested\n',
'# list\n\n'
)
expect(md2conflu(markdownList)).toBe(confluence_list)
})
test('nested mixed', function() {
var markdownList = ''.concat(
'1. this\n',
'1. is\n',
' - nested\n',
' - deep nested\n',
'1. a\n',
' - nested\n',
'1. list\n'
)
var confluence_list = ''.concat(
'# this\n',
'# is\n',
'#* nested\n',
'#** deep nested\n',
'# a\n',
'#* nested\n',
'# list\n\n'
)
expect(md2conflu(markdownList)).toBe(confluence_list)
})
})

test('strong text', function() {
expect(md2conflu('**strong**')).toBe('*strong*\n\n')
})

test('italics text', function() {
expect(md2conflu('*some text here*')).toBe('_some text here_\n\n')
})

test('inline code', function() {
expect(md2conflu('`hello world`')).toBe('{{hello world}}\n\n')
})

test('block of code', function() {
code = ' this is code\n'
expect(md2conflu(code)).toBe('{code:language=none|borderStyle=solid|theme=RDark|linenumbers=true|collapse=false}\nthis is code\n{code}\n\n')
})

test('strikethrough', function() {
expect(md2conflu('~~strikethrough text~~')).toBe('-strikethrough text-\n\n')
})

test('quote', function() {
expect(md2conflu('> this is a quote')).toBe('{quote}this is a quote\n\n{quote}')
})

describe('hyperlink', function() {
test('with no text or title', function () {
expect(md2conflu('[](http://github.com)')).toBe('[http://github.com]\n\n')
expect(md2conflu('http://github.com')).toBe('[http://github.com|http://github.com]\n\n')
})
test('with text', function () {
expect(md2conflu('[github](http://github.com)')).toBe('[github|http://github.com]\n\n')
})
test('with title', function () {
expect(md2conflu('[](http://github.com "Github")')).toBe('[http://github.com|Github]\n\n')
})
test('with text and title', function () {
expect(md2conflu('[github](http://github.com "Github")')).toBe('[github|http://github.com|Github]\n\n')
})
})

describe('image link', function () {
test('without alt', function() {
expect(md2conflu('![](http://github.com/logo.png)')).toBe('!http://github.com/logo.png!\n\n')
})
test('with alt', function() {
expect(md2conflu('![logo](http://github.com/logo.png)')).toBe('!http://github.com/logo.png|alt=logo!\n\n')
})
});

describe('horizontal rule', function() {
test('* * *', function() {
expect(md2conflu('* * *')).toBe('----')
})
test('***', function() {
expect(md2conflu('***')).toBe('----')
})
test('*****', function() {
expect(md2conflu('*****')).toBe('----')
})
test('---------------------------------------', function() {
expect(md2conflu('---------------------------------------')).toBe('----')
})
})
Loading