@tryghost/kg-mobiledoc-html-renderer 5.2.0 → 5.3.3

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2013-2021 Ghost Foundation
3
+ Copyright (c) 2013-2022 Ghost Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -36,4 +36,4 @@ Follow the instructions for the top-level repo.
36
36
 
37
37
  # Copyright & License
38
38
 
39
- Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
39
+ Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
@@ -1,6 +1,6 @@
1
1
  const SimpleDom = require('simple-dom');
2
- const semver = require('semver');
3
2
  const Renderer = require('mobiledoc-dom-renderer').default;
3
+ const {slugify} = require('@tryghost/kg-utils');
4
4
 
5
5
  const walkDom = function (node, func) {
6
6
  func(node);
@@ -24,29 +24,6 @@ const nodeTextContent = function (node) {
24
24
  return textContent;
25
25
  };
26
26
 
27
- const slugify = function (inputString, {ghostVersion = '4.0'} = {}) {
28
- const version = semver.coerce(ghostVersion);
29
-
30
- if (semver.satisfies(version, '<4.x')) {
31
- // backwards compatible slugs used in Ghost 2.x to 3.x mobiledoc
32
- return inputString.replace(/[<>&"?]/g, '')
33
- .trim()
34
- .replace(/[^\w]/g, '-')
35
- .replace(/-{2,}/g, '-')
36
- .toLowerCase();
37
- } else {
38
- // news slugs introduced in 4.0
39
- // allows all chars except symbols but will urlEncode everything
40
- // produces %-encoded chars in src but browsers show real chars in status bar and url bar
41
- return encodeURIComponent(inputString.trim()
42
- .toLowerCase()
43
- .replace(/[\][!"#$%&'()*+,./:;<=>?@\\^_{|}~]/g, '')
44
- .replace(/\s+/g, '-')
45
- .replace(/^-|-{2,}|-$/g, '')
46
- );
47
- }
48
- };
49
-
50
27
  // used to walk the rendered SimpleDOM output and modify elements before
51
28
  // serializing to HTML. Saves having a large HTML parsing dependency such as
52
29
  // jsdom that may break on malformed HTML in MD or HTML cards
@@ -74,6 +51,19 @@ class DomModifier {
74
51
  node.setAttribute('id', id);
75
52
  }
76
53
 
54
+ wrapBlockquoteContentInP(node) {
55
+ if (node.firstChild && node.firstChild.tagName === 'P') {
56
+ return;
57
+ }
58
+
59
+ const p = this.options.dom.createElement('p');
60
+ while (node.firstChild) {
61
+ p.appendChild(node.firstChild);
62
+ }
63
+
64
+ node.appendChild(p);
65
+ }
66
+
77
67
  modifyChildren(node) {
78
68
  walkDom(node, this.modify.bind(this));
79
69
  }
@@ -83,6 +73,11 @@ class DomModifier {
83
73
  if (node.nodeType === 1 && node.nodeName.match(/^h\d$/i)) {
84
74
  this.addHeadingId(node);
85
75
  }
76
+
77
+ // wrap blockquote content in P tag for emails
78
+ if (this.options.target === 'email' && node.nodeType === 1 && node.nodeName === 'BLOCKQUOTE') {
79
+ this.wrapBlockquoteContentInP(node);
80
+ }
86
81
  }
87
82
  }
88
83
 
@@ -98,12 +93,25 @@ class MobiledocHtmlRenderer {
98
93
 
99
94
  render(mobiledoc, _cardOptions = {}) {
100
95
  const ghostVersion = mobiledoc.ghostVersion || '4.0';
96
+
101
97
  const defaultCardOptions = {
102
98
  ghostVersion,
103
99
  target: 'html'
104
100
  };
105
101
  const cardOptions = Object.assign({}, defaultCardOptions, _cardOptions);
106
- const rendererOptions = Object.assign({}, this.options, {cardOptions});
102
+
103
+ const sectionElementRenderer = {
104
+ ASIDE: function (tagName, dom) {
105
+ // we use ASIDE sections in Koenig as a workaround for applying
106
+ // a different blockquote style because mobiledoc doesn't support
107
+ // storing arbitrary attributes with sections
108
+ const blockquote = dom.createElement('blockquote');
109
+ blockquote.setAttribute('class', 'kg-blockquote-alt');
110
+ return blockquote;
111
+ }
112
+ };
113
+
114
+ const rendererOptions = Object.assign({}, this.options, {cardOptions, sectionElementRenderer});
107
115
  const renderer = new Renderer(rendererOptions);
108
116
  const rendered = renderer.render(mobiledoc);
109
117
  const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
@@ -119,7 +127,7 @@ class MobiledocHtmlRenderer {
119
127
 
120
128
  // Walk the DOM output and modify nodes as needed
121
129
  // eg. to add ID attributes to heading elements
122
- const modifier = new DomModifier({ghostVersion});
130
+ const modifier = new DomModifier(Object.assign({}, cardOptions, {dom: this.options.dom}));
123
131
  modifier.modifyChildren(rendered.result);
124
132
 
125
133
  const output = serializer.serializeChildren(rendered.result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/kg-mobiledoc-html-renderer",
3
- "version": "5.2.0",
3
+ "version": "5.3.3",
4
4
  "repository": "https://github.com/TryGhost/Koenig/tree/master/packages/kg-mobiledoc-html-renderer",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -22,12 +22,12 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
+ "@tryghost/kg-utils": "^1.0.0",
25
26
  "mobiledoc-dom-renderer": "^0.7.0",
26
- "semver": "^7.3.4",
27
27
  "simple-dom": "^1.4.0"
28
28
  },
29
29
  "devDependencies": {
30
- "c8": "7.10.0"
30
+ "c8": "7.11.0"
31
31
  },
32
- "gitHead": "5fbc876127879babe588a577bc810038c98df5ef"
32
+ "gitHead": "615a20445876522e113a8770ada332e8c00d939b"
33
33
  }