@marp-team/marpit
Version:
The skinny framework for creating slide deck from Markdown
232 lines (189 loc) • 8.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _markdownIt = _interopRequireDefault(require("markdown-it"));
var _wrap_array = _interopRequireDefault(require("./helpers/wrap_array"));
var _theme_set = _interopRequireDefault(require("./theme_set"));
var _element = require("./element");
var _apply = _interopRequireDefault(require("./markdown/directives/apply"));
var _background_image = _interopRequireDefault(require("./markdown/background_image"));
var _collect_comment = _interopRequireDefault(require("./markdown/collect_comment"));
var _comment = _interopRequireDefault(require("./markdown/comment"));
var _container = _interopRequireDefault(require("./markdown/container"));
var _header_and_footer = _interopRequireDefault(require("./markdown/header_and_footer"));
var _heading_divider = _interopRequireDefault(require("./markdown/heading_divider"));
var _inline_svg = _interopRequireDefault(require("./markdown/inline_svg"));
var _parse = _interopRequireDefault(require("./markdown/directives/parse"));
var _parse_image = _interopRequireDefault(require("./markdown/parse_image"));
var _slide = _interopRequireDefault(require("./markdown/slide"));
var _slide_container = _interopRequireDefault(require("./markdown/slide_container"));
var _assign = _interopRequireDefault(require("./markdown/style/assign"));
var _parse2 = _interopRequireDefault(require("./markdown/style/parse"));
var _sweep = _interopRequireDefault(require("./markdown/sweep"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const defaultOptions = {
backgroundSyntax: true,
container: _element.marpitContainer,
filters: true,
headingDivider: false,
inlineStyle: true,
looseYAML: false,
markdown: 'commonmark',
printable: true,
slideContainer: false,
inlineSVG: false
/**
* Parse Marpit Markdown and render to the slide HTML/CSS.
*/
};
class Marpit {
/**
* Create a Marpit instance.
*
* @param {Object} [opts]
* @param {boolean} [opts.backgroundSyntax=true] Support markdown image syntax
* with the alternate text including `bg`. Normally it converts into spot
* directives about background image. If `inlineSVG` is enabled, it
* supports the advanced backgrounds.
* @param {false|Element|Element[]}
* [opts.container={@link module:element.marpitContainer}] Container
* element(s) wrapping whole slide deck.
* @param {boolean} [opts.filters=true] Support filter syntax for markdown
* image. It can apply to inline image and the advanced backgrounds.
* @param {false|number|number[]} [opts.headingDivider=false] Start a new
* slide page at before of headings. it would apply to headings whose
* larger than or equal to the specified level if a number is given, or
* ONLY specified levels if a number array.
* @param {boolean} [opts.inlineStyle=true] Recognize `<style>` elements to
* append additional styles to theme. When it is `true`, Marpit will parse
* style regardless markdown-it's `html` option.
* @param {boolean} [opts.looseYAML=false] Allow loose YAML for directives.
* @param {string|Object|Array} [opts.markdown='commonmark'] markdown-it
* initialize option(s).
* @param {boolean} [opts.printable=true] Make style printable to PDF.
* @param {false|Element|Element[]} [opts.slideContainer] Container element(s)
* wrapping each slide sections.
* @param {boolean} [opts.inlineSVG=false] Wrap each sections by inline SVG.
* _(Experimental)_
*/
constructor(opts = {}) {
/**
* The current options for this instance.
*
* This property is read-only and marked as immutable. You cannot change the
* value of options after creating instance.
*
* @member {Object} options
* @memberOf Marpit#
* @readonly
*/
Object.defineProperty(this, 'options', {
enumerable: true,
value: Object.freeze(_objectSpread({}, defaultOptions, opts))
});
Object.defineProperties(this, {
containers: {
value: [...(0, _wrap_array.default)(this.options.container)]
},
slideContainers: {
value: [...(0, _wrap_array.default)(this.options.slideContainer)]
}
});
/**
* @type {ThemeSet}
*/
this.themeSet = new _theme_set.default();
/**
* @type {MarkdownIt}
*/
this.markdown = new _markdownIt.default(...(0, _wrap_array.default)(this.options.markdown));
this.applyMarkdownItPlugins();
}
/**
* The plugin interface of markdown-it for current Marpit instance.
*
* This is useful to integrate Marpit with the other markdown-it based parser.
*
* @type {Function}
* @readonly
*/
get markdownItPlugins() {
return this.applyMarkdownItPlugins.bind(this);
}
/** @private */
applyMarkdownItPlugins(md = this.markdown) {
const {
backgroundSyntax,
filters,
looseYAML
} = this.options;
md.use(_comment.default, {
looseYAML
}).use(_parse2.default, this).use(_slide.default).use(_parse.default, this, {
looseYAML
}).use(_apply.default).use(_header_and_footer.default).use(_heading_divider.default, this).use(_slide_container.default, this.slideContainers).use(_container.default, this.containers).use(_parse_image.default, {
filters
}).use(_sweep.default).use(_inline_svg.default, this).use(_assign.default, this).use(_collect_comment.default, this);
if (backgroundSyntax) md.use(_background_image.default);
}
/**
* @typedef {Object} Marpit~RenderResult
* @property {string} html Rendered HTML.
* @property {string} css Rendered CSS.
* @property {string[][]} comments Parsed HTML comments per slide pages,
* excepted YAML for directives. It would be useful for presenter notes.
*/
/**
* Render Markdown into HTML and CSS string.
*
* @param {string} markdown A Markdown string.
* @returns {Marpit~RenderResult} An object of rendering result.
*/
render(markdown) {
return {
html: this.renderMarkdown(markdown),
css: this.renderStyle(this.lastGlobalDirectives.theme),
comments: this.lastComments
};
}
/**
* Render Markdown by using `markdownIt#render`.
*
* This method is for internal. You can override this method if you have to
* render with customized way.
*
* @private
* @param {string} markdown A Markdown string.
* @returns {string} The result string of rendering Markdown.
*/
renderMarkdown(markdown) {
return this.markdown.render(markdown);
}
/**
* Render style by using `themeSet#pack`.
*
* This method is for internal.
*
* @private
* @param {string|undefined} theme Theme name.
* @returns {string} The result string of rendering style.
*/
renderStyle(theme) {
return this.themeSet.pack(theme, this.themeSetPackOptions());
}
/** @private */
themeSetPackOptions() {
return {
after: this.lastStyles ? this.lastStyles.join('\n') : undefined,
containers: [...this.containers, ...this.slideContainers],
inlineSVG: this.options.inlineSVG,
printable: this.options.printable
};
}
}
var _default = Marpit;
exports.default = _default;