Skip to content

Commit 2293ba9

Browse files
committed
Make prism/hljs templates classes; keep but deprecate their functions
1 parent 0d32117 commit 2293ba9

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

code-input.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ export namespace templates {
374374
*/
375375
constructor(prism: Object, plugins?: Plugin[])
376376
}
377+
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation
377378
/**
378379
* @deprecated Please use `new codeInput.templates.Prism(...)`
379380
*/
380381
function prism(prism: Object, plugins?: Plugin[]): Template
381-
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation
382382
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation
383383
/**
384384
* A template that uses highlight.js syntax highlighting (https://highlightjs.org/).
@@ -392,11 +392,11 @@ export namespace templates {
392392
*/
393393
constructor(hljs: Object, plugins?: Plugin[])
394394
}
395+
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation
395396
/**
396397
* @deprecated Please use `new codeInput.templates.Hljs(...)`
397398
*/
398399
function hljs(hljs: Object, plugins?: Plugin[]): Template
399-
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation
400400
/**
401401
* Constructor to create a proof-of-concept template that gives a message if too many characters are typed.
402402
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`

code-input.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,27 @@ var codeInput = {
232232
return new codeInput.templates.Prism(prism, plugins);
233233
},
234234
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
235+
/**
236+
* @deprecated Please use `new codeInput.templates.Prism(...)`
237+
*/
238+
prism(prism, plugins = []) { // Dependency: Prism.js (https://prismjs.com/)
239+
return new codeInput.templates.Prism(prism, plugins);
240+
},
235241

236242
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
237243
/**
238244
* @deprecated Please use `new codeInput.templates.Hljs(...)`
239245
*/
240246
hljs(hljs, plugins = []) { // Dependency: Highlight.js (https://highlightjs.org/)
241-
return new codeInput.templates.Hljs(hljs, plugins);
247+
return new codeInput.templates.Hljs(hljs, plugins)
242248
},
243249
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
250+
/**
251+
* @deprecated Please use `new codeInput.templates.Hljs(...)`
252+
*/
253+
hljs(hljs, plugins = []) { // Dependency: Highlight.js (https://highlightjs.org/)
254+
return new codeInput.templates.Hljs(hljs, plugins);
255+
},
244256

245257
/**
246258
* @deprecated Make your own version of this template if you need it - we think it isn't widely used so will remove it from the next version of code-input.

esm/generate.mjs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,18 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
9797
await templateMjs.writeFile("const templates = {\n");
9898
// Code after start and before end of this template, making use of the imported Template, not codeInput.Template
9999
let copyingCode = false;
100+
let templateClassName = null;
100101
for await (const line of codeInputJs.readLines()) {
101102
if(line.includes("ESM-SUPPORT-END-TEMPLATE-"+templateName)) {
102103
break;
103104
}
104105
if(copyingCode) {
106+
if(templateClassName === null) {
107+
const templateClassNameThisLine = line.match(/[A-Za-z]+(?=: class extends codeInput.Template)/);
108+
if(templateClassNameThisLine !== null && templateClassNameThisLine.length > 0) {
109+
templateClassName = templateClassNameThisLine;
110+
}
111+
}
105112
await templateMjs.writeFile(line.replaceAll("codeInput.Template", "Template")+"\n");
106113
}
107114
if(line.includes("ESM-SUPPORT-START-TEMPLATE-"+templateName)) {
@@ -110,7 +117,7 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
110117
}
111118
await templateMjs.writeFile("};\n");
112119
// Export, assuming the name of the function is the same as the name of the file.
113-
await templateMjs.writeFile("export default templates."+templateName+";\n");
120+
await templateMjs.writeFile("export default templates."+templateClassName+";\n");
114121
await codeInputJs.close();
115122
await templateMjs.close();
116123
});
@@ -124,16 +131,16 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
124131
await templateDMts.writeFile("import { Template, Plugin } from \"../code-input.d.mts\";\n");
125132
// Code after start and before end of this template, making use of the imported Template, not codeInput.Template
126133
let copyingCode = false;
127-
let functionSeen = false;
134+
let classSeen = false;
128135
for await (let line of codeInputDTs.readLines()) {
129136
if(line.includes("ESM-SUPPORT-END-TEMPLATE-"+templateName)) {
130137
break;
131138
}
132139
if(copyingCode) {
133-
if(/( |\t)*function.*/.test(line) && !functionSeen) {
140+
if(/( |\t)*class.*/.test(line) && !classSeen) {
134141
// Replace only first occurrence
135-
line = line.replace("function", "export default function");
136-
functionSeen = true;
142+
line = line.replace("class", "export default class");
143+
classSeen = true;
137144
}
138145
await templateDMts.writeFile(line.replaceAll("codeInput.Template", "Template").replaceAll("codeInput.Plugin", "Plugin")+"\n");
139146
}

