github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/stories/assets/scripts/stories.js (about) 1 /* JS for stories */ 2 3 DOM.Ready(function() { 4 // Watch story form to fetch title of story page 5 SetSubmitStoryName(); 6 }); 7 8 9 // SetSubmitStoryName sets the story name from a URL 10 // attempt to extract a last param from URL, and set name to a munged version of that 11 function SetSubmitStoryName() { 12 13 DOM.On('.active_url_field', 'change', function(e) { 14 var name = DOM.First('.name_field') 15 var summary = DOM.First('.summary_field') 16 var original_name = name.value 17 var original_url = this.value 18 19 // First locally fill in the name field 20 if (original_name== "") { 21 22 // For github urls, try to fetch some more info 23 if (original_url.startsWith('https://github.com/')) { 24 25 var url = original_url.replace('https://github.com/','https://api.github.com/repos/') 26 DOM.Get(url,function(request){ 27 var data = JSON.parse(request.response); 28 29 // if we got a reponse, try using it. 30 name.value = data.name + " - " + data.description 31 summary.value = data.description + " by " + data.owner.login 32 33 // later use 34 // created_at -> original_published_at 35 // updated_at -> original_updated_at 36 // data.owner.name -> original_author 37 38 },function(){ 39 console.log("failed to fetch github data") 40 }); 41 42 return false; 43 } 44 45 // We could also attempt to fetch the html page, and grab metadata from it 46 // author, pubdate, metadesc etc 47 // would this be better done in a background way after story submission? 48 49 50 // Else just use name from local url if we can 51 name.value = urlToSentenceCase(original_url); 52 53 54 } 55 56 57 58 }); 59 60 } 61 62 // Change a URL to a sentence for SetSubmitStoryName 63 function urlToSentenceCase(url) { 64 if (url === undefined) { 65 return "" 66 } 67 68 var parts, name 69 url = url.replace(/\/$/, ""); // remove trailing / 70 parts = url.split("/"); // now split on / 71 name = parts[parts.length - 1]; // last part of string after last / 72 name = name.replace(/[\?#].*$/, ""); //remove anything after ? or # 73 name = name.replace(/^\d*-/, ""); // remove prefix numerals with dash (common on id based keys) 74 name = name.replace(/\..*$/, ""); // remove .html etc extensions 75 name = name.replace(/[_\-+]/g, " "); // remove all - or + or _ in string, replacing with space 76 name = name.trim(); // remove whitespace trailing or leading 77 name = name.toLowerCase(); // all lower 78 name = name[0].toUpperCase() + name.substring(1); // Sentence case 79 80 81 // Deal with some specific URLs 82 if (url.match(/youtube|vimeo\.com/)) { 83 name = "Video: " 84 } 85 if (url.match(/medium\.com/)) { 86 // Eat the last word (UDID) on medium posts 87 name = name.replace(/ [^ ]*$/, ""); 88 } 89 90 91 92 return name 93 }