Textboxでフロントマターを処理する
code:script.js
function frontMatterParse(text){//フロントマターを見つける
//改行コードが混ざっているので注意
const regex = /^---\r\n(\s\S*?)\r\n---\r\n/;
const fm = text.match(regex);
const afterText = text.replace(regex,"")
let type = ""
let data = ""
if (fm){
const typeRegex = /type: (.*)\n\r/;
const hasType = fm1.match(typeRegex)
if ( hasType ){
type = "<span> " + hasType1 +"</span>"
}
const dateRegex = /date: (.*)\n/;
const hasdate = fm1.match(dateRegex)
if ( hasdate ){
const rdate = new Date(hasdate1)
data = "<div class='datetime'><span>" + rdate.toLocaleString() + "</span></div>"
}
}
return type + data + afterText
}
r-styleのものを参考にして作った
こちらはサーバーにアップしたmdファイルを読み込んでいるので改行コードがシンプルに扱える
code:smaple.js
function frontMatterParse(text){//フロントマターを見つける
const regex = /^---\n(\s\S*?)\n---/;
const fm = text.match(regex);
const afterText = text.replace(regex,"")
let title = ""
let data = ""
if (fm){
const titleRegex = /^title : (.*)\n/;
const hasTitle = fm1.match(titleRegex)
if ( hasTitle ){
title = "<h2> " + hasTitle1 +"</h2>"
}
const dateRegex = /date : (.*)\n/;
const hasdate = fm1.match(dateRegex)
if ( hasdate ){
const rdate = new Date(hasdate1)
console.log(rdate)
data = "<div class='datetime'><span>" + rdate.toLocaleString() + "</span></div>"
}
}
return title + data + afterText
}
→Textboxでお仲間ページを表示する