Skip to content

Commit

Permalink
MAJOR: Migrate from CommonJS to ES Module (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadanan authored Jan 9, 2024
1 parent 1c57d4f commit 0e4a302
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 216 deletions.
17 changes: 6 additions & 11 deletions bin/headless.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"use strict";
import "global-jsdom/register";
import MarkdownIt from "markdown-it";
import MarkdownItMusic from "../index.js";
import { createSVGWindow } from "svgdom";
import { registerWindow } from "@svgdotjs/svg.js";

require("jsdom-global")();

const MarkdownIt = require("markdown-it");
const MarkdownItMusic = require("../index");
const { createSVGWindow } = require("svgdom");
const { registerWindow } = require("@svgdotjs/svg.js");

function render(src, transpose, theme) {
export function render(src, transpose, theme) {
const window = createSVGWindow();
const document = window.document;
registerWindow(window, document);
Expand All @@ -17,5 +14,3 @@ function render(src, transpose, theme) {
md.setTheme(theme);
return md.render(src);
}

module.exports = render;
25 changes: 13 additions & 12 deletions bin/musicmd.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node
import { createReadStream, createWriteStream } from "fs";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { render } from "./headless.js";

"use strict";

const render = require("./headless");
const fs = require("fs");
const yargs = require("yargs");

const argv = yargs
.scriptName("musicmd")
.command("* [markdown]", "Render the markdown to HTML")
const argv = yargs(hideBin(process.argv))
.command({
command: "* [markdown]",
describe: "Render the markdown to HTML",
})
.option("outfile", {
alias: "o",
describe: "render the output of infile to outfile",
Expand All @@ -26,10 +26,11 @@ const argv = yargs
default: "light",
nargs: 1,
})
.help().argv;
.help()
.parse();

const ifp = argv.markdown ? fs.createReadStream(argv.markdown) : process.stdin;
const ofp = argv.outfile ? fs.createWriteStream(argv.outfile) : process.stdout;
const ifp = argv.markdown ? createReadStream(argv.markdown) : process.stdin;
const ofp = argv.outfile ? createWriteStream(argv.outfile) : process.stdout;

const chunks = [];

Expand Down
8 changes: 1 addition & 7 deletions header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

const HEADER = `<!DOCTYPE html>
<script>
// https://cdn.jsdelivr.net/npm/@floating-ui/[email protected]
Expand Down Expand Up @@ -280,7 +278,7 @@ function getThemeStyles(theme) {
return theme;
}

function getHeader(opts = { theme: "light", fontSize: 1 }) {
export function getHeader(opts = { theme: "light", fontSize: 1 }) {
return (
HEADER +
`
Expand All @@ -294,7 +292,3 @@ ${getThemeStyles(opts.theme)}
`
);
}

module.exports = {
getHeader,
};
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use strict";
const meta = require("markdown-it-meta");
const markdownitfence = require("markdown-it-fence");
const abc = require("./renderers/abc_renderer.js");
const vextab = require("./renderers/vextab_renderer.js");
const ChordsRenderer = require("./renderers/chords_renderer.js");
const { parseVerse, isVoiceLine } = require("./parsers/verse");
const { getHeader } = require("./header");
const { chordCarousel } = require("./renderers/chord_carousel.js");

function MarkdownMusic(md) {
import markdownitfence from "markdown-it-fence";
import meta from "markdown-it-meta";
import { getHeader } from "./header.js";
import { isVoiceLine, parseVerse } from "./parsers/verse.js";
import * as abc from "./renderers/abc_renderer.js";
import { chordCarousel } from "./renderers/chord_carousel.js";
import { ChordsRenderer } from "./renderers/chords_renderer.js";
import * as vextab from "./renderers/vextab_renderer.js";

export default function MarkdownMusic(md) {
md.use(meta);
md.rendererRegistry = {};
md.userOpts = { headers: [] };
Expand Down Expand Up @@ -65,7 +64,9 @@ function MarkdownMusic(md) {
};

md.renderer.rules.mmdFooter = () => {
return md.chordsRenderer.chordsUsed.map(chordCarousel).join("");
return md.chordsRenderer.chordsUsed
.map((voicing) => chordCarousel(voicing))
.join("");
};

md.rendererRegistry[abc.lang] = abc.callback;
Expand Down Expand Up @@ -124,5 +125,3 @@ function getLines(state, startLine, endLine) {
state.eMarks[endLine - 1 || startLine],
);
}

module.exports = MarkdownMusic;
6 changes: 2 additions & 4 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
* @jest-environment jsdom
*/

"use strict";

const MarkdownIt = require("markdown-it");
const MarkdownItMusic = require("./index");
import MarkdownIt from "markdown-it";
import MarkdownItMusic from "./index.js";

describe("Markdown It Music", () => {
var md;
Expand Down
24 changes: 7 additions & 17 deletions lib/chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function mod(n, m) {
return ((n % m) + m) % m;
}

class Chord {
export class Chord {
constructor(root, quality, bass) {
this.root = root;
this.quality = quality || "";
Expand Down Expand Up @@ -157,18 +157,18 @@ class Chord {
}
}

function compareNotes(note1, note2) {
export function compareNotes(note1, note2) {
return (NOTE_SEQUENCE.get(note1) || 0) - (NOTE_SEQUENCE.get(note2) || 0);
}

function compareQualities(quality1 = "", quality2 = "") {
export function compareQualities(quality1 = "", quality2 = "") {
if (quality1.length != quality2.length) {
return quality1.length - quality2.length;
}
return quality1.localeCompare(quality2);
}

function compareChords(chord1, chord2) {
export function compareChords(chord1, chord2) {
let cmp = compareNotes(chord1.root, chord2.root);
if (cmp != 0) {
return cmp;
Expand All @@ -180,29 +180,19 @@ function compareChords(chord1, chord2) {
return compareNotes(chord1.bass, chord2.bass);
}

function isAnnotation(str) {
export function isAnnotation(str) {
return !!str.match(ANNOTATION_PATTERN);
}

function isChord(str) {
export function isChord(str) {
return !!str.match(CHORD_PATTERN);
}

function parseChord(str) {
export function parseChord(str) {
const tokens = str.match(CHORD_PATTERN);
if (!tokens) {
throw new Error(`${str} is not a valid chord`);
}

return new Chord(tokens[1], tokens[2], tokens[3]);
}

module.exports = {
compareNotes,
compareQualities,
compareChords,
parseChord,
isAnnotation,
isChord,
Chord,
};
4 changes: 2 additions & 2 deletions lib/chord.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {
import {
Chord,
compareChords,
parseChord,
isChord,
isAnnotation,
} = require("./chord");
} from "./chord.js";

describe("Chord", () => {
test.each([
Expand Down
12 changes: 3 additions & 9 deletions lib/chordbook.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";
import { parseChord } from "./chord.js";
import { compareVoicings, parseVoicing } from "./voicing.js";

const { parseVoicing, compareVoicings } = require("./voicing");
const { parseChord } = require("./chord");

const guitarChordbook = new Map();
export const guitarChordbook = new Map();

function addGuitarChord(chord, shorthand) {
chord.aliases().map((c) => {
Expand Down Expand Up @@ -173,7 +171,3 @@ for (let os = 1; os < 17; os++) {
`o${os} m1 b3,5,${os} n2,${2 + os} n6,${2 + os}`,
);
}

module.exports = {
guitarChordbook,
};
6 changes: 2 additions & 4 deletions lib/chordbook.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const { parseVoicing } = require("./voicing");
const { guitarChordbook } = require("./chordbook");
import { parseVoicing } from "./voicing.js";
import { guitarChordbook } from "./chordbook.js";

describe("Guitar Chordbook", () => {
test.each([
Expand Down
11 changes: 2 additions & 9 deletions lib/voicing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

class Voicing {
constructor(offset = 1, mutes = [], notes = [], barres = []) {
this.offset = offset;
Expand All @@ -9,7 +7,7 @@ class Voicing {
}
}

function compareVoicings(voicing1, voicing2) {
export function compareVoicings(voicing1, voicing2) {
let cmp = voicing1.offset - voicing2.offset;
if (cmp != 0) {
return cmp;
Expand Down Expand Up @@ -40,7 +38,7 @@ function compareVoicings(voicing1, voicing2) {
* @param {string} str The voicing using our shorthand notation.
* @return {Object} The parsed fingering.
*/
function parseVoicing(str) {
export function parseVoicing(str) {
const voicing = new Voicing();
if (!str) {
return voicing;
Expand Down Expand Up @@ -83,8 +81,3 @@ function parseVoicing(str) {

return voicing;
}

module.exports = {
parseVoicing,
compareVoicings,
};
4 changes: 1 addition & 3 deletions lib/voicing.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const { parseVoicing, compareVoicings } = require("./voicing");
import { parseVoicing, compareVoicings } from "./voicing.js";

describe("Voicing", () => {
test("parses empty shorthand", () => {
Expand Down
33 changes: 13 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "markdown-it-music",
"version": "1.0.4",
"type": "module",
"description": "Renderer for Music Markdown.",
"main": "index.js",
"scripts": {
"test": "jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"lint": "eslint .",
"posttest": "eslint .",
"fix-lint": "eslint . --fix"
Expand All @@ -21,7 +22,7 @@
"@svgdotjs/svg.js": "^3.1.1",
"abcjs": "^6.1.6",
"fast-memoize": "^2.5.2",
"jsdom-global": "^3.0.2",
"global-jsdom": "^9.2.0",
"markdown-it": "^14.0.0",
"markdown-it-fence": "^0.1.3",
"markdown-it-meta": "0.0.1",
Expand All @@ -36,7 +37,6 @@
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.2.2",
"jest-environment-jsdom": "^29.2.2",
"prettier": "^3.0.3",
"rewire": "^7.0.0"
"prettier": "^3.0.3"
}
}
Loading

0 comments on commit 0e4a302

Please sign in to comment.