diff --git a/jest.config.js b/jest.config.js index b9f1ee56c..4c295a7c5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -18,4 +18,5 @@ module.exports = { }, testEnvironment: 'jest-environment-jsdom', setupFiles: ['./tests/unit/config.js'], + transformIgnorePatterns: ['/node_modules/(?!@apple/highlightjs-pkl)'], }; diff --git a/package-lock.json b/package-lock.json index bf2f994b8..67d534383 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "swift-docc-render", "version": "0.1.0", "dependencies": { + "@apple/highlightjs-pkl": "^1.0.1", "core-js": "^3.8.2", "css.escape": "^1.5.1", "highlight.js": "^11.3.1", @@ -81,6 +82,15 @@ "node": ">=6.0.0" } }, + "node_modules/@apple/highlightjs-pkl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@apple/highlightjs-pkl/-/highlightjs-pkl-1.0.1.tgz", + "integrity": "sha512-AYoR/X1W5HlT9elhCjB6cQ6Zs0rvT14o4SJRw2M8lCBP6ceK9LgbHaMRxydikU2Xjmzt0XEYCPzS63+kar35xg==", + "license": "Apache-2.0", + "dependencies": { + "highlight.js": "^11.11.1" + } + }, "node_modules/@babel/code-frame": { "version": "7.27.1", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz", diff --git a/package.json b/package.json index 3a6cf4902..080f202b1 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "webpack-asset-path.js" ], "dependencies": { + "@apple/highlightjs-pkl": "^1.0.1", "core-js": "^3.8.2", "css.escape": "^1.5.1", "highlight.js": "^11.3.1", diff --git a/src/utils/custom-highlight-lang/pkl.js b/src/utils/custom-highlight-lang/pkl.js new file mode 100644 index 000000000..9843235d1 --- /dev/null +++ b/src/utils/custom-highlight-lang/pkl.js @@ -0,0 +1,13 @@ +/** + * This source file is part of the Swift.org open source project + * + * Copyright (c) 2025 Apple Inc. and the Swift project authors + * Licensed under Apache License v2.0 with Runtime Library Exception + * + * See https://swift.org/LICENSE.txt for license information + * See https://swift.org/CONTRIBUTORS.txt for Swift project authors +*/ + +import pkl from '@apple/highlightjs-pkl'; + +export default pkl; diff --git a/src/utils/syntax-highlight.js b/src/utils/syntax-highlight.js index 924436c92..6ec3cec9f 100644 --- a/src/utils/syntax-highlight.js +++ b/src/utils/syntax-highlight.js @@ -31,6 +31,7 @@ const Languages = { objectivec: ['mm', 'objc', 'obj-c'].concat(CustomLanguageAliases.objectivec), perl: ['pl', 'pm'], php: [], + pkl: [], python: ['py', 'gyp', 'ipython'], ruby: ['rb', 'gemspec', 'podspec', 'thor', 'irb'], scss: [], @@ -50,6 +51,7 @@ const Languages = { export const CustomLanguagesSet = new Set([ 'markdown', 'swift', + 'pkl', ]); export const LanguageAliasEntries = Object.entries(Languages); diff --git a/tests/unit/utils/syntax-highlight.spec.js b/tests/unit/utils/syntax-highlight.spec.js index c4d0dd4ba..fc70fd15d 100644 --- a/tests/unit/utils/syntax-highlight.spec.js +++ b/tests/unit/utils/syntax-highlight.spec.js @@ -286,6 +286,19 @@ describe("syntax-highlight", () => { `); }); + it("tokenizes pkl code", async () => { + const content = [ + "foo = 1", + "bar = \"Hello \\(foo)\"", + ]; + const { highlightedCode } = await prepare(content, "pkl"); + expect(highlightedCode).toMatchInlineSnapshot(` + foo = 1 + bar = "Hello \\(foo)" + ` + ); + }); + it("returns false if the language is not supported", async () => { expect(await registerHighlightLanguage("pascal")).toBe(false); });