canvas-native
Advanced tools
Comparing version 2.3.4 to 2.4.0
@@ -11,2 +11,6 @@ 'use strict'; | ||
var brackets = BRACKETS; | ||
exports.brackets = brackets; | ||
function stripPrefix(text) { | ||
@@ -13,0 +17,0 @@ return text.replace(PREFIX_REGEX, ''); |
@@ -18,3 +18,3 @@ 'use strict'; | ||
var context = { groupType: null, hasTitle: false }; | ||
var context = { groupType: null, groupLang: null, hasTitle: false }; | ||
@@ -40,7 +40,10 @@ var didNewLine = false; | ||
if (/^```.*/.test(source)) { | ||
var fenceMatch = undefined; | ||
if (fenceMatch = source.match(/^```(.*)$/)) { | ||
if (context.groupType === 'code') { | ||
context.groupType = null; | ||
context.groupLang = null; | ||
} else { | ||
context.groupType = 'code'; | ||
context.groupLang = fenceMatch[1]; | ||
} | ||
@@ -47,0 +50,0 @@ |
@@ -9,3 +9,3 @@ 'use strict'; | ||
var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -38,3 +38,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
type: this.type, | ||
content: this.content | ||
content: this.content, | ||
meta: { | ||
language: this.language | ||
} | ||
}; | ||
@@ -45,3 +48,3 @@ } | ||
value: function toMarkdown(prev, next) { | ||
var result = this.match[2]; | ||
var result = this.content; | ||
@@ -65,3 +68,3 @@ if (!prev || prev.type !== this.type) { | ||
get: function get() { | ||
return this.match[2]; | ||
return this.match[3]; | ||
} | ||
@@ -73,6 +76,19 @@ }, { | ||
} | ||
}, { | ||
key: 'language', | ||
get: function get() { | ||
return this.match[2] || null; | ||
} | ||
}], [{ | ||
key: 'buildPrefix', | ||
value: function buildPrefix() { | ||
return (0, _brackets.wrap)('code'); | ||
value: function buildPrefix(markdown) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var groupLang = _ref.groupLang; | ||
if (groupLang) { | ||
return (0, _brackets.wrap)('code-' + groupLang); | ||
} else { | ||
return (0, _brackets.wrap)('code'); | ||
} | ||
} | ||
@@ -82,5 +98,6 @@ }, { | ||
value: function matchMarkdown(markdown) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var _ref2 = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var groupType = _ref.groupType; | ||
var groupType = _ref2.groupType; | ||
var groupLang = _ref2.groupLang; | ||
@@ -107,3 +124,4 @@ if (groupType !== 'code') { | ||
get: function get() { | ||
return new RegExp('^(' + this.buildPrefix() + ')(.*)$'); | ||
var prefix = (0, _brackets.wrap)('code(?:-([^' + _brackets.brackets[1] + ']*))?'); | ||
return new RegExp('^(' + prefix + ')(.*)$'); | ||
} | ||
@@ -110,0 +128,0 @@ }]); |
@@ -72,2 +72,4 @@ 'use strict'; | ||
value: function matchMarkdown(markdown) { | ||
var context = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var mdMatch = markdown.match(this.markdownPattern); | ||
@@ -79,3 +81,3 @@ | ||
var nativeString = this.buildPrefix(markdown) + markdown; | ||
var nativeString = this.buildPrefix(markdown, context) + markdown; | ||
return this.match(nativeString); | ||
@@ -82,0 +84,0 @@ } |
const BRACKETS = ['⧙', '⧘']; | ||
const PREFIX_REGEX = new RegExp(`^${BRACKETS[0]}.*?${BRACKETS[1]}`); | ||
export const brackets = BRACKETS; | ||
export function stripPrefix(text) { | ||
@@ -5,0 +7,0 @@ return text.replace(PREFIX_REGEX, ''); |
@@ -7,3 +7,3 @@ import PARSE_ORDER from './parse-order'; | ||
const context = { groupType: null, hasTitle: false }; | ||
const context = { groupType: null, groupLang: null, hasTitle: false }; | ||
@@ -22,7 +22,10 @@ let didNewLine = false; | ||
if (/^```.*/.test(source)) { | ||
let fenceMatch; | ||
if ((fenceMatch = source.match(/^```(.*)$/))) { | ||
if (context.groupType === 'code') { | ||
context.groupType = null; | ||
context.groupLang = null; | ||
} else { | ||
context.groupType = 'code'; | ||
context.groupLang = fenceMatch[1]; | ||
} | ||
@@ -29,0 +32,0 @@ |
import Type from './type'; | ||
import { wrap } from '../brackets'; | ||
import { brackets, wrap } from '../brackets'; | ||
export default class Code extends Type { | ||
get content() { | ||
return this.match[2]; | ||
return this.match[3]; | ||
} | ||
@@ -13,2 +13,6 @@ | ||
get language() { | ||
return this.match[2] || null; | ||
} | ||
toJSON() { | ||
@@ -18,2 +22,5 @@ return { | ||
content: this.content, | ||
meta : { | ||
language: this.language | ||
} | ||
}; | ||
@@ -23,3 +30,3 @@ } | ||
toMarkdown(prev, next) { | ||
let result = this.match[2]; | ||
let result = this.content; | ||
@@ -50,10 +57,15 @@ if (!prev || prev.type !== this.type) { | ||
static get nativePattern() { | ||
return new RegExp(`^(${this.buildPrefix()})(.*)$`); | ||
const prefix = wrap(`code(?:-([^${brackets[1]}]*))?`) | ||
return new RegExp(`^(${prefix})(.*)$`); | ||
} | ||
static buildPrefix() { | ||
return wrap('code'); | ||
static buildPrefix(markdown, { groupLang } = {}) { | ||
if (groupLang) { | ||
return wrap(`code-${groupLang}`); | ||
} else { | ||
return wrap('code'); | ||
} | ||
} | ||
static matchMarkdown(markdown, { groupType } = {}) { | ||
static matchMarkdown(markdown, { groupType, groupLang } = {}) { | ||
if (groupType !== 'code') { | ||
@@ -60,0 +72,0 @@ return null; |
@@ -60,3 +60,3 @@ export default class Type { | ||
static matchMarkdown(markdown) { | ||
static matchMarkdown(markdown, context = {}) { | ||
let mdMatch = markdown.match(this.markdownPattern); | ||
@@ -68,3 +68,3 @@ | ||
const nativeString = this.buildPrefix(markdown) + markdown; | ||
const nativeString = this.buildPrefix(markdown, context) + markdown; | ||
return this.match(nativeString); | ||
@@ -71,0 +71,0 @@ } |
{ | ||
"name": "canvas-native", | ||
"description": "Utilities for working with the native Canvas format", | ||
"version": "2.3.4", | ||
"version": "2.4.0", | ||
"author": "Jonathan Clem <jonathan@usecanvas.com>", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/usecanvas/canvas-native/issues", |
@@ -294,2 +294,3 @@ import format from '../../../lib/formatters/json'; | ||
content: 'defmodule Foo', | ||
meta: { language: null } | ||
}, | ||
@@ -299,2 +300,3 @@ { | ||
content: 'end', | ||
meta: { language: null } | ||
}, | ||
@@ -301,0 +303,0 @@ ] |
@@ -20,3 +20,3 @@ import parse from '../../../lib/parsers/markdown'; | ||
\`\`\` | ||
\`\`\`ruby | ||
class Foo | ||
@@ -30,2 +30,6 @@ def bar | ||
\`\`\` | ||
no language | ||
\`\`\` | ||
> Foo | ||
@@ -46,8 +50,9 @@ > Bar | ||
`${wrap('ordered-list-0')}1. Qux`, | ||
`${wrap('code')}class Foo`, | ||
`${wrap('code')} def bar`, | ||
`${wrap('code')}`, | ||
`${wrap('code')}`, | ||
`${wrap('code')} end`, | ||
`${wrap('code')}end`, | ||
`${wrap('code-ruby')}class Foo`, | ||
`${wrap('code-ruby')} def bar`, | ||
`${wrap('code-ruby')}`, | ||
`${wrap('code-ruby')}`, | ||
`${wrap('code-ruby')} end`, | ||
`${wrap('code-ruby')}end`, | ||
`${wrap('code')}no language`, | ||
`${wrap('blockquote')}> Foo`, | ||
@@ -54,0 +59,0 @@ `${wrap('blockquote')}> Bar`, |
@@ -12,2 +12,7 @@ import Code from '../../../lib/types/code'; | ||
}); | ||
it('matches code with a language', () => { | ||
const source = `${wrap('code-ruby')}Code`; | ||
expect(Code.match(source)).to.be.an.instanceof(Code); | ||
}); | ||
}); | ||
@@ -30,6 +35,7 @@ | ||
it('serializes to JSON', () => { | ||
const line = Code.match(wrap('code') + 'alert("ok");'); | ||
const line = Code.match(wrap('code-ruby') + 'alert("ok");'); | ||
expect(line.toJSON()).to.eql({ | ||
type : 'code', | ||
content: 'alert("ok");', | ||
meta : { language: 'ruby' } | ||
}); | ||
@@ -36,0 +42,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125918
3565