From 286b6010461ad284956a1b4540b98a7ecd49c8ab Mon Sep 17 00:00:00 2001 From: Thomas Marschall-Taylor Date: Sun, 10 May 2020 00:19:22 +0000 Subject: [PATCH 1/3] added mermaid graphics to allmark like seen on gitlab, minor CSS tweaks to add borders to tables --- cli/allmark.go | 2 +- .../markdowntohtml/preprocessor/mermaid.go | 63 + .../preprocessor/preprocessor.go | 7 + web/view/templates/defaulttheme/master.go | 16 + web/view/themes/themefiles/latest.go | 1 + web/view/themes/themefiles/mermaidjs.go | 22568 ++++++++++++++++ web/view/themes/themefiles/printcss.go | 24 +- web/view/themes/themefiles/screencss.go | 14 +- web/view/themes/themes.go | 1 + 9 files changed, 22693 insertions(+), 3 deletions(-) create mode 100644 services/converter/markdowntohtml/preprocessor/mermaid.go create mode 100644 web/view/themes/themefiles/mermaidjs.go diff --git a/cli/allmark.go b/cli/allmark.go index 1da3167a..a8b17472 100644 --- a/cli/allmark.go +++ b/cli/allmark.go @@ -37,7 +37,7 @@ const ( CommandNameVersion = "version" ) -var version = "v0.10.0-dev" +var version = "v0.20.0-dev-TMT" var ( serveFlags = flag.NewFlagSet("serve-flags", flag.ContinueOnError) diff --git a/services/converter/markdowntohtml/preprocessor/mermaid.go b/services/converter/markdowntohtml/preprocessor/mermaid.go new file mode 100644 index 00000000..f5824a4b --- /dev/null +++ b/services/converter/markdowntohtml/preprocessor/mermaid.go @@ -0,0 +1,63 @@ +// Copyright 2020 Thomas Marschall. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package preprocessor + +import ( + "github.com/andreaskoch/allmark/common/paths" + "github.com/andreaskoch/allmark/model" + "fmt" + "regexp" + "strings" +) + +var ( + // ```mermaid``` + mermaidMarkdownExtensionPattern = regexp.MustCompile(`(?s)\x60{3}mermaid(.*?)\x60{3}`) +) + +func newMermaidExtension(pathProvider paths.Pather, files []*model.File) *MermaidTableExtension { + return &MermaidTableExtension{ + pathProvider: pathProvider, + files: files, + } +} + +type MermaidTableExtension struct { + pathProvider paths.Pather + files []*model.File +} + +func (converter *MermaidTableExtension) Convert(markdown string) (convertedContent string, converterError error) { + + convertedContent = markdown + + for _, match := range mermaidMarkdownExtensionPattern.FindAllStringSubmatch(convertedContent, -1) { + + if len(match) != 2 { + continue + } + + // parameters + originalText := strings.TrimSpace(match[0]) + code := strings.TrimSpace(match[1]) + + // get the code + renderedCode := converter.getMermaidDiv(code) + + // replace markdown + convertedContent = strings.Replace(convertedContent, originalText, renderedCode, 1) + + } + + return convertedContent, nil +} + +func (converter *MermaidTableExtension) getMermaidDiv(code string) string { + + // div container with mermaid code + divCode := fmt.Sprintf(`
%s
`, code) + return divCode + +} diff --git a/services/converter/markdowntohtml/preprocessor/preprocessor.go b/services/converter/markdowntohtml/preprocessor/preprocessor.go index b34d490b..0a794553 100644 --- a/services/converter/markdowntohtml/preprocessor/preprocessor.go +++ b/services/converter/markdowntohtml/preprocessor/preprocessor.go @@ -83,6 +83,13 @@ func (preprocessor *Preprocessor) Convert( preprocessor.logger.Warn("Error while converting reference extensions. Error: %s", referenceConversionError) } + // markdown extension: mermaid + mermaidConverter := newMermaidExtension(pathProvider, files) + markdown, mermaidConversionError := mermaidConverter.Convert(markdown) + if mermaidConversionError != nil { + preprocessor.logger.Warn("Error while converting reference extensions. Error: %s", referenceConversionError) + } + return markdown, nil } diff --git a/web/view/templates/defaulttheme/master.go b/web/view/templates/defaulttheme/master.go index d3eda017..a0bfb2b4 100644 --- a/web/view/templates/defaulttheme/master.go +++ b/web/view/templates/defaulttheme/master.go @@ -145,6 +145,22 @@ const masterTemplate = ` + + +