Skip to content

Commit

Permalink
Add replacements for Polar Bear markup language
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Jan 29, 2019
1 parent 819631c commit a9f2e6d
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 128 deletions.
21 changes: 21 additions & 0 deletions src/bear-style-code-block.js
@@ -0,0 +1,21 @@
export default function bearStyleCodeBlock (turndownService) {
turndownService.addRule('bearStyleCodeBlock', {
filter: function (node) {
let child = node.firstChild
return (
node.nodeName === 'PRE' &&
child &&
child.nodeName === 'CODE' &&
child.classList.contains('code-multiline')
)
},
replacement: function (content, node, options) {
let child = node.firstChild
return (
'\n```' + child.lang + '\n' +
child.textContent +
'\n' + '```\n'
)
}
})
}
33 changes: 33 additions & 0 deletions src/bear-style-todo.js
@@ -0,0 +1,33 @@
function selectTodoCheckbox (node) {
return node.firstElementChild
}

function selectTodoText (node) {
return node.firstElementChild && node.firstElementChild.nextElementSibling
}

export default function todo (turndownService) {
turndownService.addRule('todo', {
filter: function (node) {
var todoCheckboxElement = selectTodoCheckbox(node)
var todoTextElement = selectTodoText(node)

return node.nodeName === 'LI' &&
todoCheckboxElement &&
todoTextElement &&
todoCheckboxElement.classList.contains('todo-checkbox') &&
todoTextElement.classList.contains('todo-text')
},
replacement: function (content, node) {
var checked = selectTodoCheckbox(node)
.classList
.contains('todo-checked')

return (
checked
? '+ '
: '- '
) + content
}
})
}
8 changes: 8 additions & 0 deletions src/bold.js
@@ -0,0 +1,8 @@
export default function bold (turndownService) {
turndownService.addRule('bold', {
filter: ['b', 'strong'],
replacement: function (content) {
return '*' + content + '*'
}
})
}
15 changes: 0 additions & 15 deletions src/gfm.js

This file was deleted.

10 changes: 5 additions & 5 deletions src/highlighted-code-block.js → src/github-style-code-block.js
@@ -1,7 +1,7 @@
var highlightRegExp = /highlight-(?:text|source)-([a-z0-9]+)/

export default function highlightedCodeBlock (turndownService) {
turndownService.addRule('highlightedCodeBlock', {
export default function githubStyleCodeBlock (turndownService) {
turndownService.addRule('githubStyleCodeBlock', {
filter: function (node) {
var firstChild = node.firstChild
return (
Expand All @@ -16,9 +16,9 @@ export default function highlightedCodeBlock (turndownService) {
var language = (className.match(highlightRegExp) || [null, ''])[1]

return (
'\n\n' + options.fence + language + '\n' +
node.firstChild.textContent +
'\n' + options.fence + '\n\n'
'\n```' + language + '\n' +
node.firstChild.textContent +
'\n' + '```\n'
)
}
})
Expand Down
16 changes: 16 additions & 0 deletions src/github-style-todo.js
@@ -0,0 +1,16 @@
export default function todo (turndownService) {
turndownService.addRule('todo', {
filter: function (node) {
return node.nodeName === 'LI' &&
node.firstChild &&
node.firstChild.type === 'checkbox'
},
replacement: function (content, node) {
return (
node.firstChild.checked
? '+ '
: '- '
) + content
}
})
}
8 changes: 8 additions & 0 deletions src/italic.js
@@ -0,0 +1,8 @@
export default function italic (turndownService) {
turndownService.addRule('italic', {
filter: ['i', 'em'],
replacement: function (content) {
return '/' + content + '/'
}
})
}
8 changes: 8 additions & 0 deletions src/line-separator.js
@@ -0,0 +1,8 @@
export default function lineSeparator (turndownService) {
turndownService.addRule('lineSeparator', {
filter: ['hr'],
replacement: function () {
return '---'
}
})
}
8 changes: 8 additions & 0 deletions src/mark.js
@@ -0,0 +1,8 @@
export default function mark (turndownService) {
turndownService.addRule('mark', {
filter: ['mark'],
replacement: function (content) {
return '::' + content + '::'
}
})
}
39 changes: 39 additions & 0 deletions src/polar-bear.js
@@ -0,0 +1,39 @@
import bearStyleCodeBlock from './bear-style-code-block'
import bearStyleTodo from './bear-style-todo'
import bold from './bold'
import githubStyleCodeBlock from './github-style-code-block'
import githubStyleTodo from './github-style-todo'
import lineSeparator from './line-separator'
import italic from './italic'
import mark from './mark'
import strikethrough from './strikethrough'
import underline from './underline'

function bear (turndownService) {
turndownService.use([
bearStyleCodeBlock,
bearStyleTodo,
bold,
githubStyleCodeBlock,
githubStyleTodo,
italic,
lineSeparator,
mark,
strikethrough,
underline
])
}

export {
bear,
bearStyleCodeBlock,
bearStyleTodo,
bold,
githubStyleCodeBlock,
githubStyleTodo,
italic,
lineSeparator,
mark,
strikethrough,
underline
}
2 changes: 1 addition & 1 deletion src/strikethrough.js
Expand Up @@ -2,7 +2,7 @@ export default function strikethrough (turndownService) {
turndownService.addRule('strikethrough', {
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~' + content + '~'
return '-' + content + '-'
}
})
}
97 changes: 0 additions & 97 deletions src/tables.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/task-list-items.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/underline.js
@@ -0,0 +1,8 @@
export default function underline (turndownService) {
turndownService.addRule('underline', {
filter: ['u'],
replacement: function (content) {
return '_' + content + '_'
}
})
}

0 comments on commit a9f2e6d

Please sign in to comment.