Skip to content

Commit

Permalink
basic implementation of inline code - fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
devils2ndself committed Sep 29, 2022
1 parent d0ed6e5 commit 1ec3142
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions test/Markdown File.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ This is the title of the file


This is the first paragraph

This is a paragraph with `inline code`, isn't it `cool`??``


# Heading 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sit amet tincidunt nulla. Maecenas nec felis vel nulla faucibus ornare. Integer vitae ipsum fringilla, dapibus enim ac, imperdiet felis. Fusce placerat tortor vitae tortor laoreet vehicula. Donec dictum sit amet quam ut faucibus. Mauris vel tellus tristique, congue sem ut, condimentum erat. Aliquam erat volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris volutpat erat lacus, egestas viverra enim mattis sed.

Expand Down
41 changes: 36 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func GenerateHTML(input string, output string, name string) {
markdownValid := false

if filepath.Ext(input) == ".md" {
// Turn inline Markdown features to HTML
text = GenerateInlineMarkdownHtml(text)
// If prefix is a heading or similar markdown feature
// that overwrites regular paragraph, change it
if prefix, validPrefix := CheckMarkdownPrefix(text); validPrefix {
// We don't want to put headers inside <p> tags
if paragraphOpen {
Expand All @@ -261,12 +265,11 @@ func GenerateHTML(input string, output string, name string) {
// This is set to true so for next non markdown text a new <p> is written first
paragraphDelimiterFound = true
}
_, werr = writer.WriteString(GenerateMarkdownHtml(prefix, text))
_, werr = writer.WriteString(GeneratePrefixMarkdownHtml(prefix, text))
if werr != nil {
log.Fatal("Error writing to new file!")
} else {
markdownValid = true
}
}
markdownValid = true
}
}

Expand Down Expand Up @@ -323,7 +326,35 @@ func CheckMarkdownPrefix(text string) (string, bool) {
return "", false
}

func GenerateMarkdownHtml(prefix string, text string) string {
func GenerateInlineMarkdownHtml(text string) string {
// Idea of set + stack is good until we meet bold text...
// Bold text consists of 2 characters instead of 1, so this will need to be reworked
acceptedDelimiters := map[rune]string{
'`': "<code>%s</code>",
}
var delimiterStack []rune

for _, character := range text {
// If the character is in the set
if _, found := acceptedDelimiters[character]; found {
// Opening delimiter - add to stack
if len(delimiterStack) == 0 || delimiterStack[len(delimiterStack)-1] != character {
delimiterStack = append(delimiterStack, character)
} else {
// Closing delimiter - remove from stack and append to HTML
delimiterStack = delimiterStack[:len(delimiterStack)-1]
if character == '`' {
text = strings.Replace(text, string(character), "<code>", 1)
text = strings.Replace(text, string(character), "</code>", 1)
}
}
}
}

return text
}

func GeneratePrefixMarkdownHtml(prefix string, text string) string {
prefixesHtmlFormatStrings := map[string]string{
"# ": "<h1>%s</h1>",
"## ": "<h2>%s</h2>",
Expand Down

0 comments on commit 1ec3142

Please sign in to comment.