From e981089130010e240e25fddc0fe0d4a024e231b0 Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Wed, 11 Jan 2023 13:30:10 +0000 Subject: [PATCH] resolves #1220 super.format (#1682) resolves https://github.com/asciidoctor/asciidoctor.js/issues/1220 --- .../core/spec/graalvm/asciidoctor-convert.mjs | 8 +++--- packages/core/spec/share/asciidoctor-spec.cjs | 28 +++++++++++++++++++ packages/core/src/asciidoctor-core-api.js | 18 ++++++++++-- packages/core/types/.eslintrc.js | 8 +++--- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/packages/core/spec/graalvm/asciidoctor-convert.mjs b/packages/core/spec/graalvm/asciidoctor-convert.mjs index 993f4368f..3ee3ee926 100644 --- a/packages/core/spec/graalvm/asciidoctor-convert.mjs +++ b/packages/core/spec/graalvm/asciidoctor-convert.mjs @@ -1,8 +1,8 @@ /* global Asciidoctor */ import Asciidoctor from '../../build/asciidoctor-graalvm.js' -var asciidoctor = Asciidoctor() +const asciidoctor = Asciidoctor() -var data = '= asciidoctor.js, AsciiDoc in JavaScript\n' + +const data = '= asciidoctor.js, AsciiDoc in JavaScript\n' + 'Doc Writer \n\n' + 'Asciidoctor and Opal come together to bring\n' + 'http://asciidoc.org[AsciiDoc] to the browser!.\n\n' + @@ -13,6 +13,6 @@ var data = '= asciidoctor.js, AsciiDoc in JavaScript\n' + 'NOTE: That\'s all she wrote!!!\n\n' + 'include::spec/fixtures/include.adoc[]' -var options = { safe: 'server', header_footer: true, attributes: { showtitle: true } } -var html = asciidoctor.convert(data, options) +const options = { safe: 'server', header_footer: true, attributes: { showtitle: true } } +const html = asciidoctor.convert(data, options) console.log(html) diff --git a/packages/core/spec/share/asciidoctor-spec.cjs b/packages/core/spec/share/asciidoctor-spec.cjs index 429c6f65b..b3535ac52 100644 --- a/packages/core/spec/share/asciidoctor-spec.cjs +++ b/packages/core/spec/share/asciidoctor-spec.cjs @@ -2059,6 +2059,34 @@ America/New_York expect(html).to.include('
Europe/London\nAmerica/New_York
') expect(html).to.include('') }) + + it('should call super.format', () => { + class BuildHighlighter { + format (node, lang, opts) { + opts = Object.assign({}, opts, { + transform: (x, code) => { + code['$[]=']('class', `language-${lang || 'none'} hljs`) + } + }) + return this.super.format(node, lang, opts) + } + + handlesHighlighting () { return true } + + highlight (node, source, lang, opts) { + return `${source}${lang}` + } + } + + asciidoctor.SyntaxHighlighter.register('build', BuildHighlighter) + const source = `[source,ruby] +---- +puts 'Hello, World!' +----` + const doc = asciidoctor.load(source, { attributes: { 'source-highlighter': 'build' } }) + const html = doc.convert({ standalone: false }) + expect(html).to.include('
puts \'Hello, World!\'ruby
') + }) }) describe('Table', function () { diff --git a/packages/core/src/asciidoctor-core-api.js b/packages/core/src/asciidoctor-core-api.js index 2dc6e5efd..390dbb55c 100644 --- a/packages/core/src/asciidoctor-core-api.js +++ b/packages/core/src/asciidoctor-core-api.js @@ -267,7 +267,8 @@ function initializeClass (superClass, className, functions, defaultFunctions, ar } } Opal.def(scope, '$initialize', initialize) - Opal.def(scope, 'super', function (func) { + let $superFunction + Opal.def(scope, 'super', ($superFunction = function (func) { if (typeof func === 'function') { Opal.send(this, Opal.find_super_dispatcher(this, func.name, func)) } else { @@ -281,7 +282,20 @@ function initializeClass (superClass, className, functions, defaultFunctions, ar } Opal.send(this, Opal.find_super_dispatcher(this, 'initialize', initialize), argumentsList) } - }) + })) + for (const functionName in functions) { + $superFunction[functionName] = function () { + const argumentsList = Array.from(arguments) + for (let i = 0; i < argumentsList.length; i++) { + // convert all (Opal) Hash arguments to JSON. + if (typeof argumentsList[i] === 'object' && typeof argumentsList[i].constructor === 'function' && argumentsList[i].constructor.name === 'Object') { + argumentsList[i] = toHash(argumentsList[i]) + } + } + const self = scope.$$prototype + return Opal.send(self, Opal.find_super_dispatcher(self, functionName, self[`$${functionName}`]), argumentsList) + } + } if (defaultFunctions) { for (const defaultFunctionName in defaultFunctions) { if (Object.prototype.hasOwnProperty.call(defaultFunctions, defaultFunctionName) && !Object.prototype.hasOwnProperty.call(defaultFunctionsOverridden, defaultFunctionName)) { diff --git a/packages/core/types/.eslintrc.js b/packages/core/types/.eslintrc.js index a2076a8be..cb235c8d2 100644 --- a/packages/core/types/.eslintrc.js +++ b/packages/core/types/.eslintrc.js @@ -2,11 +2,11 @@ module.exports = { root: true, parser: '@typescript-eslint/parser', plugins: [ - '@typescript-eslint', + '@typescript-eslint' ], extends: [ 'eslint:recommended', - 'plugin:@typescript-eslint/recommended', + 'plugin:@typescript-eslint/recommended' ], overrides: [ { @@ -17,8 +17,8 @@ module.exports = { '@typescript-eslint/no-this-alias': [ 'error', { - 'allowDestructuring': false, - 'allowedNames': ['self'] + allowDestructuring: false, + allowedNames: ['self'] } ] }