'use strict'; /* 달력 연동 안내. 글은 관리자 페이지 '연동 안내'에서 고친 홈페이지 설정(api/site)을 그대로 그린다. * 모든 글은 textContent로 넣어 HTML로 해석되지 않는다. 꾸밈은 **굵게**와 https:// 링크 및 번호·점 목록만 받는다. */ (function () { const $ = id => document.getElementById(id); function el(tag, text, className) { const node = document.createElement(tag); if (text !== undefined && text !== null) node.textContent = String(text); if (className) node.className = className; return node; } // 한 줄 안의 **굵게**와 https 주소만 꾸민다. 주소 끝의 마침표와 괄호는 링크에서 뺀다. function richLine(parent, text) { for (const part of String(text).split(/(\*\*[^*]+\*\*|https:\/\/[^\s<>"']+)/g)) { if (!part) continue; if (/^\*\*[^*]+\*\*$/.test(part)) parent.append(el('strong', part.slice(2, -2))); else if (/^https:\/\//.test(part)) { const tail = (/[.,)\]]+$/.exec(part) || [''])[0]; const url = tail ? part.slice(0, -tail.length) : part; const link = el('a', url.replace(/^https:\/\//, '')); link.href = url; link.target = '_blank'; link.rel = 'noopener noreferrer'; parent.append(link); if (tail) parent.append(tail); } else parent.append(part); } } function renderText(container, text) { container.replaceChildren(); for (const block of String(text || '').replace(/\r\n/g, '\n').split(/\n\s*\n/)) { let list = null, kind = null; for (const raw of block.split('\n')) { const line = raw.trim(); if (!line) continue; const number = /^\d+\.\s+/.exec(line), dash = /^-\s+/.exec(line); const want = number ? 'ol' : dash ? 'ul' : null; if (want) { if (kind !== want) { list = el(want); container.append(list); kind = want; } const item = el('li'); richLine(item, line.slice((number || dash)[0].length)); list.append(item); } else { kind = null; list = null; const paragraph = el('p'); richLine(paragraph, line); container.append(paragraph); } } } } function render(settings) { if (!settings) return; const sections = $('guide-sections'), toc = $('guide-toc'); sections.replaceChildren(); toc.replaceChildren(); if (settings.guideOn === false) { $('guide-title').textContent = '달력 연동 안내'; $('guide-intro').replaceChildren(el('p', '연동 안내를 준비하고 있습니다. 조금 뒤에 다시 들러 주세요.')); return; } $('guide-title').textContent = settings.guideTitle || '휴대폰 달력과 연동하기'; renderText($('guide-intro'), settings.guideIntro); // 순서는 서버 설정 목록의 순서를 따른다. 관리자에서 끈 안내는 빼고 그린다. const keys = Object.keys(settings).map(key => /^guide_(.+)_title$/.exec(key)).filter(Boolean).map(match => match[1]); let number = 0; for (const key of keys) { if (settings['guide_' + key + '_on'] === false) continue; number += 1; const id = 'guide-' + key; const chip = el('a', settings['guide_' + key + '_title']); chip.href = '#' + id; toc.append(chip); const card = el('section', null, 'guide-section' + (key === 'start' ? ' first' : '')); card.id = id; const head = el('h2'); head.append(el('span', number, 'step'), settings['guide_' + key + '_title']); const body = el('div', null, 'rich'); renderText(body, settings['guide_' + key + '_body']); card.append(head, body); sections.append(card); } if (!number) sections.append(el('p', '지금 보여 줄 안내가 없습니다.', 'small')); if (location.hash && document.getElementById(location.hash.slice(1))) document.getElementById(location.hash.slice(1)).scrollIntoView(); } render(window.dutySiteSettings); document.addEventListener('duty-site', event => render(event.detail)); // 설정을 끝내 받지 못하면 안내가 비어 보이지 않게 알린다. setTimeout(() => { if (!window.dutySiteSettings) $('guide-sections').replaceChildren(el('p', '안내를 불러오지 못했습니다. 인터넷 연결을 확인하고 새로고침해 주세요.', 'small')); }, 8000); })();