66// ASCIIMathTeXImg.js is loaded as a plain <script> tag by the PHP (sloppy mode).
77// It sets window.AMparseMath before init() is called.
88
9- import markdownit from './markdownit.js' ;
10- import markdownitSub from './markdownitextensions/sub.js' ;
11- import asciimathBlock from './markdownitextensions/asciimathblock.js' ;
12- import markdownitrules from './markdownitrules.js' ;
9+ import markdown from './filters/markdown.js' ;
10+ import calculation from './filters/calculation.js' ;
1311
14- // tex.js uses named CJS exports (exports.tex = ...) — import the whole namespace.
15- import * as mdItPluginTex from './markdownitextensions/tex.js' ;
12+ const filterlib = { markdown, calculation } ;
1613
1714import finalfunction from './extractors/finalfunction.js' ;
1815import lastexpr from './extractors/lastexpr.js' ;
1916import lastblock from './extractors/lastblock.js' ;
17+ import lastcalc from './extractors/lastcalc.js' ;
2018import regexmatch from './extractors/regexmatch.js' ;
2119import regexall from './extractors/regexall.js' ;
2220
23- const extractorlib = { finalfunction, lastexpr, lastblock, regexmatch, regexall } ;
21+ const extractorlib = { finalfunction, lastexpr, lastblock, lastcalc , regexmatch, regexall } ;
2422
2523export default function init ( inputIds , operations ) {
2624 const markdownContainerId = inputIds [ 0 ] ;
2725 // inputIds[1..N] correspond to each parsed answer entry in order.
2826 const alloperations = operations ;
2927 const filters = operations . filter ( operator => operator . operation === 'filter' ) ;
3028 const extractors = operations . filter ( operator => operator . operation === 'extractor' ) ;
31- const markdownitinfo = filters . find ( operator => operator . type === 'markdownit' ) ;
32- const inputTransforms = markdownitinfo ? markdownitinfo . transforms : 'latexwrap,boldfilter' ;
33-
3429 const blockCollector = { blocks : [ ] } ;
3530
36- // mdItPluginTex.tex must come before markdownitrules.
37- const previewMarkdownConverter = markdownit ( { html : true } )
38- . use ( markdownitSub )
39- . use ( mdItPluginTex . tex , { render : ( content ) => content , delimiters : 'brackets' } )
40- . use ( asciimathBlock )
41- . use ( markdownitrules , { transforms : inputTransforms , collector : blockCollector } ) ;
42-
43- function convertMarkdown ( markdown ) {
44- const html = previewMarkdownConverter . render ( markdown ) ;
45- document . getElementById ( 'asciiContainerRow' ) . innerHTML = html ;
46- }
47-
4831 function renderMath ( ) {
4932 const raw = document . getElementById ( markdownContainerId ) . value . trim ( ) ;
5033 const output = document . getElementById ( 'asciiContainerRow' ) ;
@@ -53,24 +36,46 @@ export default function init(inputIds, operations) {
5336 return ;
5437 }
5538
56- convertMarkdown ( raw ) ;
39+ let processedOutput = raw ;
40+ let displayfixed = false ;
41+ let answerIndex = 1 ;
42+
43+ if ( alloperations ) {
44+ alloperations . forEach ( ( currentop , i ) => {
45+ if ( currentop . operation === 'filter' ) {
46+ const filter = filterlib [ currentop . type ] ;
47+ if ( filter ) {
48+ let filterInput = processedOutput ;
49+ if ( currentop . reset === 'true' ) {
50+ filterInput = raw ;
51+ }
52+ const filterOutput = filter ( filterInput , blockCollector , currentop ) ;
53+ if ( ! displayfixed ) {
54+ processedOutput = filterOutput ;
55+ }
56+ if ( currentop . display === 'true' ) {
57+ displayfixed = true ;
58+ }
59+ }
60+ } else if ( currentop . operation === 'extractor' ) {
61+ const extractor = ( extractorlib [ currentop . type ] ) ? extractorlib [ currentop . type ] : extractorlib [ 'lastexpr' ] ;
62+ const answerEl = document . getElementById ( inputIds [ answerIndex ] ) ;
63+ answerIndex ++ ;
64+ if ( extractor && answerEl ) {
65+ extractor ( raw , answerEl , blockCollector . blocks , currentop ) ;
66+ }
67+ }
68+ } ) ;
69+ }
70+
71+ document . getElementById ( 'asciiContainerRow' ) . innerHTML = processedOutput ;
5772
5873 // Tell MathJax to typeset only this element.
5974 if ( typeof MathJax . typesetPromise === 'function' ) {
6075 MathJax . typesetPromise ( [ output ] ) ; // MathJax 3
6176 } else {
6277 MathJax . Hub . Queue ( [ "Typeset" , MathJax . Hub , 'asciiContainerRow' ] ) ; // MathJax 2
6378 }
64-
65- if ( extractors ) {
66- extractors . forEach ( ( entry , i ) => {
67- const extractor = ( extractorlib [ entry . type ] ) ? extractorlib [ entry . type ] : extractorlib [ 'lastexpr' ] ;
68- const answerEl = document . getElementById ( inputIds [ 1 + i ] ) ;
69- if ( extractor && answerEl ) {
70- extractor ( raw , answerEl , blockCollector . blocks , entry ) ;
71- }
72- } ) ;
73- }
7479 }
7580
7681 let debounceTimer ;
0 commit comments