Starlight | Power the code block
โExpressive Codeโ๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๊ณต์ ๋ ํฌ์์ ๋ค์๊ณผ ๊ฐ์ด ์ค๋ช ํ๋ค:
Expressive Code is an engine for presenting source code on the web, aiming to make your code easy to understand and visually stunning.
์ฆ, ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฝ๋ ๋ธ๋ก์ ๋ ๋งค๋ ฅ์ ์ผ๋ก ๋ง๋๋ ์ถ๊ฐ์ ์ธ ์คํ์ผ๋ง๊ณผ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. Next.js์ Astro์ ๊ฐ์ด Rehype ํ๋ฌ๊ทธ์ธ์ ์ง์ํ๋ ๋ค์ํ ํ๋ ์์ํฌ์์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, Astro๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ Starlight์์๋ ๊ธฐ๋ณธ ์ฝ๋ ๋ธ๋ก ๋ ๋๋ง ์์ง์ผ๋ก ์ฌ์ฉํ๋ค.
Expressive Code๋ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง๋ง, ๊ธฐ๋ณธ ์ํ์์๋ ์๋ ๋ ๊ฐ์ง ๊ณต์ ์ถ๊ฐ ๊ธฐ๋ฅ(ํ๋ฌ๊ทธ์ธ)์ด ์๋ค. ์ด ๊ธ์์๋ Starlight์์ ์ด ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค.
๋ ํ๋ฌ๊ทธ์ธ์ ๋ค์๊ณผ ๊ฐ๋ค:
- Collapsible Sections (์ค ๋ฒ์ ์ถ์)
: ๊ธด ์ฝ๋ ์์ ์์ ํน์ ์ค ๋ฒ์๋ฅผ ์ถ์ํ์ฌ, ํต์ฌ ๋ถ๋ถ์ ์ง์คํ๋๋ก ๋๋ ๊ธฐ๋ฅ - Line Numbers (์ค ๋ฒํธ ํ์)
: ์ฝ๋ ๋ธ๋ก์ ์ค ๋ฒํธ๋ฅผ ํ์ํ์ฌ, ์ฝ๋์ ํน์ ์์น๋ฅผ ์ฝ๊ฒ ์ฐธ์กฐํ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฅ
์ฌ์ฉ ๋ชจ์ต ์ฐธ๊ณ
Section titled โ์ฌ์ฉ ๋ชจ์ต ์ฐธ๊ณ โ์ด๋ค ๋ชจ์ต์ธ์ง๋ถํฐ ๋ณด๋ฉด ๋ ์ง๊ด์ ์ผ๋ก ๊ฐ ํ๋ฌ๊ทธ์ธ์ ์์๋ฅผ ์ดํดํ ์ ์๋ค.
์๋์ ๊ณต์ ๋ฌธ์์์ ์์๋ฅผ ํ์ธํ ์ ์๋ค.
ํ๋ฌ๊ทธ์ธ์ ํ์ฑํํ๋ ๋ฐฉ๋ฒ์ ์์ ํ๋ค.
โExpressive Codeโ์ ๊ณต์ ๋ฌธ์์ ๋ฌ๋ฆฌ, ๋ ํ๋ฌ๊ทธ์ธ์ ๋ถ๋ฆฌํ์ง ์๊ณ ๋ชจ๋ ์ค์นํ๋ ์ํฉ์ผ๋ก ๊ฐ์ ํ๋ค.
๋ํ, Starlight์์๋ Expressive Code๊ฐ ์ด๋ฏธ ์ค์น๋์ด ์์์ ๋ฐ๋ผ ์ถ๊ฐ ํ๋ฌ๊ทธ์ธ ์ค์น๋ก ๋ฐ๋ก ๋์ด๊ฐ๋ค.
npm i @expressive-code/plugin-collapsible-sectionsnpm i @expressive-code/plugin-line-numbers์ค์น ํ ec.config.mjs ํ์ผ์ ์์ฑํด ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ๋ค:
import { pluginCollapsibleSections } from '@expressive-code/plugin-collapsible-sections'import { pluginLineNumbers } from '@expressive-code/plugin-line-numbers'
/** @type {import('@astrojs/starlight/expressive-code').StarlightExpressiveCodeOptions} */export default { plugins: [pluginCollapsibleSections(), pluginLineNumbers()],10 collapsed lines
defaultProps: { // Disable line numbers by default showLineNumbers: false, // But enable line numbers for certain languages overridesByLang: { 'js,ts,html,python': { showLineNumbers: true, }, }, },}7~16๋ฒ ์ค์ ์ถ๊ฐ์ ์ธ ํ๋ฌ๊ทธ์ธ ์ค์ ๋ถ๋ถ์ผ๋ก, ์ ํ ์ฌํญ์ด๋ค. ์ ์์์์๋ ์ฝ๋ ๋ธ๋ก ์์ฑ ์ ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ค ๋ฒํธ๋ฅผ ํ์ํ์ง ์์ง๋ง, JavaScript, TypeScript, HTML, Python ์ธ์ด๋ฅผ ํ์ํ๋ ์ฝ๋ ๋ธ๋ก์ ๋ํด์๋ ์ค ๋ฒํธ๋ฅผ ํ์ํ๋๋ก ์ค์ ํ๋ค.
์ฌ์ฉ ์์
Section titled โ์ฌ์ฉ ์์โ์๋๋ Starlight์์ ์ฝ๋ ๋ธ๋ก์ ๋ ๋๋งํ ๋, ์์์ ์ค์ ํ ํ๋ฌ๊ทธ์ธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ์์์ด๋ค.
๋ ์์ธํ ์ฌ์ฉ๋ฒ์ Expressive Code ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋ค.
์์ 1
Section titled โ์์ 1โ```js showLineNumbers=true collapse={7-8} collapseStyle="collapsible-start"function exampleFunction() { console.log("This is an example function."); โฎ}```function exampleFunction() { console.log("This is an example function."); console.log("It has multiple lines of code."); console.log("Some lines can be collapsed."); console.log("This is the last line before collapse.");2 collapsed lines
// This line will be collapsed console.log("This line is hidden by default.");}์์ 2
Section titled โ์์ 2โ```js showLineNumbers=false collapse={6-7} collapseStyle="github"function anotherExampleFunction() { console.log("This is another example function."); โฎ}```function anotherExampleFunction() { console.log("This is another example function."); // The two lines under are collapsed until expanded // ("github" style, irreversible)2 collapsed lines
console.log("It also has multiple lines of code."); console.log("This line is hidden by default.");}