This commit is contained in:
counterweight 2026-05-14 10:56:04 +02:00
parent bc92231240
commit b8225c639e
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
11904 changed files with 1472749 additions and 133 deletions

View file

@ -0,0 +1,128 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.assign = void 0;
var _postcss = _interopRequireDefault(require("postcss"));
var _postcss_plugin = _interopRequireDefault(require("../../helpers/postcss_plugin"));
var _plugin = _interopRequireDefault(require("../../plugin"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** @module */
const uniqKeyChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const uniqKeyCharsLength = uniqKeyChars.length;
const generateScopeAttr = uniqKey => `data-marpit-scope-${uniqKey}`;
const generateUniqKey = (length = 8) => {
let ret = '';
for (let i = 0; i < length; i += 1) ret += uniqKeyChars[Math.floor(Math.random() * uniqKeyCharsLength)];
return ret;
};
const injectScopePostCSSplugin = (0, _postcss_plugin.default)('marpit-style-assign-postcss-inject-scope', (key, keyframeSet) => css => css.each(function inject(node) {
const {
type,
name
} = node;
if (type === 'atrule') {
if (name === 'keyframes' && node.params) {
keyframeSet.add(node.params);
node.params += `-${key}`;
} else if (name === 'media' || name === 'supports') {
node.each(inject);
}
} else if (type === 'rule') {
node.selectors = node.selectors.map(selector => {
const injectSelector = /^section(?![\w-])/.test(selector) ? selector.slice(7) : ` ${selector}`;
return `section[${generateScopeAttr(key)}]${injectSelector}`;
});
}
}));
const scopeKeyframesPostCSSPlugin = (0, _postcss_plugin.default)('marpit-style-assign-postcss-scope-keyframes', (key, keyframeSet) => css => {
if (keyframeSet.size === 0) return;
const keyframeMatcher = new RegExp(`\\b(${[...keyframeSet.values()].map(kf => kf.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&')).join('|')})(?!\\()\\b`);
css.walkDecls(/^animation(-name)?$/, decl => {
decl.value = decl.value.replace(keyframeMatcher, kf => `${kf}-${key}`);
});
});
/**
* Marpit style assign plugin.
*
* Assign style global directive and parsed styles to Marpit instance's
* `lastStyles' property.
*
* @function assign
* @param {MarkdownIt} md markdown-it instance.
*/
function _assign(md) {
const {
marpit
} = md;
md.core.ruler.after('marpit_slide', 'marpit_style_assign', state => {
if (state.inlineMode) return;
const directives = marpit.lastGlobalDirectives || {};
marpit.lastStyles = directives.style ? [directives.style] : [];
let current;
for (const token of state.tokens) {
if (token.meta && token.meta.marpitSlideElement === 1) {
current = token;
} else if (token.meta && token.meta.marpitSlideElement === -1) {
if (current.meta && current.meta.marpitStyleScoped) {
const {
key,
keyframeSet,
styles
} = current.meta.marpitStyleScoped;
// Rewrite keyframes name in animation decls
const processor = (0, _postcss.default)([scopeKeyframesPostCSSPlugin(key, keyframeSet)]);
current.meta.marpitStyleScoped.styles = styles.map(style => {
try {
return processor.process(style).css;
} catch {
return style;
}
});
// Assign scoped styles
marpit.lastStyles.push(...current.meta.marpitStyleScoped.styles);
}
current = undefined;
} else if (token.type === 'marpit_style') {
const {
content
} = token;
// Scoped style
const {
marpitStyleScoped
} = token.meta || {};
if (current && marpitStyleScoped) {
current.meta = current.meta || {};
current.meta.marpitStyleScoped = current.meta.marpitStyleScoped || {};
let {
key
} = current.meta.marpitStyleScoped;
if (!key) {
key = generateUniqKey();
current.meta.marpitStyleScoped.key = key;
current.attrSet(generateScopeAttr(key), '');
}
current.meta.marpitStyleScoped.styles = current.meta.marpitStyleScoped.styles || [];
current.meta.marpitStyleScoped.keyframeSet = current.meta.marpitStyleScoped.keyframeSet || new Set();
const processor = (0, _postcss.default)([injectScopePostCSSplugin(key, current.meta.marpitStyleScoped.keyframeSet)]);
try {
current.meta.marpitStyleScoped.styles.push(processor.process(content).css);
} catch {
// No ops
}
} else if (content) {
// Global style
marpit.lastStyles.push(content);
}
}
}
});
}
const assign = exports.assign = (0, _plugin.default)(_assign);
var _default = exports.default = assign;

View file

@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse = exports.default = void 0;
var _plugin = _interopRequireDefault(require("../../plugin"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** @module */
const styleMatcher = /<style([\s\S]*?)>([\s\S]*?)<\/style>/i;
const styleMatcherOpening = /^<style(?=(\s|>|$))/i;
const styleMatcherClosing = /<\/style>/i;
const styleMatcherScoped = /\bscoped\b/i;
/**
* Marpit style parse plugin.
*
* Parse `<style>` elements as the hidden `marpit_style` token. The parsed style
* will use in {@link ThemeSet#pack} to append the style additionally.
*
* `<style>` elements will strip regardless of html setting provided by
* markdown-it.
*
* @function parse
* @param {MarkdownIt} md markdown-it instance.
*/
function _parse(md) {
/**
* Based on markdown-it html_block rule
* https://github.com/markdown-it/markdown-it/blob/master/lib/rules_block/html_block.js
*/
md.block.ruler.before('html_block', 'marpit_style_parse', (state, startLine, endLine, silent) => {
// Fast fail
let pos = state.bMarks[startLine] + state.tShift[startLine];
if (state.src.charCodeAt(pos) !== 0x3c) return false;
let max = state.eMarks[startLine];
let line = state.src.slice(pos, max);
// Match to opening element
if (!styleMatcherOpening.test(line)) return false;
if (silent) return true;
// Parse ending element
let nextLine = startLine + 1;
if (!styleMatcherClosing.test(line)) {
while (nextLine < endLine) {
if (state.sCount[nextLine] < state.blkIndent) break;
pos = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
line = state.src.slice(pos, max);
nextLine += 1;
if (styleMatcherClosing.test(line)) break;
}
}
state.line = nextLine;
// Create token
const token = state.push('marpit_style', '', 0);
token.map = [startLine, nextLine];
token.markup = state.getLines(startLine, nextLine, state.blkIndent, true);
token.meta = {};
token.hidden = true;
const matchedContent = styleMatcher.exec(token.markup);
if (matchedContent) {
const [, attrStr, contentStr] = matchedContent;
token.content = contentStr.trim();
token.meta.marpitStyleScoped = styleMatcherScoped.test(attrStr.trim());
}
return true;
});
}
const parse = exports.parse = (0, _plugin.default)(_parse);
var _default = exports.default = parse;