# decorateCooked в теме против HighlightJS

**URL:** <https://meta.discourse.org/t/decoratecooked-in-theme-vs-highlighjs/126208>\
**Category:** Product Support\
**Created:** [19.Август.2019 18:21:14 UTC](https://meta.discourse.org/t/decoratecooked-in-theme-vs-highlighjs/126208 "2019-08-19T18:21:14Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![alldritt](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alldritt/32/119516_2.png) [@alldritt](https://meta.discourse.org/u/alldritt)\
**Post date:** [19.Август.2019 19:14:23 UTC](https://meta.discourse.org/t/decoratecooked-in-theme-vs-highlighjs/126208/2 "2019-08-19T19:14:23Z")

</div>

Я нашел обходной путь для этой проблемы с использованием `MutationObserver`, но не уверен, что это лучший подход:

````plaintext
<script type="text/discourse-plugin" version="0.1">
    if (navigator.appVersion.indexOf('Mac') >= 0) { // Делать это только на Mac
        if (typeof myObserver == 'undefined') {
            var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
            var myObserver = new MutationObserver(function (mutations) {
                mutations.forEach(function (mutation) {
                    var target = $(mutation.target);
                    
                    if (target.hasClass('applescript')) {
                        var src = encodeURIComponent(mutation.target.innerText);

                        $('<a class="widget-button btn btn-default" href="sdapplescript://com.apple.scripteditor?action=new&script=' + src + '">Открыть в Script Debugger</a>').insertAfter(target.parent());
                    }
                });
            });
            var obsConfig = { childList: false, characterData: false, attributes: true, subtree: false };
        }

        api.decorateCooked(
            $elem => {
                $elem.find('.lang-applescript, .applescript').filter(function () {
                    return $(this).parents('.d-editor-preview').length < 1; // Не делать это в предпросмотре редактора
                }).each(function(index) {
                    var src = encodeURIComponent(this.innerText);

                    $('<a class="widget-button btn btn-default" href="sdapplescript://com.apple.scripteditor?action=new&script=' + src + '">Открыть в Script Debugger</a>').insertAfter($(this).parent());
                });
                
                // Обработать случай, когда HighlightJS применяется *после* вызова decorateCooked. Это происходит для
                // блоков кода, введенных через отступы, а не через ```.
                $elem.find('code:not(.applescript,.lang-applescript)').filter(function () {
                    return $(this).parents('.d-editor-preview').length < 1; // Не делать это в предпросмотре редактора
                }).each(function () {
                    myObserver.observe(this, obsConfig);
                });
            },
            { id: 'applescript-decorator', onlyStream: true }
        );
    }
</script>

````

---

_[View the full topic](https://meta.discourse.org/t/decoratecooked-in-theme-vs-highlighjs/126208)._
