Skip to content

Commit

Permalink
refactoring codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed Nov 9, 2017
1 parent b44867c commit 30157d6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $ cat log.txt

```
$ mklink -i
Press Ctrl+C to stop
Input 'q' or 'quit' to stop
mklimk> https://git.io/vFR5M
[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)
mklimk>
Expand Down
3 changes: 1 addition & 2 deletions cli/mklink/makelink/makelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ func (c *Context) MakeLink(url string) error {
return err
}

r := lnk.Encode(c.linkStyle)
buf := new(bytes.Buffer)
io.Copy(c.writer, io.TeeReader(r, buf))
io.Copy(c.writer, io.TeeReader(lnk.Encode(c.linkStyle), buf))
strLink := buf.String()
if c.clipbrdFlag {
clipboard.WriteAll(strLink)
Expand Down
10 changes: 7 additions & 3 deletions mklink.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Link struct {

//New returns new Link instance
func New(url string) (*Link, error) {
link := &Link{URL: strings.Trim(url, "\t \n")}
link := &Link{URL: trimString(url)}
doc, err := goquery.NewDocument(link.URL)
if err != nil {
return link, err
Expand All @@ -31,17 +31,21 @@ func New(url string) (*Link, error) {

doc.Find("head").Each(func(_ int, s *goquery.Selection) {
s.Find("title").Each(func(_ int, s *goquery.Selection) {
link.Title = strings.Trim(s.Text(), "\t \n")
link.Title = trimString(s.Text())
})
s.Find("meta[name='description']").Each(func(_ int, s *goquery.Selection) {
if v, ok := s.Attr("content"); ok {
link.Description = strings.Trim(v, "\t \n")
link.Description = trimString(v)
}
})
})

return link, nil
}
func trimString(s string) string {
s = strings.Replace(s, "\n", " ", -1)
return strings.Trim(s, "\t ")
}

//JSON returns string (io.Reader) with JSON format
func (lnk *Link) JSON() (io.Reader, error) {
Expand Down

0 comments on commit 30157d6

Please sign in to comment.