@tryghost/kg-mobiledoc-html-renderer
Advanced tools
Comparing version 3.0.1 to 3.0.2-4.0.0-rc.0.0
const SimpleDom = require('simple-dom'); | ||
const semver = require('semver'); | ||
const Renderer = require('mobiledoc-dom-renderer').default; | ||
@@ -26,2 +27,25 @@ | ||
const slugify = function (inputString, {ghostVersion = '4.0'} = {}) { | ||
const version = semver.coerce(ghostVersion); | ||
if (semver.satisfies(version, '<4.x')) { | ||
// backwards compatible slugs used in Ghost 2.x to 3.x mobiledoc | ||
return inputString.replace(/[<>&"?]/g, '') | ||
.trim() | ||
.replace(/[^\w]/g, '-') | ||
.replace(/-{2,}/g, '-') | ||
.toLowerCase(); | ||
} else { | ||
// news slugs introduced in 4.0 | ||
// allows all chars except symbols but will urlEncode everything | ||
// produces %-encoded chars in src but browsers show real chars in status bar and url bar | ||
return encodeURIComponent(inputString.trim() | ||
.toLowerCase() | ||
.replace(/[\][!"#$%&'()*+,./:;<=>?@\\^_{|}~]/g, '') | ||
.replace(/\s+/g, '-') | ||
.replace(/^-|-{2,}|-$/g, '') | ||
); | ||
} | ||
}; | ||
// used to walk the rendered SimpleDOM output and modify elements before | ||
@@ -31,4 +55,5 @@ // serializing to HTML. Saves having a large HTML parsing dependency such as | ||
class DomModifier { | ||
constructor() { | ||
constructor(options) { | ||
this.usedIds = []; | ||
this.options = options; | ||
} | ||
@@ -42,8 +67,3 @@ | ||
let text = nodeTextContent(node); | ||
let id = text | ||
.replace(/[<>&"?]/g, '') | ||
.trim() | ||
.replace(/[^\w]/g, '-') | ||
.replace(/-{2,}/g, '-') | ||
.toLowerCase(); | ||
let id = slugify(text, this.options); | ||
@@ -83,8 +103,5 @@ if (this.usedIds[id] !== undefined) { | ||
render(mobiledoc, _cardOptions = {}) { | ||
/** | ||
* version 1 === Ghost 1.0 markdown-only mobiledoc | ||
* version 2 (latest) === Ghost 2.0 full mobiledoc | ||
*/ | ||
const ghostVersion = mobiledoc.ghostVersion || '4.0'; | ||
const defaultCardOptions = { | ||
version: 2, | ||
ghostVersion, | ||
target: 'html' | ||
@@ -109,3 +126,3 @@ }; | ||
// eg. to add ID attributes to heading elements | ||
const modifier = new DomModifier(); | ||
const modifier = new DomModifier({ghostVersion}); | ||
modifier.modifyChildren(rendered.result); | ||
@@ -112,0 +129,0 @@ |
{ | ||
"name": "@tryghost/kg-mobiledoc-html-renderer", | ||
"version": "3.0.1", | ||
"version": "3.0.2-4.0.0-rc.0.0", | ||
"repository": "https://github.com/TryGhost/Koenig/tree/master/packages/kg-mobiledoc-html-renderer", | ||
@@ -26,5 +26,6 @@ "author": "Ghost Foundation", | ||
"mobiledoc-dom-renderer": "^0.7.0", | ||
"semver": "^7.3.4", | ||
"simple-dom": "^1.4.0" | ||
}, | ||
"gitHead": "93c853c6be3de6e0dbece24367aeba2089df217e" | ||
"gitHead": "f9a83f9ba1f0fe3fcf4cdffa996a7c8da548226b" | ||
} |
@@ -37,4 +37,4 @@ # Koenig Mobiledoc Html Renderer | ||
# Copyright & License | ||
# Copyright & License | ||
Copyright (c) 2020 Ghost Foundation - Released under the [MIT license](LICENSE). | ||
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6662
113
40
3
1
2
+ Addedsemver@^7.3.4
+ Addedsemver@7.6.3(transitive)