marked-abc 0.1.3 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-abc",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Render sheet music with ABCjs in Markdown documents using Marked.",
5
5
  "main": "./lib/index.esm.js",
6
6
  "module": "./lib/index.esm.js",
package/src/index.ts CHANGED
@@ -74,8 +74,30 @@ type AbcToken = {
74
74
  abc: string,
75
75
  };
76
76
 
77
- export default function(options: MarkedAbcOptions = {}): MarkedExtension {
78
- return {
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,6 +115,7 @@ 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);
118
+ // Trim all leading whitespace or ABCjs freaks out and fails to render it all
96
119
  const abc = raw
97
120
  .replace(SCORE_OPEN_BEGIN, '')
98
121
  .replace(SCORE_CLOSE, '')
@@ -112,6 +135,8 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
112
135
  const eleId = `abc-score-${++scoreCounter}`;
113
136
  const sanitize = options.sanitizer ?? escape;
114
137
 
138
+ abcElements.push({ id: eleId, score: (token as AbcToken).abc });
139
+
115
140
  // Unreachable during tests due to missing DOM
116
141
  // JS moment: the coverage ignore comment is not included in its own ignore meaning I need
117
142
  // to place it before this if statement, meaning the coverage of the if statement itself is
@@ -121,7 +146,6 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
121
146
  waitForElement(`#${eleId}`).then((ele: Element) => {
122
147
  abcjs.renderAbc(
123
148
  ele,
124
- // Trim all leading whitespace or ABCjs freaks out and fails to render it all
125
149
  (token as AbcToken).abc,
126
150
  options.abcOptions,
127
151
  );
@@ -136,4 +160,9 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
136
160
  },
137
161
  }],
138
162
  };
163
+
164
+ return {
165
+ extension,
166
+ forceRenderAll,
167
+ };
139
168
  }