-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempler.js
More file actions
54 lines (51 loc) · 1.2 KB
/
templer.js
File metadata and controls
54 lines (51 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function randomid(){
return 'id'+Math.random().toString(36).slice(-8)
}
function istitle(line){
const re=/^#/;
return re.test(line)
}
function isimg(line){
const re=/\.(jpeg|jpg|gif|png|tiff|bmp|avif|webp)/i;
return re.test(line)
}
function islink(line){
const re=/\.(html|htm|txt)/i;
return re.test(line)
}
function divide(line,def){
def = def||'リンク'
const re=/:/
if(!re.test(line)){
return {name:def,url:line}
}
const ary=line.split(':')
return {name:ary[0],url:ary[1]}
}
function iskaigyo(line){
return !!(line === '')
}
function parse(line){
if(istitle(line)){
const id = randomid()
return `<h2 id="${id}">${line}</h2>`
}
if(isimg(line)){
const {url,name} = divide(line,'画像')
return `<p><a data-img="${url}">${name}</a></p>`
}
if(islink(line)){
const {url,name} = divide(line,'リンク')
return `<p><a href="${url}" target="_blank">${name}</a></p>`
}
if(iskaigyo(line)){
return `<p><br></p>`
}
return `<p>${line}</p>`
}
export function templer(temp){
const br ='\n'
const html = temp.trimStart().split(br).map(parse).join(br)
return '<div class="templer frame pre">'+html+'</div>'
}
if(window) window.templer = templer