esm/generate.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ mkdir -p templates
4545
# Code after start and before end of this template, making use of the imported Template, not codeInput.Template
4646
head -$(($(sed -n "/ESM-SUPPORT-END-TEMPLATE-$0/=" ../code-input.js | head -1) - 1)) ../code-input.js | tail --line=+$(($(sed -n "/ESM-SUPPORT-START-TEMPLATE-$0/=" ../code-input.js | head -1) + 1)) | sed "s/codeInput\.Template/Template/g" >> templates/$0.mjs
4747
echo "};" >> templates/$0.mjs
48-
# Export, assuming the name of the function is the same as the name of the file.
49-
echo "export default templates.$0;" >> templates/$0.mjs
48+
49+
TEMPLATE_CLASS_NAME="$(grep -Eo "[a-zA-Z]+: class extends Template" templates/$0.mjs | head -1 | sed "s/: class extends Template//")";
50+
# Export.
51+
echo "export default templates.$TEMPLATE_CLASS_NAME;" >> templates/$0.mjs
5052
5153
# $0 is the template name, $1 is the autogenerated notice
5254
' "%" "$AUTOGENERATED_NOTICE"
@@ -57,9 +59,9 @@ mkdir -p templates
5759
echo "" >> templates/$0.d.mts;
5860
# Imports
5961
echo "import { Template, Plugin } from \"../code-input.d.mts\";" >> templates/$0.d.mts
60-
# Code after start and before end of this template, making use of the imported Template, not codeInput.Template, and the imported Plugin, not codeInput.Plugin, exporting the function as default
61-
# export default function replacement should work but won"t leave indentation as JS version does.
62-
head -$(($(sed -n "/ESM-SUPPORT-END-TEMPLATE-$0/=" ../code-input.d.ts | head -1) - 1)) ../code-input.d.ts | tail --line=+$(($(sed -n "/ESM-SUPPORT-START-TEMPLATE-$0/=" ../code-input.d.ts | head -1) + 1)) | sed "s/codeInput\.Template/Template/g" | sed "s/codeInput\.Plugin/Plugin/g" | sed -E "s/^[[:space:]]*function /export default function /" >> templates/$0.d.mts
62+
# Code after start and before end of this template, making use of the imported Template, not codeInput.Template, and the imported Plugin, not codeInput.Plugin, exporting the class as default
63+
# export default class replacement should work but won"t leave indentation as JS version does.
64+
head -$(($(sed -n "/ESM-SUPPORT-END-TEMPLATE-$0/=" ../code-input.d.ts | head -1) - 1)) ../code-input.d.ts | tail --line=+$(($(sed -n "/ESM-SUPPORT-START-TEMPLATE-$0/=" ../code-input.d.ts | head -1) + 1)) | sed "s/codeInput\.Template/Template/g" | sed "s/codeInput\.Plugin/Plugin/g" | sed -E "s/^[[:space:]]*class /export default class /" >> templates/$0.d.mts
6365
6466
# $0 is the template name, $1 is the autogenerated notice
6567
' "%" "$AUTOGENERATED_NOTICE"

0 commit comments

Comments
 (0)