marked-abc 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/lib/index.d.ts +4 -1
- package/lib/index.esm.js +38 -37
- package/lib/index.esm.js.map +3 -3
- package/lib/index.umd.js +40 -39
- package/lib/index.umd.js.map +3 -3
- package/package.json +1 -1
- package/src/index.ts +41 -5
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -74,8 +74,30 @@ type AbcToken = {
|
|
|
74
74
|
abc: string,
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
export default function(
|
|
78
|
-
|
|
77
|
+
export default function(
|
|
78
|
+
options: MarkedAbcOptions = {},
|
|
79
|
+
): { extension: MarkedExtension, forceRenderAll: () => void } {
|
|
80
|
+
const abcElements: { id: string, score: string }[] = [];
|
|
81
|
+
|
|
82
|
+
// Can only be run in browser.
|
|
83
|
+
/* node:coverage disable */
|
|
84
|
+
/**
|
|
85
|
+
* Re-render all known abc scores.
|
|
86
|
+
*
|
|
87
|
+
* While this should be done automatically in most cases, it may be need to be called on element
|
|
88
|
+
* mount if the markdown was originally rendered server-side (where ABCjs will not run).
|
|
89
|
+
*/
|
|
90
|
+
function forceRenderAll() {
|
|
91
|
+
for (const abc of abcElements) {
|
|
92
|
+
const match = document.querySelector(`#${abc.id}`);
|
|
93
|
+
if (match) {
|
|
94
|
+
abcjs.renderAbc(match, abc.score, options.abcOptions);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/* node:coverage enable */
|
|
99
|
+
|
|
100
|
+
const extension: MarkedExtension = {
|
|
79
101
|
extensions: [{
|
|
80
102
|
name: 'abcScore',
|
|
81
103
|
level: 'block',
|
|
@@ -93,7 +115,15 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
|
|
|
93
115
|
}
|
|
94
116
|
const end = endMatch.index + endMatch[0].length;
|
|
95
117
|
const raw = src.slice(0, end);
|
|
96
|
-
|
|
118
|
+
// Trim all leading whitespace or ABCjs freaks out and fails to render it all
|
|
119
|
+
const abc = raw
|
|
120
|
+
.replace(SCORE_OPEN_BEGIN, '')
|
|
121
|
+
.replace(SCORE_CLOSE, '')
|
|
122
|
+
.trim()
|
|
123
|
+
.split('\n')
|
|
124
|
+
.map((line) => line.trim())
|
|
125
|
+
.join('\n');
|
|
126
|
+
console.log(abc);
|
|
97
127
|
return {
|
|
98
128
|
type: 'abcScore',
|
|
99
129
|
raw,
|
|
@@ -105,6 +135,8 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
|
|
|
105
135
|
const eleId = `abc-score-${++scoreCounter}`;
|
|
106
136
|
const sanitize = options.sanitizer ?? escape;
|
|
107
137
|
|
|
138
|
+
abcElements.push({ id: eleId, score: (token as AbcToken).abc });
|
|
139
|
+
|
|
108
140
|
// Unreachable during tests due to missing DOM
|
|
109
141
|
// JS moment: the coverage ignore comment is not included in its own ignore meaning I need
|
|
110
142
|
// to place it before this if statement, meaning the coverage of the if statement itself is
|
|
@@ -114,8 +146,7 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
|
|
|
114
146
|
waitForElement(`#${eleId}`).then((ele: Element) => {
|
|
115
147
|
abcjs.renderAbc(
|
|
116
148
|
ele,
|
|
117
|
-
|
|
118
|
-
(token as AbcToken).abc.split('\n').map((line: string) => line.trim()).join('\n'),
|
|
149
|
+
(token as AbcToken).abc,
|
|
119
150
|
options.abcOptions,
|
|
120
151
|
);
|
|
121
152
|
});
|
|
@@ -129,4 +160,9 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
|
|
|
129
160
|
},
|
|
130
161
|
}],
|
|
131
162
|
};
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
extension,
|
|
166
|
+
forceRenderAll,
|
|
167
|
+
};
|
|
132
168
|
}
|