Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

馃悰 fix missing meta description preview #719

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/components/gh-post-settings-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Ember from 'ember';
import SettingsMenuMixin from 'ghost-admin/mixins/settings-menu-component';
import boundOneWay from 'ghost-admin/utils/bound-one-way';
import computed, {alias} from 'ember-computed';
import formatMarkdown from 'ghost-admin/utils/format-markdown';
import injectService from 'ember-service/inject';
import isNumber from 'ghost-admin/utils/isNumber';
import moment from 'moment';
Expand Down Expand Up @@ -70,17 +71,20 @@ export default Component.extend(SettingsMenuMixin, {
return metaTitle;
}),

seoDescription: computed('model.html', 'metaDescriptionScratch', function () {
seoDescription: computed('model.scratch', 'metaDescriptionScratch', function () {
let metaDescription = this.get('metaDescriptionScratch') || '';
let mobiledoc = this.get('model.scratch');
let markdown = mobiledoc.cards && mobiledoc.cards[0][1].markdown;
let placeholder;

if (metaDescription.length > 0) {
placeholder = metaDescription;
} else {
let html = this.get('model.html');
let div = document.createElement('div');
div.innerHTML = formatMarkdown(markdown, false);

// Strip HTML
placeholder = this.$('<div />', {html}).text();
placeholder = div.textContent;
// Replace new lines and trim
placeholder = placeholder.replace(/\n+/g, ' ').trim();
}
Expand Down
12 changes: 7 additions & 5 deletions app/utils/format-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ let md = markdownit({
}
});

export default function formatMarkdown(_markdown) {
export default function formatMarkdown(_markdown, replaceJS = true) {
let markdown = _markdown || '';
let escapedhtml = '';

// convert markdown to HTML
escapedhtml = md.render(markdown);

// replace script and iFrame
escapedhtml = escapedhtml.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
'<pre class="js-embed-placeholder">Embedded JavaScript</pre>');
escapedhtml = escapedhtml.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi,
'<pre class="iframe-embed-placeholder">Embedded iFrame</pre>');
if (replaceJS) {
escapedhtml = escapedhtml.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
'<pre class="js-embed-placeholder">Embedded JavaScript</pre>');
escapedhtml = escapedhtml.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi,
'<pre class="iframe-embed-placeholder">Embedded iFrame</pre>');
}

// sanitize html
escapedhtml = html_sanitize(escapedhtml, cajaSanitizers.url, cajaSanitizers.id);
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/components/gh-post-settings-menu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,23 @@ describe.skip('Unit: Component: post-settings-menu', function () {
expect(component.get('seoDescription')).to.equal('a description');
});

it.skip('should be generated from the rendered markdown if not explicitly set', function () {
// can't test right now because the rendered markdown is being pulled
// from the DOM via jquery
it('should be generated from the rendered mobiledoc if not explicitly set', function () {
let component = this.subject({
model: EmberObject.extend({
author: RSVP.resolve(),
metaDescription: null,
metaDescriptionScratch: boundOneWay('metaDescription'),
scratch: {
cards: [
['markdown-card', {
markdown: '# This is a <strong>test</strong> <script>foo</script>'
}]
]
}
}).create()
});

expect(component.get('seoDescription')).to.equal('This is a test');
});

it('should truncate to 156 characters with an appended ellipsis', function () {
Expand Down