Skip to content

Commit

Permalink
modify for indentify for code block
Browse files Browse the repository at this point in the history
  • Loading branch information
madneal committed Sep 19, 2017
1 parent 8e445e6 commit d5766af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Export to Markdown",
"short_name": "ExportToMarkdown",
"description": "This is utilized to export the story in Medium to a markdown format file.",
"version": "0.0.1",
"version": "0.1",
"icons": {
"16": "icons/icon-128.png",
"48": "icons/icon-128.png",
Expand All @@ -15,7 +15,7 @@
],
"browser_action": {
"default_title": "Export to Markdown",
"default_icon": "icons/icon-48.png",
"default_icon": "icons/icon-128.png",
"default_popup": "popup.html"
},
"manifest_version": 2,
Expand Down
61 changes: 37 additions & 24 deletions scripts/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ document.querySelector('.export').addEventListener('click', function () {
exportMedium()
})

document.querySelector('.copy').addEventListener('click', function() {
document.querySelector('.copy').addEventListener('click', function () {
const value = document.querySelector('#source').value
copyToClipboard(value);
})
Expand All @@ -29,8 +29,8 @@ function cancelLoad() {
loadIcon.style.visibility = 'hidden'
}

function exportMedium () {
chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
function exportMedium() {
chrome.tabs.query({ active: true, currentWindow: true }, function (arrayOfTabs) {
const activeTab = arrayOfTabs[0]
const url = activeTab.url + '?format=json'
fetch(url)
Expand All @@ -54,7 +54,7 @@ function exportMedium () {
})
}

function parseJsonToMarkdown (jsonStr) {
function parseJsonToMarkdown(jsonStr) {
// cut the useless string to format json string
const str = jsonStr.substring(16, jsonStr.length)
const data = JSON.parse(str)
Expand Down Expand Up @@ -83,20 +83,28 @@ function parseJsonToMarkdown (jsonStr) {
story.markdown = []

const paragraphs = story.paragraphs
let lastPtype = '';
let sequence = 0;
for (let i = 0; i < paragraphs.length; i++) {
if (sections[i]) {
story.markdown.push(sections[i])
}
const p = paragraphs[i];
const text = processParagraph(p);
if (p.type === 10) {
sequence++;
} else {
sequence = 0
}
const text = processParagraph(p, sequence);
lastPtype = p.type;
if (text !== story.markdown[i]) {
story.markdown.push(text)
}
}
return story
}

function processSection (s) {
function processSection(s) {
let section = ''
if (s.backgroundImage) {
const imageWidth = parseInt(s.backgroundImage.originalWidth, 10)
Expand All @@ -106,7 +114,7 @@ function processSection (s) {
return section
}

function processParagraph (p) {
function processParagraph(p, sequence) {
const markups_array = createMarkupsArray(p.markups)
if (markups_array.length > 0) {
let previousIndex = 0, text = p.text, tokens = []
Expand Down Expand Up @@ -134,47 +142,47 @@ function processParagraph (p) {
case 3:
p.text = '\n## ' + p.text.replace(/\n/g, '\n ##')
break
case 4:
case 4:
const imageWidth = parseInt(p.metadata.originalWidth, 10)
const imageSrc = MEDIUM_IMG_CDN + Math.max(imageWidth * 2, 2000) + '/' + p.metadata
.id
p.text = '\n![' + p.text + '](' + imageSrc + ')'
p.text = '\n![' + p.text + '](' + imageSrc + ')'
break
case 6:
markup = '> '
break
case 7:
p.text = '> # ' + p.text.replace(/\n/g, '\n> #')
p.text = '> # ' + p.text.replace(/\n/g, '\n> #')
break
case 8:
p.text = '\n ' + p.text.replace(/\n/g, '\n ')
p.text = '\n ' + p.text.replace(/\n/g, '\n ')
break
case 9:
markup = '\n* '
break
case 10:
markup = '\n 1. '
markup = '\n ' + sequence + '. '
break
case 11:
p.text = '\n <iframe src="https://medium.com/media/' + p.iframe.mediaResourceId + '" frameborder=0></iframe>'
break
case 13:
markup = '\n### '
markup = '\n### '
break
case 15:
p.text = '*' + p.text + '*'
break
}

p.text = markup + p.text + '\n'

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) {
function addMarkup(markups_array, open, close, start, end) {
if (markups_array[start]) {
markups_array[start] += open
} else {
Expand All @@ -188,7 +196,7 @@ function addMarkup (markups_array, open, close, start, end) {
return markups_array
}

function createMarkupsArray (markups) {
function createMarkupsArray(markups) {
let markups_array = []
if (!markups || markups.length === 0) {
return markups_array
Expand All @@ -205,6 +213,11 @@ function createMarkupsArray (markups) {
case 3: // anchor tag
addMarkup(markups_array, '[', '](' + m.href + ')', m.start, m.end)
break
case 10: // code tag
if (m.end - m.start < 30) {
addMarkup(markups_array, '`', '`', m.start, m.end)
}
break
default:
console.log('Unknown markup type' + m.type, m)
break
Expand All @@ -224,15 +237,15 @@ function copyToClipboard(input) {
el.setAttribute('readonly', '')
el.value = input

document.body.appendChild(el)
el.select()
document.body.appendChild(el)
el.select()

let success = false;
try {
success = document.execCommand('copy', true);
} catch (err) {}
let success = false;
try {
success = document.execCommand('copy', true);
} catch (err) { }

document.body.removeChild(el);
document.body.removeChild(el);

return success;
return success;
}

0 comments on commit d5766af

Please sign in to comment.