Add an option to pass a custom config to remarkableJS

This commit is contained in:
Tissevert 2019-02-22 22:02:07 +01:00
parent faa72435ce
commit cfa501e3c4
4 changed files with 28 additions and 14 deletions

View File

@ -3,7 +3,7 @@ window.addEventListener('load', function() {
var cache = unitJS.Cache(); var cache = unitJS.Cache();
var dom = unitJS.Dom(); var dom = unitJS.Dom();
var fun = unitJS.Fun(); var fun = unitJS.Fun();
var md = new Remarkable({html: true}); var md = new Remarkable(remarkableConfig);
md.block.ruler.enable(['footnote']); md.block.ruler.enable(['footnote']);
var comments = Comments({async: async, cache: cache, dom: dom}); var comments = Comments({async: async, cache: cache, dom: dom});
var domRenderer = DomRenderer({comments: comments, fun: fun, md: md, dom: dom}); var domRenderer = DomRenderer({comments: comments, fun: fun, md: md, dom: dom});

View File

@ -23,6 +23,7 @@ data Arguments = BlogConfig {
, pagesPath :: Maybe FilePath , pagesPath :: Maybe FilePath
, previewArticlesCount :: Int , previewArticlesCount :: Int
, previewLinesCount :: Int , previewLinesCount :: Int
, remarkableConfig :: Maybe FilePath
, wording :: Maybe FilePath , wording :: Maybe FilePath
} }
| Version | Version
@ -41,35 +42,37 @@ blogConfig :: Parser Arguments
blogConfig = BlogConfig blogConfig = BlogConfig
<$> argument filePath (value "." <> metavar "INPUT_DIR") <$> argument filePath (value "." <> metavar "INPUT_DIR")
<*> Optparse.option filePath ( <*> Optparse.option filePath (
metavar "ARTICLES_PATH" metavar "DIRECTORY"
<> value "articles" <> value "articles"
<> short 'a' <> short 'a'
<> long "articles" <> long "articles"
<> help "name of the directory containing the articles within INPUT_DIR" <> help "relative path to the directory containing the articles within INPUT_DIR"
) )
<*> option filePath 'b' "banner" "BANNER_PATH" "path to the file to use for the blog's banner" <*> option filePath 'b' "banner" "FILE" "path to the file to use for the blog's banner"
<*> option filePath 'c' "card-image" "CARD_IMAGE" "path to the image to use for the blog's card" <*> option filePath 'c' "card-image" "FILE" "relative path to the image to use for the blog's card"
<*> option filePath 'C' "comments-at" "INSTANCE_URL" "url of the instance where comments are stored" <*> option filePath 'C' "comments-at" "URL" "url of the instance where comments are stored"
<*> option filePath 'f' "favicon" "FAVICON" "path to the image to use for the blog's favicon" <*> option filePath 'f' "favicon" "FILE" "path to the image to use for the blog's favicon"
<*> option filePath 'H' "head" "HEAD_PATH" "path to the file to add in the blog's head" <*> option filePath 'H' "head" "FILE" "path to the file to add in the blog's head"
<*> option str 'n' "name" "BLOG_NAME" "name of the blog" <*> option str 'n' "name" "BLOG_NAME" "name of the blog"
<*> option filePath 'p' "pages" <*> option filePath 'p' "pages"
"PAGES_PATH" "name of the directory containing the pages within INPUT_DIR" "DIRECTORY" "relative path to the directory containing the pages within INPUT_DIR"
<*> Optparse.option auto ( <*> Optparse.option auto (
metavar "PREVIEW_ARTICLES_COUNT" metavar "INTEGER"
<> value 3 <> value 3
<> short 'A' <> short 'A'
<> long "preview-articles" <> long "preview-articles"
<> help "number of articles listed on the page of each category" <> help "number of articles listed on the page of each category"
) )
<*> Optparse.option auto ( <*> Optparse.option auto (
metavar "PREVIEW_LINES_COUNT" metavar "INTEGER"
<> value 10 <> value 10
<> short 'L' <> short 'L'
<> long "preview-lines" <> long "preview-lines"
<> help "number of lines to display in articles preview" <> help "number of lines to display in articles preview"
) )
<*> option filePath 'w' "wording" "WORDING" "path to the file containing the wording to use" <*> option filePath 'r' "remarkable-config" "FILE"
"path to a file containing a custom RemarkableJS configuration"
<*> option filePath 'w' "wording" "FILE" "path to the file containing the wording to use"
version :: Parser Arguments version :: Parser Arguments
version = flag' Version ( version = flag' Version (

View File

@ -12,6 +12,7 @@ data Path = Path {
articlesPath :: FilePath articlesPath :: FilePath
, commentsAt :: Maybe String , commentsAt :: Maybe String
, pagesPath :: Maybe FilePath , pagesPath :: Maybe FilePath
, remarkableConfig :: Maybe FilePath
, root :: FilePath , root :: FilePath
} }
@ -19,7 +20,10 @@ build :: Arguments -> IO Path
build arguments = do build arguments = do
articlesPath <- filePath . Dir $ Arguments.articlesPath arguments articlesPath <- filePath . Dir $ Arguments.articlesPath arguments
pagesPath <- mapM (filePath . Dir) $ Arguments.pagesPath arguments pagesPath <- mapM (filePath . Dir) $ Arguments.pagesPath arguments
remarkableConfig <- mapM (filePath . File) $ Arguments.remarkableConfig arguments
root <- absolute . Dir $ Arguments.sourceDir arguments root <- absolute . Dir $ Arguments.sourceDir arguments
return $ Path { return $ Path {
articlesPath, commentsAt = Arguments.commentsAt arguments, pagesPath, root articlesPath, commentsAt, pagesPath, remarkableConfig, root
} }
where
commentsAt = Arguments.commentsAt arguments

View File

@ -8,6 +8,7 @@ import qualified Blog (get)
import Control.Monad.IO.Class (MonadIO(..)) import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Reader (ReaderT) import Control.Monad.Reader (ReaderT)
import Data.ByteString.Lazy (ByteString, concat, readFile, writeFile) import Data.ByteString.Lazy (ByteString, concat, readFile, writeFile)
import Data.ByteString.Lazy.Char8 (pack)
import qualified Files (find) import qualified Files (find)
import JSON (exportBlog) import JSON (exportBlog)
import Paths_hablo (getDataDir) import Paths_hablo (getDataDir)
@ -22,12 +23,18 @@ compile sources = concat (header:sources ++ [footer])
header = "(function() {\n" header = "(function() {\n"
footer = "})();" footer = "})();"
var :: (String, ByteString) -> ByteString
var (varName, content) = concat ["var ", pack varName, " = ", content, ";\n"]
generate :: ReaderT Blog IO () generate :: ReaderT Blog IO ()
generate = do generate = do
destinationDir <- (</> "js") <$> (Blog.get $path.$root) destinationDir <- (</> "js") <$> (Blog.get $path.$root)
blogJSON <- exportBlog blogJSON <- exportBlog
remarkablePath <- Blog.get $path.$remarkableConfig
liftIO $ do liftIO $ do
remarkableJSON <- maybe (return "{html: true}") readFile remarkablePath
let jsVars = var <$> [("blog", blogJSON), ("remarkableConfig", remarkableJSON)]
jsFiles <- (</> "js") <$> getDataDir >>= Files.find jsFiles <- (</> "js") <$> getDataDir >>= Files.find
jsCode <- mapM readFile jsFiles jsCode <- mapM readFile jsFiles
createDirectoryIfMissing False destinationDir createDirectoryIfMissing False destinationDir
writeFile (destinationDir </> "hablo.js") $ compile ("var blog = ":blogJSON:";":jsCode ) writeFile (destinationDir </> "hablo.js") $ compile (jsVars ++ jsCode )