A single page application (if that’s what SPA stands for) in a DT document – while I can see that you might find that useful, I doubt that there’s much enthusiasm in the DT user community for it. Not least because JavaScript is not very popular here.
In principle, there’s no problem having a script element in an MD document whose content is executed when the MD is shown in preview mode – what you see is HTML and WebKit will run the script code. This currently does not work in DTTG because there’s a bug in the new MD parser. I have good reason to believe that the developers will fix that bug. So, in an MD document you can put whatever code you want to be run when the MD is rendered as HTML.
And of course, this is run in a sandbox. In a user agent, aka browser. So no, no escaping it and doing something to other documents. Same as in the browser – you can’t manipulate another document from your current one (unless something is broken in the browser).
DT (not DTTG) though offers the possibility to work with any of its documents (and those of other, scriptable apps) through OSA, using AppleScript or JavaScript. But that is a completely different thing from what happens when you run a script in a user agent. A lot less secure, too.
Code running in an HTML document is interpreted by the user agent. And limited to whatever functionality this agent provides. Writing anything to the local filesystem is not part of that functionality, AFAIK. If you want that, someone would have to write a new user agent with its own JS engine that does what you want. Opening a huge security hole – someone sends you an MD file with a script element uploading your private documents to example.com.
I for one would be very much against that kind of “feature”.
They use Apple’s framework for the HTML rendering. So they are limited to what these frameworks offer. I detailed the limitations above. They’d have to rewrite everything.
Yes, there are apps providing JS APIs, notably Drafts (I haven’t looked at other ones). But Drafts is not about HTML, it just runs JS code using its own JS APIs. And it doesn’t use “documents” in the traditional sense, everything is stored in an SQLite database.