mapbox / delaunator

An incredibly fast JavaScript library for Delaunay triangulation of 2D points
https://mapbox.github.io/delaunator/
ISC License
2.33k stars 142 forks source link

Improvements to the data structures guide #30

Closed mourner closed 4 years ago

mourner commented 6 years ago

@redblobgames I'm wondering whether we could drop the script { display: block; } hack — while it's definitely cool, it has some drawbacks:

redblobgames commented 6 years ago

Tradeoffs :-)

I agree, the presentation and tooling would be better without this hack. The main reason I used it is that in some of my previous projects, I had duplicate code, one version that runs and one version that is displayed on the page. The main risk was that the version that's displayed (untested) can diverge from the version that runs (tested), and readers get the wrong version of the code.

Alternatives I considered for handling the duplication between the displayed code and the running code:

Do you have a preference?

redblobgames commented 6 years ago

I looked at highlight.js and I can make it work on this page by running it on each of the <script> elements:

        document.querySelectorAll('main script:not([src])').forEach(element => {
            let sibling = document.createElement('pre');
            sibling.innerHTML = hljs.highlight('js', element.textContent, false, null).value;
            element.parentNode.insertBefore(sibling, element);
        });

However it doesn't solve lint or refactoring.

mourner commented 6 years ago

@redblobgames one more approach that I think would be relatively simple and keep all the benefits is to add a custom linting script that matches all the code in pre blocks against the real code and fails the tests on a mismatch.

Also forgot to mention another potential drawback of the script hack — I have a suspicion that this code won't be indexed by search engines, although no proof of that.