mirror of
				https://github.com/SphericalKat/medium.rip.git
				synced 2025-11-03 20:25:56 +00:00 
			
		
		
		
	feat: finish markup converters
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
		
							parent
							
								
									4b8607b12a
								
							
						
					
					
						commit
						3de9bfc58c
					
				
							
								
								
									
										2
									
								
								frontend/dist/index.html
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								frontend/dist/index.html
									
									
									
									
										vendored
									
									
								
							@ -6,7 +6,7 @@
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    
 | 
			
		||||
    <title>{{ .Title }}</title>
 | 
			
		||||
  <link rel="stylesheet" href="/assets/index-bab4abd8.css">
 | 
			
		||||
  <link rel="stylesheet" href="/assets/index-544d9ea3.css">
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1 class="text-2xl">Hello, World!</h1>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								frontend/dist/show.html
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								frontend/dist/show.html
									
									
									
									
										vendored
									
									
								
							@ -14,7 +14,7 @@
 | 
			
		||||
        rel="stylesheet">
 | 
			
		||||
 | 
			
		||||
    <title>{{ .Title }}</title>
 | 
			
		||||
  <link rel="stylesheet" href="/assets/show-1b4526c3.css">
 | 
			
		||||
  <link rel="stylesheet" href="/assets/show-7a43d2b5.css">
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body class="flex flex-col h-full w-full items-center">
 | 
			
		||||
@ -22,11 +22,11 @@
 | 
			
		||||
        <h2 class="text-2xl">{{.Title}}</h2>
 | 
			
		||||
        <p><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p>
 | 
			
		||||
        {{range .Nodes}}
 | 
			
		||||
            {{if eq .type "H3"}}
 | 
			
		||||
                <h3>.text</h3>
 | 
			
		||||
            {{if eq .Type "H3"}}
 | 
			
		||||
                <h3>{{.Text}}</h3>
 | 
			
		||||
            {{end}}
 | 
			
		||||
            {{if eq .type "IMG"}}
 | 
			
		||||
                <h3>.text</h3>
 | 
			
		||||
            {{if eq .Type "IMG"}}
 | 
			
		||||
                
 | 
			
		||||
            {{end}}
 | 
			
		||||
        {{end}}
 | 
			
		||||
    </article>
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,9 @@
 | 
			
		||||
package converters
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sort"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/medium.rip/pkg/entities"
 | 
			
		||||
)
 | 
			
		||||
@ -67,20 +69,40 @@ func ranges(text string, markups []entities.Markup) []RangeWithMarkup {
 | 
			
		||||
	return ranges
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Convert(text string, markups []entities.Markup) {
 | 
			
		||||
	// for _, m := range markups {
 | 
			
		||||
	// 	switch m.Type {
 | 
			
		||||
	// 	case "A":
 | 
			
		||||
	// 		if m.Href != nil {
 | 
			
		||||
func Convert(text string, markups []entities.Markup) string {
 | 
			
		||||
	var markedUp strings.Builder
 | 
			
		||||
	for _, r := range ranges(text, markups) {
 | 
			
		||||
		textToWrap := string(text[r.Range[0]:r.Range[1]])
 | 
			
		||||
		markedUp.WriteString(WrapInMarkups(textToWrap, r.Markups))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 		} else if {
 | 
			
		||||
	// 			m.UserID != nil {
 | 
			
		||||
	return markedUp.String()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	// 			}
 | 
			
		||||
	// 		}
 | 
			
		||||
	// 	case "CODE":
 | 
			
		||||
	// 	case "EM":
 | 
			
		||||
	// 	case "STRONG":
 | 
			
		||||
	// 	}
 | 
			
		||||
	// }
 | 
			
		||||
func WrapInMarkups(child string, markups []entities.Markup) string {
 | 
			
		||||
	if len(markups) == 0 {
 | 
			
		||||
		return child
 | 
			
		||||
	}
 | 
			
		||||
	markedUp := markupNodeInContainer(child, markups[0])
 | 
			
		||||
	return WrapInMarkups(markedUp, markups[1:])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func markupNodeInContainer(child string, markup entities.Markup) string {
 | 
			
		||||
	switch markup.Type {
 | 
			
		||||
	case "A":
 | 
			
		||||
		if markup.Href != nil {
 | 
			
		||||
			return fmt.Sprintf(`<a href="%s">%s</a>`, *markup.Href, child)
 | 
			
		||||
		} else if markup.UserID != nil {
 | 
			
		||||
			return fmt.Sprintf(`<a href="https://medium.com/u/%s">%s</a>`, markup.UserID, child)
 | 
			
		||||
		}
 | 
			
		||||
	case "CODE":
 | 
			
		||||
		return fmt.Sprintf(`<code>%s</code>`, child)
 | 
			
		||||
	case "EM":
 | 
			
		||||
		return fmt.Sprintf(`<em>%s</em>`, child)
 | 
			
		||||
	case "STRONG":
 | 
			
		||||
		return fmt.Sprintf(`<strong>%s</strong>`, child)
 | 
			
		||||
	default:
 | 
			
		||||
		return fmt.Sprintf(`<code>%s</code>`, child)
 | 
			
		||||
	}
 | 
			
		||||
	return child
 | 
			
		||||
}
 | 
			
		||||
@ -55,4 +55,23 @@ func TestRanges(t *testing.T) {
 | 
			
		||||
	if len(ranges[3].Markups) != 0 {
 | 
			
		||||
		t.Errorf("Expected markup to be empty, got %v", ranges[3].Markups)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestConvert(t *testing.T) {
 | 
			
		||||
	markup := Convert("strong and emphasized only", []entities.Markup{
 | 
			
		||||
		{
 | 
			
		||||
			Type:  "STRONG",
 | 
			
		||||
			Start: 0,
 | 
			
		||||
			End:   10,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Type:  "EM",
 | 
			
		||||
			Start: 7,
 | 
			
		||||
			End:   21,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	if markup != "<strong>strong </strong><em><strong>and</strong></em><em> emphasized</em> only" {
 | 
			
		||||
		t.Errorf("Expected markup to be <strong>strong </strong><em><strong>and</strong></em><em> emphasized</em> only, got %s", markup)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user