frontmatter conversion into toml is done in native host

This commit is contained in:
Marcell Mars 2021-06-05 22:15:47 +02:00
parent b6247c1fab
commit 901eb12374
5 changed files with 54 additions and 36 deletions

View File

@ -1,37 +1,39 @@
function captureFrontMatterAndMarkdown() {
console.log(location.pathname.split('/public')[0])
const formData = new FormData(document.querySelector("form"))
let frontmatter = "---\n"
for (const k of formData.keys()) {
// draft, iscjklanguage are default implicit (false) params in hugo
if (["draft", "iscjklanguage"].includes(k)){
if (formData.get(k) === "true") {
frontmatter += `${k}: true\n`
}
} else if (k.endsWith('[]')) {
formData.set(k, formData.getAll(k))
var s = JSON.stringify(formData.getAll(k)[0].split(","))
if (s != "") {
frontmatter += `${k.slice(0, -2)}: ${s}\n`
}
} else if (k == "relpath") {
filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
gitpath = `${location.pathname.split('/public')[0]}`
} else if (k == "relpermalink") {
publishpath = `${location.pathname.split('/public')[0]}/public${formData.get(k)}index.html`
} else if (["protocol", "offline", "ignore"].includes(k)) {
continue
var formDataJSON = {};
formData.forEach((value, key) => {
if(key.endsWith('[]')){
let kie = key.slice(0, -2);
(kie in formDataJSON) || (formDataJSON[kie] = [])
formDataJSON[kie].push(value);
} else if (['draft', 'iscjklanguage'].includes(key) && value!="true") {
return
} else if (key == "relpath"){
filepath = `${location.pathname.split('/public')[0]}/content/${value}`;
gitpath = `${location.pathname.split('/public')[0]}`;
} else if (key == "relpermalink" ){
publishpath = `${location.pathname.split('/public')[0]}/public${value}index.html`;
} else if (["protocol", "offline", "ignore"].includes(key)) {
return
} else {
var s = JSON.stringify(formData.get(k))
frontmatter += `${k}: ${s}\n`
formDataJSON[key] = value
}
}
frontmatter += "---\n"
});
const content = document.querySelector('textarea').value
let doc = frontmatter + "\n" + content
let publish = formData.get("publish") == "on" ? true : false
let offline = formData.get("offline") == "on" ? true : false
return {"hugopage": doc, "filepath": filepath, "gitpath": gitpath, "publishpath": publishpath, "publish": publish, "offline": offline, "protocol": document.location.protocol.substr(0,4)}
return {
"hugofrontmatter": JSON.stringify(formDataJSON),
"hugocontent": content,
"filepath": filepath,
"gitpath": gitpath,
"publishpath": publishpath,
"publish": publish,
"offline": offline,
"protocol": document.location.protocol.substr(0,4)
}
}
let sb = document.getElementById('sandpointsButton')

View File

@ -2,4 +2,7 @@ module main
go 1.15
require github.com/go-git/go-git/v5 v5.2.0
require (
github.com/go-git/go-git/v5 v5.2.0
github.com/pelletier/go-toml v1.9.2 // indirect
)

View File

@ -32,6 +32,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pelletier/go-toml v1.9.2 h1:7NiByeVF4jKSG1lDF3X8LTIkq2/bu+1uYbIm1eS5tzk=
github.com/pelletier/go-toml v1.9.2/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=

View File

@ -17,6 +17,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pelletier/go-toml"
)
// this project started by copying:
@ -38,13 +39,14 @@ var bufferSize = 8192 * 8
// IncomingMessage represents a message sent to the native host.
type IncomingMessage struct {
Hugopage string `json:"hugopage"`
Filepath string `json:"filepath"`
Gitpath string `json:"gitpath"`
Publishpath string `json:"publishpath"`
Publish bool `json:"publish"`
Offline bool `json:"offline"`
Protocol string `json:"protocol"`
Hugocontent string `json:"hugocontent"`
Hugofrontmatter string `json:"hugofrontmatter"`
Filepath string `json:"filepath"`
Gitpath string `json:"gitpath"`
Publishpath string `json:"publishpath"`
Publish bool `json:"publish"`
Offline bool `json:"offline"`
Protocol string `json:"protocol"`
}
// OutgoingMessage respresents a response to an incoming message query.
@ -157,7 +159,16 @@ func commitChangeToGit(iMsg IncomingMessage) {
relPathFile := filepath.Join(strings.ReplaceAll(iMsg.Filepath, iMsg.Gitpath+"/", ""))
// err = ioutil.WriteFile(iMsg.Filepath, []byte(iMsg.Hugopage), 0644)
// check(err)
writeMessageToFile(iMsg.Filepath, iMsg.Hugopage)
var frontmatter map[string]interface{}
json.Unmarshal([]byte(iMsg.Hugofrontmatter), &frontmatter)
Trace.Printf("Frontmatter JSON: %s", &frontmatter)
tml, err := toml.TreeFromMap(frontmatter)
if err != nil {
Error.Printf("ERROR: Frontmatter JSON to TML:", err)
}
tomlFrontmatter := fmt.Sprintf("+++\n%s+++\n", tml)
Trace.Printf("TOML Frontmatter:", tml)
writeMessageToFile(iMsg.Filepath, fmt.Sprintf("%s%s", tomlFrontmatter, iMsg.Hugocontent))
_, err = w.Add(relPathFile)
if err != nil {
Error.Printf("ERROR: Go-git Add:", err)

Binary file not shown.