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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-abc",
3
- "version": "0.1.2",
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,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
- const abc = raw.replace(SCORE_OPEN_BEGIN, '').replace(SCORE_CLOSE, '');
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
- // Trim all leading whitespace or ABCjs freaks out and fails to render it all
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
  }