Skip to content

Commit

Permalink
rebber: math support
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Jul 7, 2017
1 parent 91e7cb5 commit 1eb2a11
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 286 deletions.
10 changes: 10 additions & 0 deletions packages/rebber/__tests__/__snapshots__/index.js.snap
Expand Up @@ -11,6 +11,16 @@ a paragraph\\\\footnote{footnoteRawPar inner}

exports[`link with special characters 1`] = `"\\\\externalLink{foo}{http://example.com?a=b\\\\%c\\\\textasciicircum{}\\\\{\\\\}\\\\#foo}"`;
exports[`math 1`] = `
"A sentence ($S$) with \\\\textit{italic} and inline math ($C_L$) and $b$ another.
\\\\[ L = \\\\frac{1}{2} \\\\rho v^2 S C_L \\\\]
hehe
"
`;
exports[`mix-1 1`] = `
"(for convenience, are replaced with simple single spaces in the tests)
Expand Down
29 changes: 25 additions & 4 deletions packages/rebber/__tests__/index.js
Expand Up @@ -3,6 +3,7 @@ import {readdirSync as directory, readFileSync as file} from 'fs'
import {join} from 'path'
import unified from 'unified'
import reParse from 'remark-parse'
import remarkMath from 'remark-math'
import rebber from '../src'
import dedent from 'dedent'

Expand Down Expand Up @@ -36,6 +37,8 @@ const integrationConfig = {
warningCustomBlock: require('../src/custom-types/customBlocks'),
gridTable: require('../src/custom-types/gridTable'),
abbr: require('../src/custom-types/abbr'),
math: require('../src/custom-types/math'),
inlineMath: require('../src/custom-types/math'),
},
emoticons: emoticons,
}
Expand Down Expand Up @@ -287,13 +290,31 @@ test('footnotes', () => {
.use(reParse, {footnotes: true})
.use(rebber, integrationConfig)
.processSync(dedent`
# mytitle[^footnoteRef]
# mytitle[^footnoteRef]
[^fotnoteRef]: reference in title
[^fotnoteRef]: reference in title
# mytitle[^footnoterawhead inner]
# mytitle[^footnoterawhead inner]
a paragraph[^footnoteRawPar inner]`)
a paragraph[^footnoteRawPar inner]
`)
expect(contents).toMatchSnapshot()
})

test('math', () => {
const {contents} = unified()
.use(reParse)
.use(remarkMath)
.use(rebber, integrationConfig)
.processSync(dedent`
A sentence ($S$) with *italic* and inline math ($C_L$) and $$b$$ another.
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
hehe
`)
expect(contents).toMatchSnapshot()
})

Expand Down
38 changes: 38 additions & 0 deletions packages/rebber/dist/custom-types/math.js
@@ -0,0 +1,38 @@
'use strict';

/* Dependencies. */
var all = require('../all');
var one = require('../one');
var has = require('has');

/* Expose. */
module.exports = math;

var defaultMacros = {
inlineMath: function inlineMath(content) {
return '$' + content + '$';
},
inlineMathDouble: function inlineMathDouble(content) {
return '$$' + content + '$$';
},
math: function math(content) {
return '\\[ ' + content + ' \\]\n\n';
}

/* Stringify a Figure `node`. */
};function math(ctx, node, index, parent) {
var type = 'math';
if (node.type === 'inlineMath') {
try {
var classes = node.data.hProperties.className.split(' ');
type = classes.includes('inlineMathDouble') ? 'inlineMathDouble' : 'inlineMath';
} catch (e) {
console.error(e, 'This rebber math plugin is only compatible with remark-math.');
}
}

var macro = has(ctx, 'math') && has(ctx.math, type) && ctx.math[type] || has(defaultMacros, type) && defaultMacros[type];

var content = all(ctx, node) || node.value || '';
return macro(content.trim());
}
32 changes: 32 additions & 0 deletions packages/rebber/src/custom-types/math.js
@@ -0,0 +1,32 @@
/* Dependencies. */
const all = require('../all')
const one = require('../one')
const has = require('has')

/* Expose. */
module.exports = math

const defaultMacros = {
inlineMath: (content) => `$${content}$`,
inlineMathDouble: (content) => `$$${content}$$`,
math: (content) => `\\[ ${content} \\]\n\n`,
}

/* Stringify a Figure `node`. */
function math (ctx, node, index, parent) {
let type = 'math'
if (node.type === 'inlineMath') {
try {
const classes = node.data.hProperties.className.split(' ')
type = classes.includes('inlineMathDouble') ? 'inlineMathDouble' : 'inlineMath'
} catch (e) {
console.error(e, 'This rebber math plugin is only compatible with remark-math.')
}
}

const macro = (has(ctx, 'math') && has(ctx.math, type) && ctx.math[type]) ||
(has(defaultMacros, type) && defaultMacros[type])

const content = all(ctx, node) || node.value || ''
return macro(content.trim())
}
2 changes: 2 additions & 0 deletions packages/zmarkdown/index.js
Expand Up @@ -47,6 +47,8 @@ const rebberConfig = {
questionCustomBlock: require('rebber/dist/custom-types/customBlocks'),
abbr: require('rebber/dist/custom-types/abbr'),
gridTable: require('rebber/dist/custom-types/gridTable'),
math: require('rebber/src/custom-types/math'),
inlineMath: require('rebber/src/custom-types/math'),
},
emoticons: defaultConfig.emoticons,
link: {
Expand Down
13 changes: 6 additions & 7 deletions packages/zmarkdown/inspect.js
Expand Up @@ -7,18 +7,17 @@ const {
transform,
renderFile,
renderString
} = require('./index')(config, 'html')
} = require('./index')(config, 'latex')

const input = dedent`
**foo**
A sentence ($S$) with *italic*[^italic] and inline match ($C_L$) and $$b$$ another[^bar] footnote.
<<a>>
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
<<z>>
hehe
<< a >>
<< z >>
`

console.log(inspect(parse(input)))
Expand Down
633 changes: 360 additions & 273 deletions packages/zmarkdown/public/bundle.js

Large diffs are not rendered by default.

62 changes: 60 additions & 2 deletions packages/zmarkdown/public/bundle.js.map

Large diffs are not rendered by default.

0 comments on commit 1eb2a11

Please sign in to comment.