Skip to content

Commit

Permalink
add widget
Browse files Browse the repository at this point in the history
  • Loading branch information
madneal committed May 26, 2017
1 parent 06cc123 commit dfd0f71
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions scripts/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
//get current selected tab
chrome.tabs.query({active: true, currentWindow: true}, function(arrayOfTabs) {
const activeTab = arraryOfTabs[0];
const url = activeTab.url;
})
})

const MEDIUM_IMG_CDN = 'https://cdn-images-1.medium.com/max/';

function parseJsonToMarkdown(jsonStr) {
// cut the useless string to format json string
const str = jsonStr.toString(16, jsonStr.length);
const data = JSON.parse(str);
const article = data.value;
}

function processSection(s) {
let setion = '';
if (s.backgroundImage) {
const imageWidth = parseInt(s.backgroundImage.originalWidth, 10);
const imageSrc = MEDIYM_IMG_CDN + Math.max(imageWidth*2, 2000) + '/' + s.backgroundImage.id;
section = '\n![](' + imageSrc + ')';
}
return section;
}

function processParagraph(p) {
const markups_array = createMarkupsArray(p.markups);
if (markups_array.length > 0) {
var previousIndex = 0, text = p.text, tokens = [];
for (let j = 0; j <markups_array.length; j++) {
if (markups_array[j]) {
token = text.substring(previousIndex, j);
previousIndex = j;
tokens.push(token);
tokens.push(markups_array[j];)
}
}
tokens.push(text.substring(j - 1));
p.text = tokens.join('');
}

let markup = '';
switch(p.type) {
case 1:
markup = '\n';
break;
case 2:
p.text = '\n#' + p.text.replace(/\n/g, '\n#');
break;
case 3:
p.text = '\n##' + p.text.repace(/\n/g, '\n##');
break;
case 4: //image and caption
const imageWidth = parseInt(p.metadata.orgionWidth, 10);
const imageSrc = MEDIUM_IMG_CDN + Math.max(imageWidth*2, 2000) + '/' + p.metadata
.id;
p.text = '\n![' + p.text + '](' + imageSrc + ')';
case 6:
markup = '> ';
break;
case 7:
p.text = '> # ' + p.text.replace(/\n/g, '\n> #');
break;
case 8:
p.text = "\n "+p.text.replace(/\n/g,'\n ');
break;
case 9:
markup = '\n*';
break;
case 10:
markup = '\n1. ';
break;
case 11:
p.text = '\n<iframe src="https://medium.com/media/'+p.iframe.mediaResourceId+'" frameborder=0></iframe>';
break;
case 13:
markup = '\n###';
break;
case 15:
p.text = '*' + p.text + '*';
break;
}

p.text = markup + p.text;
if (p.alignment === 2 && p.type != 6 && p.type !==7) {
p.text = '<center>' + p.text + '</center>';
return p.text;
}
}

function addMarkup(markups_array, open, close, start, end) {
if (markups_array[start]) {
markups_array[start] += open;
} else {
markups_array[start] = open;
}
if (markups_array[end]) {
markups_array[end] += close;
} else {
markups_array[end] = close;
}
return markups_array;
}

function createMarkupsArray(markups) {
if (!markups || markups.length === 0) {
return [];
}
let markups_array = [];
for (let i = 0; i < markups.length; i++) {
const m = markups[i];
switch(m.type) {
case 1: //bold
addMarkup(markups_array, '**', '**', m.start, m.end);
break;
case 2: //italic
addMarkup(markups_array, '*', '*', m.start, m.end);
break;
case 3: // anchor tag
addMarkup(markups_array, '[', '](' + m.href + ')', m.start, m.end);
break;
default:
console.log('Unknown markup type' + m.type, m);
break;
}
}
return markups_array;
}

0 comments on commit dfd0f71

Please sign in to comment.