在 Ghost 中显示乐谱
Ghost 支持 HTML 标签,你可以在博客中嵌入 div 和 JavaScript 脚本,用 VexFlow 这个开源的乐谱渲染库,就能在 Ghost 博客中显示乐谱。
在 Markdown 编辑器中输入以下代码:
<div id="boo"></div>
<script type="text/javascript" src="https://unpkg.com/vexflow/releases/vexflow-min.js"></script>
<script>
VF = Vex.Flow;
// Create an SVG renderer and attach it to the DIV element named "boo".
var div = document.getElementById("boo")
var renderer = new VF.Renderer(div, VF.Renderer.Backends.SVG);
// Configure the rendering context.
renderer.resize(500, 150);
var context = renderer.getContext();
context.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");
// Create a stave of width 400 at position 10, 40 on the canvas.
var stave = new VF.Stave(10, 40, 400);
// Add a clef and time signature.
stave.addClef("treble").addTimeSignature("4/4");
// Connect it to the rendering context and draw!
stave.setContext(context).draw();
var notes = [
// A quarter-note C.
new VF.StaveNote({clef: "treble", keys: ["c/4"], duration: "q" }),
// A quarter-note D.
new VF.StaveNote({clef: "treble", keys: ["d/4"], duration: "q" }),
// A quarter-note rest. Note that the key (b/4) specifies the vertical
// position of the rest.
new VF.StaveNote({clef: "treble", keys: ["b/4"], duration: "qr" }),
// A C-Major chord.
new VF.StaveNote({clef: "treble", keys: ["c/4", "e/4", "g/4"], duration: "q" })
];
// Create a voice in 4/4 and add above notes
var voice = new VF.Voice({num_beats: 4, beat_value: 4});
voice.addTickables(notes);
// Format and justify the notes to 400 pixels.
var formatter = new VF.Formatter().joinVoices([voice]).format([voice], 400);
// Render voice
voice.draw(context, stave);
</script>
效果如下:
----------更新-----------
Ghost 更新到 1.0 后,需要在 HTML 代码前后分别添加
<pre style="background:#FFF">
和 </pre>
。
否则 JavaScript 代码会被转义导致出错无法执行。在默认主题下 <pre>
元素是黑色背景,所以需要在 <pre>
后加上 style="background:#FFF"
防止乐谱背景显示为黑色。