Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Include option to store embed meta as json #237

Closed
wants to merge 3 commits into from

Conversation

aggied
Copy link

@aggied aggied commented Nov 8, 2015

I want to resolve #235 by accepting a storeMeta option in the embed config that when set to true stores embed json in a <script> tag in the resulting html.

example embed config:

     embeds:{
          oembedProxy:'https://www.iframely.com/iframely',
          storeMeta:true
     }

@@ -298,6 +298,10 @@
success: function(data) {
var html = data && data.html;

if (that.options.storeMeta){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix indentation here ?

@j0k3r
Copy link
Collaborator

j0k3r commented Nov 8, 2015

Can you add a default value to false in defaults variable at the top of the file?

@aggied
Copy link
Author

aggied commented Nov 8, 2015

hopefully that works for you. cleaned up the formatting and added a default option of storeMeta:false

@j0k3r
Copy link
Collaborator

j0k3r commented Nov 9, 2015

Thanks that look great! Could you try to add some tests that check the json script tag is here when the option is enabled?
Also, as a side note, I was wondering how do you retrieve such information for your backend?

@aggied
Copy link
Author

aggied commented Nov 10, 2015

Also, as a side note, I was wondering how do you retrieve such information for your backend?

I have this as part of an angular app, so here's the first cut of code I'm using right now when the user submits:

var serialized =$scope.bindOptions.editor.serialize()["element-0"].value,
      meta={};
var postHtml=$('<div/>').html(serialized);
postHtml.contents().each(function(i,elem){
 if ($(this).prop('tagName')=='DIV' && $(this).hasClass('medium-insert-embeds')){
      var metaDiv=$(this).find('div.medium-insert-embeds-meta');
      var curMeta = JSON.parse($(metaDiv).find('script').first().html());
      $(metaDiv).remove();
      meta['block'+i]=curMeta;
 }
});

Could you try to add some tests that check the json script tag is here when the option is enabled?

I haven't used Jasmine really at all before, and this is failing below. I will spend a few moments figuring this out later but if you care to shed some light it would save me some time--

    it('stores embed metadata as json in the html after the embed display', function () {
        var $event = $.Event('keydown');

        $event.which = 13;
        $('#fixtures').html('<div class="editable"><p class="medium-insert-embeds-input medium-insert-embeds-active">https://www.youtube.com/watch?v=BROWqjuTM0g</p></div>');
        this.$el = $('.editable');
        this.$el.mediumInsert({
            addons: {
                embeds: {
                    oembedProxy: false,
                    storeMeta:true
                }
            }
        });
        this.$el.trigger($event);

        expect(this.$el.find('.medium-insert-embeds').length).toEqual(1);
        expect(this.$el.find('.medium-insert-embeds iframe').length).toEqual(1);
        expect(this.$el.find('.medium-insert-embeds-input').length).toEqual(0);
        expect(this.$el.find('.medium-insert-embeds-meta script').length).toEqual(1);
    });

@Quimera
Copy link

Quimera commented Nov 24, 2015

just for the record, an unescaped </script> tag is going to be parsed in a script element as the closing script tag http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag/1450633#1450633.
Maybe a replace with a regex after the stringify method?

JSON.stringify(obj).replace(new RegExp('/', 'g'), '\\/')

@aggied
Copy link
Author

aggied commented Nov 25, 2015

Don't mind adding the regex at all, but I'm not entirely sure it's necessary. My understanding/experience with stringify has always been that the native function handles all the encodings when the workflow is JSON.stringify() -> JSON.parse().

http://stackoverflow.com/questions/16507858/json-stringify-escapes-double-quotes-every-time-when-stringified

http://stackoverflow.com/questions/22341571/how-do-i-retain-backslashes-in-strings-when-using-json-stringify

I've ran a several tests using stringify, and I haven't got in trouble yet...

thoughts?

@linkesch
Copy link
Owner

linkesch commented Apr 5, 2016

@aggied I've forgotten about this PR a little. How are the tests going? We gonna need them when we want to introduce a new feature.

@linpekka
Copy link
Contributor

Anything I can do to get this accepted?

@linkesch
Copy link
Owner

@linpekka you can pick it up where @aggied left it. We need to rebase it agains master and write some tests.

@linpekka
Copy link
Contributor

I have looked a bit at it today, and it seems like most (perhaps all) of your embed tests use oembedProxy false, and thus mainly tests the 'mock'. Is testing via Embeds.prototype.parseUrl enough?

@linpekka
Copy link
Contributor

Created a new pull request with a merge and some simple test, let me know if you need anything else,

Store meta #333

@linkesch
Copy link
Owner

linkesch commented Aug 8, 2016

Merged with #333

@linkesch linkesch closed this Aug 8, 2016
@aggied
Copy link
Author

aggied commented Oct 22, 2016

Picking up projects that use this repo again. @linpekka thanks for finishing this off

@linpekka
Copy link
Contributor

Np, you did all the real stuff, im just the cleaner.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access to oembedProxy metadata
5 participants