marked-abc 0.1.3 → 0.2.1

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.1",
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,25 @@ 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
+ // Can only be run in browser.
81
+ /* node:coverage disable */
82
+ /**
83
+ * Re-render all known abc scores.
84
+ *
85
+ * While this should be done automatically in most cases, it may be need to be called on element
86
+ * mount if the markdown was originally rendered server-side (where ABCjs will not run).
87
+ */
88
+ function forceRenderAll() {
89
+ for (const match of document.querySelectorAll('.abc-score')) {
90
+ abcjs.renderAbc(match, match.textContent, options.abcOptions);
91
+ }
92
+ }
93
+ /* node:coverage enable */
94
+
95
+ const extension: MarkedExtension = {
79
96
  extensions: [{
80
97
  name: 'abcScore',
81
98
  level: 'block',
@@ -93,6 +110,7 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
93
110
  }
94
111
  const end = endMatch.index + endMatch[0].length;
95
112
  const raw = src.slice(0, end);
113
+ // Trim all leading whitespace or ABCjs freaks out and fails to render it all
96
114
  const abc = raw
97
115
  .replace(SCORE_OPEN_BEGIN, '')
98
116
  .replace(SCORE_CLOSE, '')
@@ -100,7 +118,7 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
100
118
  .split('\n')
101
119
  .map((line) => line.trim())
102
120
  .join('\n');
103
- console.log(abc);
121
+
104
122
  return {
105
123
  type: 'abcScore',
106
124
  raw,
@@ -121,7 +139,6 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
121
139
  waitForElement(`#${eleId}`).then((ele: Element) => {
122
140
  abcjs.renderAbc(
123
141
  ele,
124
- // Trim all leading whitespace or ABCjs freaks out and fails to render it all
125
142
  (token as AbcToken).abc,
126
143
  options.abcOptions,
127
144
  );
@@ -136,4 +153,9 @@ export default function(options: MarkedAbcOptions = {}): MarkedExtension {
136
153
  },
137
154
  }],
138
155
  };
156
+
157
+ return {
158
+ extension,
159
+ forceRenderAll,
160
+ };
139
161
  }