@mdream/js 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import "./const.mjs";
2
- import { isInsideHeading } from "./utils.mjs";
3
- import { resolveUrl } from "./url.mjs";
2
+ import { isInsideHeading, resolveUrl } from "./url.mjs";
4
3
  const ALL_RULES = {
5
4
  urls: true,
6
5
  fragments: true,
@@ -11,4 +11,5 @@ export interface ExtractedElement extends ElementNode {
11
11
  }
12
12
  type ExtractionCallback = (element: ExtractedElement, state: PluginState) => void;
13
13
  /** Extract matching elements through the composable JavaScript plugin interface. */
14
- export declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): PluginSetup;
14
+ export declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): PluginSetup;
15
+ export {};
@@ -1,6 +1,5 @@
1
1
  import { TagIdMap } from "./const.mjs";
2
- import { escapeHtml, getLanguageFromClass, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger } from "./utils.mjs";
3
- import { resolveUrl, safeAnchorOutput } from "./url.mjs";
2
+ import { escapeHtml, getLanguageFromAttributes, hasNonWhitespace, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger, resolveUrl, safeAnchorOutput, suppressesFormattingInPre } from "./url.mjs";
4
3
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
5
4
  import { parserTagHandlers } from "./parse.mjs";
6
5
  import { resolvePlugins } from "./plugin.mjs";
@@ -44,7 +43,7 @@ function safeAttributes(node, tagId) {
44
43
  return start === void 0 ? "" : ` start="${start}"`;
45
44
  }
46
45
  if (tagId === 23) {
47
- const language = getLanguageFromClass(attributes.class);
46
+ const language = getLanguageFromAttributes(attributes);
48
47
  return language ? ` class="language-${escapeHtml(language, true)}"` : "";
49
48
  }
50
49
  if (tagId === 31 || tagId === 32) {
@@ -83,6 +82,8 @@ function processText(node, state, output) {
83
82
  if (!value) return;
84
83
  }
85
84
  }
85
+ const top = state.frames.at(-1);
86
+ if (top?._tag === 1 && top.languagePending && hasNonWhitespace(value)) top.languagePending = false;
86
87
  for (let index = state.frames.length - 1; index >= 0; index--) {
87
88
  const frame = state.frames[index];
88
89
  if (frame._tag === 0) {
@@ -113,9 +114,17 @@ function processHtmlOutputEvent(event, state, output, options) {
113
114
  const tagId = resolveTagId(element);
114
115
  const frame = state.frames.at(-1);
115
116
  if (frame?._tag === 1) {
116
- if (type === 0 && tagId === 23 && !frame.language) frame.language = getLanguageFromClass(element.attributes.class);
117
- else if (type === 0 && tagId === 6) frame.output += "\n";
118
- else if (type !== 0 && tagId === 34 && element.depth === frame.depth) closeFrame(state, output, frame);
117
+ if (type === 0) {
118
+ if (frame.languagePending && tagId !== 34 && !suppressesFormattingInPre(tagId)) {
119
+ frame.languagePending = false;
120
+ const parent = element.parent;
121
+ if (tagId === 23 && parent?.tagId === 34 && parent.depth === frame.depth) {
122
+ const codeLanguage = getLanguageFromAttributes(element.attributes);
123
+ if (codeLanguage) frame.language = codeLanguage;
124
+ }
125
+ }
126
+ if (tagId === 6) frame.output += "\n";
127
+ } else if (tagId === 34 && element.depth === frame.depth) closeFrame(state, output, frame);
119
128
  return;
120
129
  }
121
130
  if (type === 0 && tagId !== void 0 && tagId >= 7 && tagId <= 12) {
@@ -135,7 +144,8 @@ function processHtmlOutputEvent(event, state, output, options) {
135
144
  state.frames.push({
136
145
  _tag: 1,
137
146
  depth: element.depth,
138
- language: getLanguageFromClass(element.attributes.class),
147
+ language: getLanguageFromAttributes(element.attributes),
148
+ languagePending: true,
139
149
  output: ""
140
150
  });
141
151
  return;
@@ -231,7 +241,7 @@ function createProcessor(options) {
231
241
  processEvent(event) {
232
242
  state.depth = event.node.depth;
233
243
  if (event.node.type === 1 ? event.node.excludedFromMarkdown : event.node.parent?.excludedFromMarkdown) return;
234
- processHtmlOutputEvent(event, outputState, state.buffer, options);
244
+ processHtmlOutputEvent(event, outputState, state.buffer, state.options);
235
245
  },
236
246
  takeOutput() {
237
247
  const output = state.buffer.join("");