-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to set a custom markdown theme by specifying the path to…
… a CSS file instead of the name of a built-in theme
- Loading branch information
Showing
5 changed files
with
150 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func theme(value string) (string, http.HandlerFunc) { | ||
// Check if the value is a path to a CSS file | ||
if filepath.Ext(value) == ".css" { | ||
cssPath, err := filepath.Abs(value) | ||
if err != nil { | ||
log.Printf("Error finding custom theme: %s: %s. Falling back to: %s.", value, err.Error(), DefaultTheme) | ||
return themePath(DefaultTheme), nil | ||
} | ||
|
||
if stat, err := os.Stat(cssPath); err == nil && !stat.IsDir() { | ||
// Add handler to serve content | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, cssPath) | ||
} | ||
return themePath("custom"), handler | ||
} | ||
|
||
log.Printf("Error finding custom theme: %s. Falling back to: %s.", value, DefaultTheme) | ||
return themePath(DefaultTheme), nil | ||
} | ||
|
||
// If the value is not a path, check that it is a valid bundled theme | ||
localPath := themeAssetPath(value) | ||
_, err := Asset(localPath) | ||
if err != nil { | ||
log.Printf("Invalid theme selected: %s. Falling back to: %s.", value, DefaultTheme) | ||
return themePath(DefaultTheme), nil | ||
} | ||
|
||
return themePath(value), nil | ||
} | ||
|
||
func themePath(value string) string { | ||
return fmt.Sprintf("/assets/themes/%s.css", value) | ||
} | ||
|
||
func themeAssetPath(value string) string { | ||
return fmt.Sprintf("assets/themes/%s.css", value) | ||
} |