2025-01-17 05:02:31 +01:00
package backend
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/google/uuid"
)
func ConvertToMarkdown ( c * Config , filename string ) ( [ ] byte , error ) {
var stderr bytes . Buffer
2025-01-19 09:00:02 +01:00
tmpDir , err := os . MkdirTemp ( os . TempDir ( ) , "cpolis_images" )
2025-01-17 05:02:31 +01:00
if err != nil {
return nil , fmt . Errorf ( "error creating temporary directory: %v" , err )
}
defer os . RemoveAll ( tmpDir )
2025-01-19 09:00:02 +01:00
articleFileName := filepath . Join ( os . TempDir ( ) , fmt . Sprint ( uuid . New ( ) , ".md" ) )
2025-01-17 05:02:31 +01:00
cmd := exec . Command ( "pandoc" , "-s" , "-f" , "docx" , "-t" , "commonmark_x" , "-o" , articleFileName , "--extract-media" , tmpDir , filename ) // TODO: Is writing to a file necessary?
cmd . Stderr = & stderr
if err = cmd . Run ( ) ; err != nil {
return nil , fmt . Errorf ( "error converting docx to markdown: %v: %v" , err , stderr . String ( ) )
}
defer os . Remove ( articleFileName )
articleContent , err := os . ReadFile ( articleFileName )
if err != nil {
return nil , fmt . Errorf ( "error reading markdown file: %v" , err )
}
imageNames , err := filepath . Glob ( filepath . Join ( tmpDir , "/media/*" ) )
if err != nil {
return nil , fmt . Errorf ( "error getting docx images from temporary directory: %v" , err )
}
for _ , name := range imageNames {
image , err := os . Open ( name )
if err != nil {
return nil , fmt . Errorf ( "error opening image file %v: %v" , name , err )
}
defer image . Close ( )
newImageName , err := SaveImage ( image , c . MaxImgHeight , c . MaxImgWidth , c . PicsDir )
if err != nil {
return nil , fmt . Errorf ( "error saving image %v: %v" , name , err )
}
2025-01-18 18:18:03 +01:00
articleContent = regexp . MustCompile ( name ) . ReplaceAll ( articleContent , [ ] byte ( c . Domain + "/image/serve/" + newImageName ) )
2025-01-17 05:02:31 +01:00
}
return articleContent , nil
}