@wdprlib/render 1.2.1 → 1.3.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.
package/dist/index.cjs CHANGED
@@ -44,12 +44,15 @@ var __export = (target, all) => {
44
44
  var exports_src = {};
45
45
  __export(exports_src, {
46
46
  renderToHtml: () => renderToHtml,
47
- createSettings: () => import_ast3.createSettings,
48
- DEFAULT_SETTINGS: () => import_ast3.DEFAULT_SETTINGS,
47
+ createSettings: () => import_ast4.createSettings,
48
+ DEFAULT_SETTINGS: () => import_ast4.DEFAULT_SETTINGS,
49
49
  DEFAULT_EMBED_ALLOWLIST: () => DEFAULT_EMBED_ALLOWLIST
50
50
  });
51
51
  module.exports = __toCommonJS(exports_src);
52
52
 
53
+ // packages/render/src/render.ts
54
+ var import_ast3 = require("@wdprlib/ast");
55
+
53
56
  // packages/render/src/context.ts
54
57
  var import_ast = require("@wdprlib/ast");
55
58
 
@@ -424,6 +427,9 @@ function sanitizeAttributes(attributes) {
424
427
  // packages/render/src/context.ts
425
428
  class RenderContext {
426
429
  chunks = [];
430
+ renderInlineStyles = false;
431
+ _styleSlotId = null;
432
+ _styleSlotContents = new Map;
427
433
  _tocIndex = 0;
428
434
  _footnoteIndex = 0;
429
435
  _equationIndex = 0;
@@ -479,6 +485,26 @@ class RenderContext {
479
485
  getOutput() {
480
486
  return this.chunks.join("");
481
487
  }
488
+ enterStyleSlot(slotId) {
489
+ this._styleSlotId = slotId;
490
+ if (!this._styleSlotContents.has(slotId)) {
491
+ this._styleSlotContents.set(slotId, []);
492
+ }
493
+ }
494
+ exitStyleSlot() {
495
+ this._styleSlotId = null;
496
+ }
497
+ hasActiveStyleSlot() {
498
+ return this._styleSlotId !== null;
499
+ }
500
+ pushToStyleSlot(css) {
501
+ if (this._styleSlotId !== null) {
502
+ this._styleSlotContents.get(this._styleSlotId).push(css);
503
+ }
504
+ }
505
+ getStyleSlotContents(slotId) {
506
+ return this._styleSlotContents.get(slotId) ?? [];
507
+ }
482
508
  nextTocIndex() {
483
509
  return this._tocIndex++;
484
510
  }
@@ -4616,13 +4642,20 @@ function evaluateIfTagsCondition(condition, pageTags) {
4616
4642
  const optional = [];
4617
4643
  for (const token of tokens) {
4618
4644
  if (token.startsWith("+")) {
4619
- required.push(token.slice(1).toLowerCase());
4645
+ const tag = token.slice(1).toLowerCase();
4646
+ if (tag)
4647
+ required.push(tag);
4620
4648
  } else if (token.startsWith("-")) {
4621
- excluded.push(token.slice(1).toLowerCase());
4649
+ const tag = token.slice(1).toLowerCase();
4650
+ if (tag)
4651
+ excluded.push(tag);
4622
4652
  } else {
4623
4653
  optional.push(token.toLowerCase());
4624
4654
  }
4625
4655
  }
4656
+ if (required.length === 0 && excluded.length === 0 && optional.length === 0) {
4657
+ return false;
4658
+ }
4626
4659
  for (const tag of required) {
4627
4660
  if (!pageTagSet.has(tag))
4628
4661
  return false;
@@ -4641,7 +4674,17 @@ function evaluateIfTagsCondition(condition, pageTags) {
4641
4674
  function renderIfTags(ctx, data) {
4642
4675
  const pageTags = ctx.page?.tags ?? [];
4643
4676
  if (evaluateIfTagsCondition(data.condition, pageTags)) {
4677
+ const prev = ctx.renderInlineStyles;
4678
+ ctx.renderInlineStyles = true;
4679
+ const slotId = data._styleSlot;
4680
+ if (slotId !== undefined) {
4681
+ ctx.enterStyleSlot(slotId);
4682
+ }
4644
4683
  renderElements(ctx, data.elements);
4684
+ if (slotId !== undefined) {
4685
+ ctx.exitStyleSlot();
4686
+ }
4687
+ ctx.renderInlineStyles = prev;
4645
4688
  }
4646
4689
  }
4647
4690
 
@@ -5085,7 +5128,14 @@ function renderToHtml(tree, options = {}) {
5085
5128
  renderElements(ctx, tree.elements);
5086
5129
  if (ctx.settings.allowStyleElements && tree.styles?.length) {
5087
5130
  for (const style of tree.styles) {
5088
- ctx.push(`<style>${escapeStyleContent(style)}</style>`);
5131
+ if (style.startsWith(import_ast3.STYLE_SLOT_PREFIX)) {
5132
+ const slotId = parseInt(style.slice(import_ast3.STYLE_SLOT_PREFIX.length), 10);
5133
+ for (const css of ctx.getStyleSlotContents(slotId)) {
5134
+ ctx.push(`<style>${escapeStyleContent(css)}</style>`);
5135
+ }
5136
+ } else {
5137
+ ctx.push(`<style>${escapeStyleContent(style)}</style>`);
5138
+ }
5089
5139
  }
5090
5140
  }
5091
5141
  return ctx.getOutput();
@@ -5197,6 +5247,13 @@ function renderElement(ctx, element) {
5197
5247
  renderIfTags(ctx, element.data);
5198
5248
  break;
5199
5249
  case "style":
5250
+ if (ctx.renderInlineStyles && ctx.settings.allowStyleElements) {
5251
+ if (ctx.hasActiveStyleSlot()) {
5252
+ ctx.pushToStyleSlot(element.data);
5253
+ } else {
5254
+ ctx.push(`<style>${escapeStyleContent(element.data)}</style>`);
5255
+ }
5256
+ }
5200
5257
  break;
5201
5258
  case "line-break":
5202
5259
  ctx.push("<br />");
@@ -5229,4 +5286,4 @@ function renderElement(ctx, element) {
5229
5286
  }
5230
5287
 
5231
5288
  // packages/render/src/index.ts
5232
- var import_ast3 = require("@wdprlib/ast");
5289
+ var import_ast4 = require("@wdprlib/ast");
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ // packages/render/src/render.ts
2
+ import { STYLE_SLOT_PREFIX } from "@wdprlib/ast";
3
+
1
4
  // packages/render/src/context.ts
2
5
  import { DEFAULT_SETTINGS } from "@wdprlib/ast";
3
6
 
@@ -372,6 +375,9 @@ function sanitizeAttributes(attributes) {
372
375
  // packages/render/src/context.ts
373
376
  class RenderContext {
374
377
  chunks = [];
378
+ renderInlineStyles = false;
379
+ _styleSlotId = null;
380
+ _styleSlotContents = new Map;
375
381
  _tocIndex = 0;
376
382
  _footnoteIndex = 0;
377
383
  _equationIndex = 0;
@@ -427,6 +433,26 @@ class RenderContext {
427
433
  getOutput() {
428
434
  return this.chunks.join("");
429
435
  }
436
+ enterStyleSlot(slotId) {
437
+ this._styleSlotId = slotId;
438
+ if (!this._styleSlotContents.has(slotId)) {
439
+ this._styleSlotContents.set(slotId, []);
440
+ }
441
+ }
442
+ exitStyleSlot() {
443
+ this._styleSlotId = null;
444
+ }
445
+ hasActiveStyleSlot() {
446
+ return this._styleSlotId !== null;
447
+ }
448
+ pushToStyleSlot(css) {
449
+ if (this._styleSlotId !== null) {
450
+ this._styleSlotContents.get(this._styleSlotId).push(css);
451
+ }
452
+ }
453
+ getStyleSlotContents(slotId) {
454
+ return this._styleSlotContents.get(slotId) ?? [];
455
+ }
430
456
  nextTocIndex() {
431
457
  return this._tocIndex++;
432
458
  }
@@ -4564,13 +4590,20 @@ function evaluateIfTagsCondition(condition, pageTags) {
4564
4590
  const optional = [];
4565
4591
  for (const token of tokens) {
4566
4592
  if (token.startsWith("+")) {
4567
- required.push(token.slice(1).toLowerCase());
4593
+ const tag = token.slice(1).toLowerCase();
4594
+ if (tag)
4595
+ required.push(tag);
4568
4596
  } else if (token.startsWith("-")) {
4569
- excluded.push(token.slice(1).toLowerCase());
4597
+ const tag = token.slice(1).toLowerCase();
4598
+ if (tag)
4599
+ excluded.push(tag);
4570
4600
  } else {
4571
4601
  optional.push(token.toLowerCase());
4572
4602
  }
4573
4603
  }
4604
+ if (required.length === 0 && excluded.length === 0 && optional.length === 0) {
4605
+ return false;
4606
+ }
4574
4607
  for (const tag of required) {
4575
4608
  if (!pageTagSet.has(tag))
4576
4609
  return false;
@@ -4589,7 +4622,17 @@ function evaluateIfTagsCondition(condition, pageTags) {
4589
4622
  function renderIfTags(ctx, data) {
4590
4623
  const pageTags = ctx.page?.tags ?? [];
4591
4624
  if (evaluateIfTagsCondition(data.condition, pageTags)) {
4625
+ const prev = ctx.renderInlineStyles;
4626
+ ctx.renderInlineStyles = true;
4627
+ const slotId = data._styleSlot;
4628
+ if (slotId !== undefined) {
4629
+ ctx.enterStyleSlot(slotId);
4630
+ }
4592
4631
  renderElements(ctx, data.elements);
4632
+ if (slotId !== undefined) {
4633
+ ctx.exitStyleSlot();
4634
+ }
4635
+ ctx.renderInlineStyles = prev;
4593
4636
  }
4594
4637
  }
4595
4638
 
@@ -5033,7 +5076,14 @@ function renderToHtml(tree, options = {}) {
5033
5076
  renderElements(ctx, tree.elements);
5034
5077
  if (ctx.settings.allowStyleElements && tree.styles?.length) {
5035
5078
  for (const style of tree.styles) {
5036
- ctx.push(`<style>${escapeStyleContent(style)}</style>`);
5079
+ if (style.startsWith(STYLE_SLOT_PREFIX)) {
5080
+ const slotId = parseInt(style.slice(STYLE_SLOT_PREFIX.length), 10);
5081
+ for (const css of ctx.getStyleSlotContents(slotId)) {
5082
+ ctx.push(`<style>${escapeStyleContent(css)}</style>`);
5083
+ }
5084
+ } else {
5085
+ ctx.push(`<style>${escapeStyleContent(style)}</style>`);
5086
+ }
5037
5087
  }
5038
5088
  }
5039
5089
  return ctx.getOutput();
@@ -5145,6 +5195,13 @@ function renderElement(ctx, element) {
5145
5195
  renderIfTags(ctx, element.data);
5146
5196
  break;
5147
5197
  case "style":
5198
+ if (ctx.renderInlineStyles && ctx.settings.allowStyleElements) {
5199
+ if (ctx.hasActiveStyleSlot()) {
5200
+ ctx.pushToStyleSlot(element.data);
5201
+ } else {
5202
+ ctx.push(`<style>${escapeStyleContent(element.data)}</style>`);
5203
+ }
5204
+ }
5148
5205
  break;
5149
5206
  case "line-break":
5150
5207
  ctx.push("<br />");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wdprlib/render",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "HTML renderer for Wikidot markup",
5
5
  "keywords": [
6
6
  "html",
@@ -39,7 +39,7 @@
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@wdprlib/ast": "1.1.0",
42
+ "@wdprlib/ast": "1.2.0",
43
43
  "domhandler": "^5.0.3",
44
44
  "htmlparser2": "^10.0.0",
45
45
  "sanitize-html": "^2.14.0",