-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from marp-team/lazy-yaml
Lazy yaml support
- Loading branch information
Showing
11 changed files
with
175 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** @module */ | ||
import YAML, { FAILSAFE_SCHEMA } from 'js-yaml' | ||
import directives from './directives' | ||
|
||
/** | ||
* Parse text as YAML by using js-yaml's FAILSAFE_SCHEMA. | ||
* | ||
* @alias module:markdown/directives/yaml | ||
* @param {String} text Target text. | ||
* @param {boolean} allowLazy By `true`, it try to parse lazy YAML at first. | ||
* @returns {Object|false} Return parse result, or `false` when failed to parse. | ||
*/ | ||
|
||
const keyPattern = `[_$]?(?:${directives.join('|')})` | ||
const directivesMatcher = new RegExp(`^\\s*(${keyPattern})\\s*:(.*)$`) | ||
const specialChars = `["'{|>~&*` | ||
const whitespaceMatcher = /^\s*$/ | ||
|
||
function strict(text) { | ||
try { | ||
const obj = YAML.safeLoad(text, { schema: FAILSAFE_SCHEMA }) | ||
if (obj === null || typeof obj !== 'object') return false | ||
|
||
return obj | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
function lazy(text) { | ||
const collected = {} | ||
const lines = text.split(/\r?\n/) | ||
|
||
return lines.every(line => { | ||
if (whitespaceMatcher.test(line)) return true | ||
|
||
const matched = directivesMatcher.exec(line) | ||
if (!matched) return false | ||
|
||
const [, directive, originalValue] = matched | ||
const value = originalValue.trim() | ||
if (specialChars.includes(value[0])) return false | ||
|
||
collected[directive] = value | ||
return true | ||
}) | ||
? collected | ||
: false | ||
} | ||
|
||
export default function(text, allowLazy) { | ||
if (allowLazy) { | ||
const lazyResult = lazy(text) | ||
if (lazyResult !== false) return lazyResult | ||
} | ||
|
||
return strict(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import dedent from 'dedent' | ||
import yaml from '../../../src/markdown/directives/yaml' | ||
|
||
describe('Marpit directives YAML parser', () => { | ||
it("ignores directive's special char with false allowLazy option", () => | ||
expect(yaml('color: #f00', false).color).toBeFalsy()) | ||
|
||
context('with allowLazy option as true', () => { | ||
it("parses directive's special char as string", () => | ||
expect(yaml('color: #f00', true).color).toBe('#f00')) | ||
|
||
it('fallbacks to regular YAML parser when passed like strict YAML', () => { | ||
const confirm = text => | ||
expect(yaml(text, true)).toMatchObject(yaml(text, false)) | ||
|
||
confirm('headingDivider: [3]') | ||
confirm('backgroundPosition: "left center"') | ||
confirm("backgroundSize: '100px 200px'") | ||
confirm(dedent` | ||
color: #f00 | ||
notSupported: key | ||
`) | ||
confirm(dedent` | ||
class: | ||
- first | ||
- second | ||
`) | ||
confirm(dedent` | ||
header: > | ||
Hello, | ||
world! | ||
`) | ||
confirm(dedent` | ||
footer: | | ||
Multiline | ||
footer | ||
`) | ||
confirm(dedent` | ||
class: &anchored klass | ||
_class: *anchored | ||
`) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.