Skip to content

Commit 2ea3e59

Browse files
committed
init ascii fretboard
Signed-off-by: Ca Chen <[email protected]>
1 parent 7b14845 commit 2ea3e59

File tree

4 files changed

+435
-1
lines changed

4 files changed

+435
-1
lines changed

public/js/extra.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import markdownitContainer from 'markdown-it-container'
2323
/* Defined regex markdown it plugins */
2424
import Plugin from 'markdown-it-regexp'
2525

26+
import { renderFretBoard } from '../vendor/fretboard/fretboard.js'
27+
2628
require('prismjs/themes/prism.css')
2729
require('prismjs/components/prism-wiki')
2830
require('prismjs/components/prism-haskell')
@@ -483,6 +485,20 @@ export function finishView (view) {
483485
console.warn(err)
484486
}
485487
})
488+
// fretboard
489+
const fretboard = view.find('div.fretboard_instance.raw').removeClass('raw')
490+
fretboard.each((key, value) => {
491+
const $value = $(value)
492+
493+
try {
494+
const $ele = $(value).parent().parent()
495+
$ele.html(renderFretBoard($value.text()))
496+
} catch (err) {
497+
$value.unwrap()
498+
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
499+
console.warn(err)
500+
}
501+
})
486502

487503
// image href new window(emoji not included)
488504
const images = view.find('img.raw[src]').removeClass('raw')

public/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var cursorActivityDebounce = 50
102102
var cursorAnimatePeriod = 100
103103
var supportContainers = ['success', 'info', 'warning', 'danger', 'spoiler']
104104
var supportCodeModes = ['javascript', 'typescript', 'jsx', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'haskell', 'coffeescript', 'yaml', 'pug', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile', 'tiddlywiki', 'mediawiki', 'go', 'gherkin'].concat(hljs.listLanguages())
105-
var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc', 'plantuml', 'vega', 'geo']
105+
var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc', 'plantuml', 'vega', 'geo', 'fretboard']
106106
var supportHeaders = [
107107
{
108108
text: '# h1',

public/vendor/fretboard/fretboard.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import {
2+
dotEmpty,
3+
dotEmpty_h,
4+
dot,
5+
dot_h,
6+
dotWideLeft,
7+
dotWideRight,
8+
dotWideMiddle,
9+
string_o,
10+
string_x,
11+
fretb_vert_4,
12+
fretb_vert_5,
13+
fretb_vert_7,
14+
fretb_vert_9,
15+
fretb_vert_12,
16+
fretb_vert_15,
17+
fretb_horiz_5,
18+
fretb_horiz_6,
19+
fretb_horiz_7,
20+
} from './fretboardSVG.js'
21+
22+
const switchList_v = {
23+
'o': `<div class='cell dot'>${dot}</div>`,
24+
'*': `<div class='cell dot faded'>${dot}</div>`,
25+
'(': `<div class='cell'>${dotWideLeft}</div>`,
26+
')': `<div class='cell'>${dotWideRight}</div>`,
27+
'=': `<div class='cell'>${dotWideMiddle}</div>`,
28+
'^': `<div class='cell'>${string_o}</div>`,
29+
'x': `<div class='cell'>${string_x}</div>`,
30+
'|': `<div class='cell empty'>${dotEmpty}</div>`,
31+
' ': `<div class='cell empty'>${dotEmpty}</div>`
32+
}
33+
const switchList_h = {
34+
'o': `<div class='cell dot'>${dot_h}</div>`,
35+
'*': `<div class='cell dot faded'>${dot_h}</div>`,
36+
'O': `<div class='cell dot root'>${dot_h}</div>`,
37+
'-': `<div class='cell empty'>${dotEmpty_h}</div>`,
38+
' ': `<div class='cell empty'>${dotEmpty_h}</div>`
39+
}
40+
41+
export const renderFretBoard = (content) => {
42+
43+
let fretboardHTML = $('<div class="fretboard_instance"></div>')
44+
if (content.includes('title:')) {
45+
const getTitle = content.split('\n', 1)[0].split('title:')[1].trim()
46+
$(fretboardHTML).append($(`<div class="title">${getTitle}</div>`))
47+
content = content.split('\n').slice(1).join('\n')
48+
}
49+
50+
let fretType = ''
51+
if (content.startsWith('type:')) {
52+
fretType = content.split('\n', 1)[0].split('type:')[1].trim().split(' ')
53+
content = content.split('\n').slice(1).join('\n')
54+
}
55+
56+
let fretb_orientation = fretType[0].startsWith('v') ? 'vert' : 'horiz'
57+
let fretb_len = fretType[0].substring(1)
58+
59+
// TODO: to get svg dynamically
60+
let fretb_bg = ''
61+
switch (fretb_len) {
62+
case '4':
63+
fretb_bg = fretb_vert_4
64+
break
65+
case '5':
66+
if (fretb_orientation === 'vert')
67+
fretb_bg = fretb_vert_5
68+
else
69+
fretb_bg = fretb_horiz_5
70+
break
71+
case '6':
72+
fretb_bg = fretb_horiz_6
73+
break
74+
case '7':
75+
if (fretb_orientation === 'vert')
76+
fretb_bg = fretb_vert_7
77+
else
78+
fretb_bg = fretb_horiz_7
79+
break
80+
case '9':
81+
fretb_bg = fretb_vert_9
82+
break
83+
case '12':
84+
fretb_bg = fretb_vert_12
85+
break
86+
case '15':
87+
fretb_bg = fretb_vert_15
88+
break
89+
}
90+
91+
// create fretboard background HTML
92+
let fretb_class = fretType[0][0] + ' ' + fretType[0]
93+
let nut = (fretType[1] && fretType[1] === 'noNut')?'noNut':''
94+
let svgHTML = $(`<div class="svg_wrapper ${fretb_class} ${nut}"></div>`)
95+
$(svgHTML).append($(fretb_bg))
96+
97+
// create cells HTML
98+
let cellsHTML = $('<div class="cells"></div>')
99+
let switchList = fretb_orientation === 'vert' ? switchList_v : switchList_h
100+
let contentCell = content.split('')
101+
// Go through each ASCII character...
102+
contentCell.forEach(char => {
103+
if (switchList[char] !== undefined) {
104+
cellsHTML.append($(switchList[char]));
105+
}
106+
})
107+
108+
$(svgHTML).append($(cellsHTML))
109+
$(fretboardHTML).append($(svgHTML))
110+
111+
return fretboardHTML[0].outerHTML
112+
}

0 commit comments

Comments
 (0)