Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Commit

Permalink
update image transformation to support http/https, add folder param
Browse files Browse the repository at this point in the history
  • Loading branch information
titpetric committed Sep 8, 2017
1 parent fc86b8f commit d0ed895
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
23 changes: 21 additions & 2 deletions front/src/markdown/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
var markdown = {
Transform: function (contents) {
Transform: function (contents, folder) {
contents = this.transformPageBreaks(contents)
contents = this.transformMeta(contents)
contents = this.transformLeanpub(contents)
contents = this.transformHugo(contents)
contents = this.transformImages(contents, folder)
return contents
},
transformPageBreaks: function (contents) {
var replacement = '<hr class="pagebreak"/>'
contents = contents.replace(/<!--more-->/gi, replacement)
contents = contents.replace(/{pagebreak}/gi, replacement)
console.log(contents)
return contents
},
transformHugo: function(contents, folder) {
contents = contents.replace(/{% asset_img ([^ ]+) %}/, '![]($1)')
return contents
},
transformImages: function (contents, folder) {
contents = contents.replace(/!\[([^\]]*)\]\(([^\)]+)\)/g, function (m, group) {
if (m.match(/\]\(http/)) {
return m
}
return m.replace('](', '](/contents' + folder + '/');
})
return contents
},
transformLeanpub: function (contents) {
contents = contents.replace(/A>/g, '>')
return contents
},
transformMeta: function (contents) {
Expand Down
36 changes: 22 additions & 14 deletions front/src/markdown/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ var assert = require("assert");
//import markdown from 'index.js'
var markdown = require("./index.js");

describe('Markdown', function(){
describe('Replacements', function(){
describe('Markdown', function () {
describe('Replacements', function () {

it('should replace metadata', function(){
var contents = `title: 'The thing about dates'
var folder = '/test'

it('should replace metadata', function () {
var content = `title: 'The thing about dates'
date: 2017-08-16 18:00:00
tags: [golang, tips, tricks]
---
Adorable`;

var contentsExpected = `| Name | Value |
var contentExpected = `| Name | Value |
|------|-------|
| title | 'The thing about dates' |
| date | 2017-08-16 18 |
Expand All @@ -24,27 +26,33 @@ Adorable`;
Adorable`

contentsNew = markdown.Transform(contents)
contentNew = markdown.Transform(content, folder)

assert.equal(contentNew, contentExpected)
})

assert.equal(contentsNew, contentsExpected)
it('should keep http/s images', function () {
var content = '![](https://scene-si.org/post/2017-09-02-parsing-strings-with-go/heading.jpg)'
var expected = '![](https://scene-si.org/post/2017-09-02-parsing-strings-with-go/heading.jpg)'
assert.equal(markdown.Transform(content, folder), expected)
})

it('should replace image', function(){
it('should replace image', function () {
var content = '{% asset_img heading.jpg %}'
var expected = '![](heading.jpg)'
assert.equal(markdown.Transform(content), expected)
var expected = '![](/contents/test/heading.jpg)'
assert.equal(markdown.Transform(content, folder), expected)
})

it('should replace pagebreak from hugo', function(){
it('should replace pagebreak from hugo', function () {
var content = '<!--more--> <!--more-->'
var expected = '<hr class="pagebreak"/> <hr class="pagebreak"/>'
assert.equal(markdown.Transform(content), expected)
assert.equal(markdown.Transform(content, folder), expected)
})

it('should replace pagebreak from leanpub', function(){
it('should replace pagebreak from leanpub', function () {
var content = '{pagebreak} {pagebreak}'
var expected = '<hr class="pagebreak"/> <hr class="pagebreak"/>'
assert.equal(markdown.Transform(content), expected)
assert.equal(markdown.Transform(content, folder), expected)
})

})
Expand Down
5 changes: 1 addition & 4 deletions front/src/src/views/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export default {
computed: {
preview: function () {
var contents = this.file.contents
contents = contents.replace(/!\[([^\]]*)\]\(/g, '![$1](/contents' + this.file.dir + '/')
// Leanpub markdown
contents = contents.replace(/A>/g, '>')
return markdown.Transform(contents)
return markdown.Transform(contents, this.file.dir)
}
},
beforeRouteLeave (to, from, next) {
Expand Down

0 comments on commit d0ed895

Please sign in to comment.