47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
/**
|
|
* 토글 선언 줄을 파싱한다.
|
|
* @param {string} line - 토글 선언 줄
|
|
* @returns {{ title: string, defaultOpen: boolean }} 토글 옵션
|
|
*/
|
|
export const parseToggleOpenerLine = (line) => {
|
|
const raw = String(line ?? '').trim().replace(/^:::toggle\s*/, '').trim()
|
|
const tokens = raw.split(/\s+/).filter(Boolean)
|
|
const state = tokens[0]?.toLowerCase()
|
|
const hasStateToken = ['open', 'opened', 'default-open', 'closed', 'close', 'default-closed'].includes(state)
|
|
const defaultOpen = ['open', 'opened', 'default-open'].includes(state)
|
|
const title = (hasStateToken ? tokens.slice(1).join(' ') : raw).trim()
|
|
|
|
return {
|
|
title,
|
|
defaultOpen
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 토글 선언 줄을 만든다.
|
|
* @param {{ title?: string, defaultOpen?: boolean }} options - 옵션
|
|
* @returns {string} 토글 선언 줄
|
|
*/
|
|
export const buildToggleOpenerLine = (options = {}) => {
|
|
const title = String(options.title ?? '').trim() || '더 보기'
|
|
const state = options.defaultOpen ? 'open' : 'closed'
|
|
|
|
return `:::toggle ${state} ${title}`
|
|
}
|
|
|
|
/**
|
|
* 토글 블록 마크다운 줄 배열을 만든다.
|
|
* @param {{ title?: string, body?: string, defaultOpen?: boolean }} options - 옵션
|
|
* @returns {string[]} 마크다운 줄
|
|
*/
|
|
export const buildToggleBlockLines = (options = {}) => {
|
|
const body = String(options.body ?? '').replace(/\r/g, '')
|
|
const bodyLines = body.length ? body.split('\n') : ['']
|
|
|
|
return [
|
|
buildToggleOpenerLine(options),
|
|
...bodyLines,
|
|
':::'
|
|
]
|
|
}
|