Is there an easy solution to convert custom markup into different formats, even with nested markings? Examples include...
- for
\k[hello]
, the output is<b>hello</b>
- for
\i[world]
, the output is<em>world</em>
- for
hello \k[dear \i[world]]
, the output ishello <b>dear <em>world</em></b>
- for
\b[some text](url)
, the output is<a href=”url”>some text</a>
- for
\r[some text](url)
, the output will be<img alt=”some text” src=”url” />
Converting the above to javascript, while considering nesting, is surprisingly simple if the markup grammar remains consistent.
//
// Define the syntax and translation to javascript.
//
const grammar = {
syntax: {
k: {markUp: `\k[`, javascript: `"+grammar.oneArg("k","`, pre: `<b>`, post: `</b>`},
i: {markUp: `\i[`, javascript: `"+grammar.oneArg("i","`, pre: `<em>`, post: `</em>`},
b: {markUp: `\b[`, javascript: `"+grammar.twoArgs("b","`, pattern: `<a href="$2">$1</a>`},
r: {markUp: `\r[`, javascript: `"+grammar.twoArgs("r","`, pattern: `<img alt="$1" src="$2"/>`},
close0: {markUp: `](`, javascript: `","`},
close1: {markUp: `)`, javascript: `")+"`},
close2: {markUp: `]`, javascript: `")+"`}
},
oneArg: function( command, arg1 ) {
return grammar.syntax[ command ].pre + arg1 + grammar.syntax[ command ].post;
},
twoArgs: function( command, arg1, arg2 ) {
return grammar.syntax[ command ].pattern.split( `$1` ).join( arg1 ).split( `$2` ).join( arg2 );
}
}
function transpileAndExecute( markUpString ) {
// Convert the markUp to javascript.
for ( command in grammar.syntax ) {
markUpString = markUpString.split( grammar.syntax[ command ].markUp ).join( grammar.syntax[ command ].javascript );
}
// With the markUp now converted to javascript, let's execute it!
return new Function( `return "${markUpString}"` )();
}
var markUpTest = `Hello \k[dear \i[world!]] \b[\i[Search:] \k[Engine 1]](http://www.google.com) \r[\i[Search:] \k[Engine 2]](http://www.yahoo.com)`;
console.log( transpileAndExecute( markUpTest ) );
It's important to address preprocessing issues like handling tokens within normal text. Using '\]' as a representation of ']' before transpiling and replacing occurrences afterward solves this issue easily.
Transpiling large amounts of markup using the defined grammar faces challenges when dealing with syntax errors. Capturing error details from the dynamically compiled javascript function can help identify line numbers and character positions, but mapping these errors back to the original markup poses a challenge.
If extra characters like ']' are misplaced, like after "Goodbye"...
Hello World! \b[\i[Goodbye]]] \k[World!]]
This results in the transpiled version...
"Hello World! "+grammar.twoArgs("b",""+grammar.oneArg("i","Goodbye")+"")+"")+" "+grammar.oneArg("k","World!")+"")+""
^
The goal is to map syntax errors back to the original markup efficiently. Any suggestions on making this process simpler would be appreciated. Thank you.