본문으로 건너뛰기

시작하기

설치

셸
composer require shikiphp/shikiphp

shikiphp에는 PHP 8.2 이상과 ext-json 및 ext-mbstring이 필요합니다. 이 둘은 거의 모든 PHP 빌드에서 활성화되어 있습니다. 그 외에는 Node, 네이티브 Oniguruma 바인딩, 추가 확장이 필요 없습니다.

첫 코드 블록

정적 Shikiphp 파사드가 가장 빠른 시작 방법입니다. 코드와 언어 ID, 테마 ID를 전달하세요:

PHP
use Shikiphp\Shikiphp;

$html = Shikiphp::codeToHtml(<<<'PHP'
<?php
function greet(string $name): string {
    return "Hello, {$name}!";
}
PHP, [
    'lang'  => 'php',
    'theme' => 'github-dark',
]);

echo $html;

codeToHtml는 스타일이 완성된 <pre class="shiki …"> 블록을 반환합니다. 전경색과 배경색이 <pre>에 인라인으로 지정되므로 별도의 스타일시트 없이도 올바르게 렌더링됩니다.

이중 테마(라이트 및 다크)

단일 theme 대신 themes 맵을 전달하세요. shikiphp는 CSS 변수를 출력합니다(Shiki 등). 따라서 렌더링된 코드 블록 하나가 라이트 및 다크 모드에 맞게 바뀝니다:

PHP
use Shikiphp\Shikiphp;

$code = 'const answer: number = 42';

echo Shikiphp::codeToHtml($code, [
    'lang'   => 'ts',
    'themes' => ['light' => 'github-light', 'dark' => 'github-dark'],
]);

기본적으로 light 테마의 색상이 인라인 기본값이 되고, dark은 --shiki-dark 변수로 노출됩니다. 간단한 CSS를 사용해 다크 테마를 활성화하세요. 문서의 동일한 코드 조각 Shiki를 참고하면 됩니다:

CSS
html.dark .shiki,
html.dark .shiki span {
  color: var(--shiki-dark) !important;
  background-color: var(--shiki-dark-bg) !important;
  /* and font styles */
  font-style: var(--shiki-dark-font-style) !important;
  font-weight: var(--shiki-dark-font-weight) !important;
  text-decoration: var(--shiki-dark-text-decoration) !important;
}

defaultColor 옵션으로 인라인 기본 테마를 선택하거나, defaultColor: false를 설정해 변수만 출력할 수 있습니다.

언어 ID 및 별칭

언어는 Shiki ID 또는 별칭(php, ts, js, rust, python, html 등)으로 참조합니다. 번들에 포함된 전체 목록을 확인하는 방법은 언어를, 테마 ID는 테마를 참조하세요.

하이라이터 재사용

파사드는 번들 Highlighter를 지연 생성하고 캐시하므로 Shikiphp::codeToHtml를 반복 호출해도 동일한 문법 및 테마 캐시를 재사용합니다. HAST 출력, 사용자 지정 문법, 토큰 그리드 등 더 다양한 기능을 제공하는 API가 필요하면 인스턴스를 직접 가져오세요:

PHP
use Shikiphp\Shikiphp;

$code = 'fn main() {}';

$highlighter = Shikiphp::highlighter();
$hast = $highlighter->codeToHast($code, ['lang' => 'rust', 'theme' => 'nord']);

전체 기능은 API를 참조하세요.