Skip to content
Permalink
Browse files

feat: auto image size

  • Loading branch information...
movsb committed May 30, 2019
1 parent 2996a38 commit c5f4125dc5c3628ac01457eece626abc24a23abf
@@ -4,6 +4,6 @@ type HTMLTranslator struct {
PostTranslator
}

func (me *HTMLTranslator) Translate(source string) (string, error) {
func (me *HTMLTranslator) Translate(source string, base string) (string, error) {
return source, nil
}
@@ -1,5 +1,5 @@
package post_translators

type PostTranslator interface {
Translate(source string) (string, error)
Translate(source string, base string) (string, error)
}
@@ -10,12 +10,13 @@ type MarkdownTranslator struct {
PostTranslator
}

func (me *MarkdownTranslator) Translate(source string) (string, error) {
func (me *MarkdownTranslator) Translate(source string, base string) (string, error) {
var err error

cmd := exec.Command(
"marked",
`--lang-prefix "language-"`,
"node",
"./service/modules/post_translators/markdown.js",
base,
)

cmd.Stdin = strings.NewReader(source)
@@ -0,0 +1,35 @@
var fs = require('fs');
var marked = require('marked');
var sizeOf = require('image-size');

var renderer = new marked.Renderer();
renderer.image = function(href, alt, text) {
function old() {
return marked.Renderer.prototype.image.call(renderer, href, alt, text);
}

if(!/^[^/]+$/.test(href)) {
return old();
}

href = escape(href);
alt = alt || "";
var d = {};
try {
var dir = process.argv[2];
var file = dir + '/' + href;
d = sizeOf(file);
} catch(err) {
return old();
}
return `<img data-src="${href}" alt="${alt}" width="${d.width}px" height="${d.height}px" />`;
};

marked.setOptions({
renderer: renderer,
langPrefix: 'language-'
});

var data = fs.readFileSync(0, 'utf-8');
var html = marked(data);
process.stdout.write(html);
@@ -241,7 +241,8 @@ func (s *Service) CreatePost(in *protocols.Post) {
panic("no translator found")
}

p.Content, err = tr.Translate(in.Source)
// TODO doesn't exist
p.Content, err = tr.Translate(in.Source, "./files/0")
if err != nil {
panic(err)
}
@@ -275,7 +276,7 @@ func (s *Service) UpdatePost(in *protocols.Post) {
default:
panic("no translator found")
}
content, err := tr.Translate(in.Source)
content, err := tr.Translate(in.Source, fmt.Sprintf("./files/%d", in.ID))
if err != nil {
panic(err)
}

0 comments on commit c5f4125

Please sign in to comment.
You can’t perform that action at this time.