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

fix the highlighting pr #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ you will notice that this [README.md](https://raw.githubusercontent.com/tslide/t

---

## crude js syntax highlighting
## Advanced syntax highlighting

```md
# tslide
Expand All @@ -38,6 +38,15 @@ function fibonacci (n) {
‘‘‘

```

## JavaScript Example

```js
function fibonacci (n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
}
```

---

![Demo Code](demo-code.png)
Expand Down
44 changes: 39 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,45 @@
require('colors')

var charm = require('charm')(process.stdout)
var keypress = require('keypress')(process.stdin)
var opts = require('optimist').default('images', true).argv
var chalk = require('chalk')
var emojis = require('node-emoji')
var fs = require('fs')
var path = require('path')
var imgcat = require('ansi-escapes').image
var iq = require('insert-queue')
var js = require('hipster/highlight/javascript')
var imgcat = require('ansi-escapes').image
var emojis = require('node-emoji')
var keypress = require('keypress')(process.stdin)
var marked = require('marked')
var opts = require('optimist').default('images', true).default('legacy', false).argv
var path = require('path')
var TerminalRenderer = require('marked-terminal')

var file = opts._[0]
if (!file) {
console.error('USAGE: tslide [markdown-file]')
console.error()
console.error('--highlight Apply syntax highlighting (default true)')
console.error('--images Inline images if supported (default true)')
console.error('--legacy Resets to legacy rendering (default false)')
process.exit(1)
}

marked.setOptions({
renderer: new TerminalRenderer({
heading: function (text) {
return chalk.green.bold(text) + '\n';
},
firstHeading: function (text) {
return chalk.magenta.underline.bold(text) + '\n';
},
highlightOptions: {
theme: require('cardinal/themes/simple')
},
tab: 2
}, {
theme: require('cardinal/themes/simple')
})
})

var text = require('fs').readFileSync(file, 'utf-8')
var slides = text.split(/---+\n/)
if(slides.length <= 1) {
Expand Down Expand Up @@ -123,6 +144,18 @@ function indent(slide, indent) {
while (indent--)
space += ' '

if (opts.legacy)
return legacyStyling(slide, space)

if (highlight)
slide = marked(slide);

return slide.split('\n').map(function (l) {
return space + l
}).join('\n')
}

function legacyStyling(slide, space) {
var code = false
var inlineBold = /\*\*(.*)\*\*/g
return slide.split('\n').map(function (l) {
Expand All @@ -141,3 +174,4 @@ function indent(slide, indent) {
return space + l
}).join('\n')
}

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
},
"dependencies": {
"ansi-escapes": "^1.3.0",
"chalk": "^1.1.3",
"charm": "~0.1.0",
"color": "~0.4.1",
"colors": "~0.6.0-1",
"hipster": "~0.9.0",
"insert-queue": "0.0.4",
"keypress": "~0.1.0",
"markdown-sections": "~1.0.1",
"marked": "^0.3.6",
"marked-terminal": "^2.0.0",
"node-emoji": "^1.3.1",
"optimist": "~0.3.4"
},
Expand Down