-
-
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 #41 from marp-team/heading-divider
Support heading divider
- Loading branch information
Showing
10 changed files
with
413 additions
and
40 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 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,21 @@ | ||
/** @module */ | ||
import YAML, { FAILSAFE_SCHEMA } from 'js-yaml' | ||
|
||
/** | ||
* Parse text as YAML by using js-yaml's FAILSAFE_SCHEMA. | ||
* | ||
* @alias module:helpers/parse_yaml | ||
* @param {String} text Target text. | ||
*/ | ||
function parseYAML(text) { | ||
try { | ||
const obj = YAML.safeLoad(text, { schema: FAILSAFE_SCHEMA }) | ||
if (obj === null || typeof obj !== 'object') return false | ||
|
||
return obj | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
export default parseYAML |
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 Token from 'markdown-it/lib/token' | ||
import split from '../helpers/split' | ||
|
||
/** | ||
* Marpit heading divider plugin. | ||
* | ||
* Start a new slide page at before of headings by prepending hidden `<hr>` | ||
* elements. | ||
* | ||
* @alias module:markdown/heading_divider | ||
* @param {MarkdownIt} md markdown-it instance. | ||
* @param {Marpit} marpit Marpit instance. | ||
*/ | ||
function headingDivider(md, marpit) { | ||
md.core.ruler.before('marpit_slide', 'marpit_heading_divider', state => { | ||
let target = marpit.options.headingDivider | ||
|
||
if ( | ||
marpit.lastGlobalDirectives && | ||
Object.prototype.hasOwnProperty.call( | ||
marpit.lastGlobalDirectives, | ||
'headingDivider' | ||
) | ||
) | ||
target = marpit.lastGlobalDirectives.headingDivider | ||
|
||
if (state.inlineMode || target === false) return | ||
|
||
if (Number.isInteger(target) && target >= 1 && target <= 6) | ||
target = [...Array(target).keys()].map(i => i + 1) | ||
|
||
if (!Array.isArray(target)) return | ||
|
||
const splitTag = target.map(i => `h${i}`) | ||
const splitFunc = t => t.type === 'heading_open' && splitTag.includes(t.tag) | ||
|
||
state.tokens = split(state.tokens, splitFunc, true).reduce( | ||
(arr, slideTokens) => { | ||
const [firstToken] = slideTokens | ||
|
||
if ( | ||
!(firstToken && splitFunc(firstToken)) || | ||
arr.filter(t => !t.hidden).length === 0 | ||
) | ||
return [...arr, ...slideTokens] | ||
|
||
const token = new Token('hr', '', 0) | ||
token.hidden = true | ||
|
||
return [...arr, token, ...slideTokens] | ||
}, | ||
[] | ||
) | ||
}) | ||
} | ||
|
||
export default headingDivider |
Oops, something went wrong.