Renamed mobiledoc converters to renderers

no issue

- matches naming with `mobiledoc-kit` nomenclature
- better matches intent for future additions of mobiledoc rendering to email/plaintext/etc
This commit is contained in:
Kevin Ansfield 2020-03-18 14:39:49 +00:00
parent dec24ad883
commit 4949ad9214
18 changed files with 90 additions and 89 deletions

View File

@ -1,7 +1,7 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:pages');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const converters = require('../../../../../lib/mobiledoc/converters');
const renderers = require('../../../../../lib/mobiledoc/renderers');
const url = require('./utils/url');
const localUtils = require('../../index');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
@ -143,7 +143,7 @@ module.exports = {
const html = frame.data.pages[0].html;
if (frame.options.source === 'html' && !_.isEmpty(html)) {
frame.data.pages[0].mobiledoc = JSON.stringify(converters.htmlToMobiledocConverter(html));
frame.data.pages[0].mobiledoc = JSON.stringify(renderers.htmlToMobiledocConverter(html));
}
}

View File

@ -3,7 +3,7 @@ const debug = require('ghost-ignition').debug('api:canary:utils:serializers:inpu
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const url = require('./utils/url');
const localUtils = require('../../index');
const converters = require('../../../../../lib/mobiledoc/converters');
const renderers = require('../../../../../lib/mobiledoc/renderers');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
const replacePageWithType = mapNQLKeyValues({
@ -159,7 +159,7 @@ module.exports = {
const html = frame.data.posts[0].html;
if (frame.options.source === 'html' && !_.isEmpty(html)) {
frame.data.posts[0].mobiledoc = JSON.stringify(converters.htmlToMobiledocConverter(html));
frame.data.posts[0].mobiledoc = JSON.stringify(renderers.htmlToMobiledocConverter(html));
}
}

View File

@ -1,7 +1,7 @@
const _ = require('lodash');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:pages');
const converters = require('../../../../../lib/mobiledoc/converters');
const renderers = require('../../../../../lib/mobiledoc/renderers');
const url = require('./utils/url');
const localUtils = require('../../index');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
@ -143,7 +143,7 @@ module.exports = {
const html = frame.data.pages[0].html;
if (frame.options.source === 'html' && !_.isEmpty(html)) {
frame.data.pages[0].mobiledoc = JSON.stringify(converters.htmlToMobiledocConverter(html));
frame.data.pages[0].mobiledoc = JSON.stringify(renderers.htmlToMobiledocConverter(html));
}
}

View File

@ -3,7 +3,7 @@ const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts');
const url = require('./utils/url');
const localUtils = require('../../index');
const converters = require('../../../../../lib/mobiledoc/converters');
const renderers = require('../../../../../lib/mobiledoc/renderers');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
const replacePageWithType = mapNQLKeyValues({
@ -159,7 +159,7 @@ module.exports = {
const html = frame.data.posts[0].html;
if (frame.options.source === 'html' && !_.isEmpty(html)) {
frame.data.posts[0].mobiledoc = JSON.stringify(converters.htmlToMobiledocConverter(html));
frame.data.posts[0].mobiledoc = JSON.stringify(renderers.htmlToMobiledocConverter(html));
}
}

View File

@ -2,7 +2,7 @@ const debug = require('ghost-ignition').debug('importer:posts');
const _ = require('lodash');
const uuid = require('uuid');
const BaseImporter = require('./base');
const converters = require('../../../../lib/mobiledoc/converters');
const renderers = require('../../../../lib/mobiledoc/renderers');
const validation = require('../../../validation');
const postsMetaSchema = require('../../../schema').tables.posts_meta;
const metaAttrs = _.keys(_.omit(postsMetaSchema, ['id']));
@ -200,11 +200,11 @@ class PostsImporter extends BaseImporter {
mobiledoc = JSON.parse(model.mobiledoc);
if (!mobiledoc.cards || !_.isArray(mobiledoc.cards)) {
model.mobiledoc = converters.mobiledocConverter.blankStructure();
model.mobiledoc = renderers.mobiledocHtmlRenderer.blankStructure();
mobiledoc = model.mobiledoc;
}
} catch (err) {
mobiledoc = converters.mobiledocConverter.blankStructure();
mobiledoc = renderers.mobiledocHtmlRenderer.blankStructure();
}
mobiledoc.cards.forEach((card) => {
@ -216,7 +216,7 @@ class PostsImporter extends BaseImporter {
});
model.mobiledoc = JSON.stringify(mobiledoc);
model.html = converters.mobiledocConverter.render(JSON.parse(model.mobiledoc));
model.html = renderers.mobiledocHtmlRenderer.render(JSON.parse(model.mobiledoc));
}
this.sanitizePostsMeta(model);
});

View File

@ -1,7 +1,7 @@
const _ = require('lodash');
const Promise = require('bluebird');
const common = require('../../../../lib/common');
const converters = require('../../../../lib/mobiledoc/converters');
const renderers = require('../../../../lib/mobiledoc/renderers');
const models = require('../../../../models');
const message1 = 'Migrating Koenig beta post\'s mobiledoc/HTML to 2.0 format';
const message2 = 'Migrated Koenig beta post\'s mobiledoc/HTML to 2.0 format';
@ -54,7 +54,7 @@ module.exports.up = function regenerateKoenigBetaHTML(options) {
// re-render the html to remove .kg-post wrapper and adjust image classes
let version = 2;
let html = converters.mobiledocConverter.render(mobiledoc, version);
let html = renderers.mobiledocHtmlRenderer.render(mobiledoc, version);
return models.Post.edit({
html,

View File

@ -1,7 +1,7 @@
const _ = require('lodash'),
Promise = require('bluebird'),
common = require('../../../../lib/common'),
converters = require('../../../../lib/mobiledoc/converters'),
renderers = require('../../../../lib/mobiledoc/renderers'),
message1 = 'Updating posts: apply new editor format and set comment_id field.',
message2 = 'Updated posts: apply new editor format and set comment_id field.',
message3 = 'Rollback: Updating posts: use old editor format',
@ -50,18 +50,18 @@ module.exports.up = (options) => {
mobiledoc = JSON.parse(post.mobiledoc || null);
if (!mobiledoc) {
mobiledoc = converters.mobiledocConverter.blankStructure();
mobiledoc = renderers.mobiledocHtmlRenderer.blankStructure();
}
} catch (err) {
common.logging.warn(`Invalid mobiledoc structure for ${post.id}. Falling back to blank structure.`);
mobiledoc = converters.mobiledocConverter.blankStructure();
mobiledoc = renderers.mobiledocHtmlRenderer.blankStructure();
}
// CASE: convert all old editor posts to the new editor format
// CASE: if mobiledoc field is null, we auto set a blank structure in the model layer
// CASE: if html field is null, we auto generate the html in the model layer
if (mobiledoc && post.html && post.html.match(/^<div class="kg-card-markdown">/)) {
html = converters.mobiledocConverter.render(mobiledoc);
html = renderers.mobiledocHtmlRenderer.render(mobiledoc);
}
return localOptions
.transacting('posts')
@ -101,7 +101,7 @@ module.exports.down = (options) => {
// CASE: revert: all new editor posts to the old editor format
if (mobiledoc && post.html) {
html = converters.mobiledocConverter.render(mobiledoc, version);
html = renderers.mobiledocHtmlRenderer.render(mobiledoc, version);
}
return localOptions

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
const Promise = require('bluebird');
const htmlToText = require('html-to-text');
const common = require('../../../../lib/common');
const converters = require('../../../../lib/mobiledoc/converters');
const renderers = require('../../../../lib/mobiledoc/renderers');
module.exports.config = {
transaction: true
@ -36,7 +36,7 @@ module.exports.up = (options) => {
return Promise.resolve();
}
const html = converters.mobiledocConverter.render(mobiledoc);
const html = renderers.mobiledocHtmlRenderer.render(mobiledoc);
const updatedAttrs = {
html: html

View File

@ -9,11 +9,11 @@ module.exports = function markdownCardDefinition() {
},
render: function (opts) {
let converters = require('../converters');
let renderers = require('../renderers');
let payload = opts.payload;
let version = opts.options && opts.options.version || 2;
// convert markdown to HTML ready for insertion into dom
let html = converters.markdownConverter.render(payload.markdown || '');
let html = renderers.markdownHtmlRenderer.render(payload.markdown || '');
if (!html) {
return '';

View File

@ -1,53 +0,0 @@
var MarkdownIt = require('markdown-it'),
converter = new MarkdownIt({
html: true,
breaks: true,
linkify: true
})
.use(require('markdown-it-footnote'))
.use(require('markdown-it-lazy-headers'))
.use(require('markdown-it-mark'))
.use(function namedHeaders(md) {
// match legacy Showdown IDs
var slugify = function (inputString, usedHeaders) {
var slug = inputString.replace(/[^\w]/g, '').toLowerCase();
if (usedHeaders[slug]) {
usedHeaders[slug] += 1;
slug += usedHeaders[slug];
}
return slug;
};
var originalHeadingOpen = md.renderer.rules.heading_open;
// originally from https://github.com/leff/markdown-it-named-headers
// moved here to avoid pulling in http://stringjs.com dependency
md.renderer.rules.heading_open = function (tokens, idx, something, somethingelse, self) {
var usedHeaders = {};
tokens[idx].attrs = tokens[idx].attrs || [];
var title = tokens[idx + 1].children.reduce(function (acc, t) {
return acc + t.content;
}, '');
var slug = slugify(title, usedHeaders);
tokens[idx].attrs.push(['id', slug]);
if (originalHeadingOpen) {
return originalHeadingOpen.apply(this, arguments);
} else {
return self.renderToken.apply(self, arguments);
}
};
});
// configure linkify-it
converter.linkify.set({
fuzzyLink: false
});
module.exports = {
render: function (markdown) {
return converter.render(markdown);
}
};

View File

@ -11,7 +11,7 @@ module.exports = {
return require('./atoms');
},
get converters() {
return require('./converters');
get renderers() {
return require('./renderers');
}
};

View File

@ -1,12 +1,12 @@
const common = require('../../common');
module.exports = {
get mobiledocConverter() {
return require('./mobiledoc-converter');
get mobiledocHtmlRenderer() {
return require('./mobiledoc-html-renderer');
},
get markdownConverter() {
return require('./markdown-converter');
get markdownHtmlRenderer() {
return require('./markdown-html-renderer');
},
get htmlToMobiledocConverter() {

View File

@ -0,0 +1,54 @@
const MarkdownIt = require('markdown-it');
const markdownIt = new MarkdownIt({
html: true,
breaks: true,
linkify: true
})
.use(require('markdown-it-footnote'))
.use(require('markdown-it-lazy-headers'))
.use(require('markdown-it-mark'))
.use(function namedHeaders(md) {
// match legacy Showdown IDs
const slugify = function (inputString, usedHeaders) {
let slug = inputString.replace(/[^\w]/g, '').toLowerCase();
if (usedHeaders[slug]) {
usedHeaders[slug] += 1;
slug += usedHeaders[slug];
}
return slug;
};
const originalHeadingOpen = md.renderer.rules.heading_open;
// originally from https://github.com/leff/markdown-it-named-headers
// moved here to avoid pulling in http://stringjs.com dependency
md.renderer.rules.heading_open = function (tokens, idx, something, somethingelse, self) {
const usedHeaders = {};
tokens[idx].attrs = tokens[idx].attrs || [];
const title = tokens[idx + 1].children.reduce(function (acc, t) {
return acc + t.content;
}, '');
const slug = slugify(title, usedHeaders);
tokens[idx].attrs.push(['id', slug]);
if (originalHeadingOpen) {
return originalHeadingOpen.apply(this, arguments);
} else {
return self.renderToken.apply(self, arguments);
}
};
});
// configure linkify-it
markdownIt.linkify.set({
fuzzyLink: false
});
module.exports = {
render: function (markdown) {
return markdownIt.render(markdown);
}
};

View File

@ -9,7 +9,7 @@ const htmlToText = require('html-to-text');
const ghostBookshelf = require('./base');
const config = require('../config');
const settingsCache = require('../services/settings/cache');
const converters = require('../lib/mobiledoc/converters');
const renderers = require('../lib/mobiledoc/renderers');
const relations = require('./relations');
const urlUtils = require('../lib/url-utils');
const MOBILEDOC_REVISIONS_COUNT = 10;
@ -365,7 +365,7 @@ Post = ghostBookshelf.Model.extend({
}
if (!this.get('mobiledoc')) {
this.set('mobiledoc', JSON.stringify(converters.mobiledocConverter.blankStructure()));
this.set('mobiledoc', JSON.stringify(renderers.mobiledocHtmlRenderer.blankStructure()));
}
// ensure all URLs are stored as relative
@ -405,7 +405,7 @@ Post = ghostBookshelf.Model.extend({
// CASE: html is null, but mobiledoc exists (only important for migrations & importing)
if (this.hasChanged('mobiledoc') || (!this.get('html') && (options.migrating || options.importing))) {
try {
this.set('html', converters.mobiledocConverter.render(JSON.parse(this.get('mobiledoc'))));
this.set('html', renderers.mobiledocHtmlRenderer.render(JSON.parse(this.get('mobiledoc'))));
} catch (err) {
throw new common.errors.ValidationError({
message: 'Invalid mobiledoc structure.',

View File

@ -9,7 +9,7 @@ const localUtils = require('./utils');
const ghost = testUtils.startGhost;
let request;
describe('Posts API', function () {
describe('Posts API (v2)', function () {
let ghostServer;
let ownerCookie;

View File

@ -9,7 +9,7 @@ const localUtils = require('./utils');
const ghost = testUtils.startGhost;
let request;
describe('Posts API', function () {
describe('Posts API (v3)', function () {
let ghostServer;
let ownerCookie;

View File

@ -1,7 +1,7 @@
const should = require('should');
const converter = require('../../../../../server/lib/mobiledoc/converters/mobiledoc-converter');
const converter = require('../../../../../server/lib/mobiledoc/renderers/mobiledoc-html-renderer');
describe('Mobiledoc converter', function () {
describe('Mobiledoc HTML renderer', function () {
describe('default', function () {
it('renders all default cards and atoms', function () {
let mobiledoc = {