v.1 : 게시판이 붙을 경우 검색 안됨
document.querySelectorAll(".content").forEach(content => {
content.innerHTML = content.innerHTML.replace(/#([\w가-힣_]+)/g, function (match, tag) {
return `<a href="?search=${tag}">${match}</a>`;
});
});
#hashtag
v.2 : 글 본문에서 검색 안됨
document.querySelectorAll(".content").forEach(content => {
// 현재 페이지 URL에서 마지막 경로(게시판 ID) 가져오기
const pathParts = window.location.pathname.split("/").filter(Boolean); // 빈 값 제거
const boardId = pathParts.length > 0 ? pathParts[pathParts.length - 1] : ""; // 마지막 경로 값 (게시판 ID)
console.log(boardId); // 변수명 수정
content.innerHTML = content.innerHTML.replace(/#([\w가-힣_]+)/g, function (match, tag) {
if (boardId) {
return `<a href="./${boardId}/?search_keyword=${tag}&search_target=title_content">${match}</a>`;
} else {
return `<a href="./?search_keyword=${tag}&search_target=title_content">${match}</a>`;
}
});
});
v.3
document.querySelectorAll(".content").forEach(content => {
// 현재 페이지 URL에서 경로 가져오기
const pathParts = window.location.pathname.split("/").filter(Boolean); // 빈 값 제거
let boardId = pathParts.length > 0 ? pathParts[0] : ""; // 첫 번째 경로(게시판 ID)
// 글 번호(두 번째 경로)가 있으면 제거 (ex: threads/471363 → threads/)
if (pathParts.length > 1 && !isNaN(pathParts[1])) {
boardId = pathParts[0]; // 첫 번째 경로만 남기기
}
console.log("게시판 ID:", boardId); // 디버깅용
content.innerHTML = content.innerHTML.replace(/#([\w가-힣_]+)/g, function (match, tag) {
if (boardId) {
return `<a href="/${boardId}/?search_keyword=${tag}&search_target=title_content">${match}</a>`;
} else {
return `<a href="/?search_keyword=${tag}&search_target=title_content">${match}</a>`;
}
});
});
참조
https://codepen.io/nikolett_codes/pen/daWxea
https://xe1.xpressengine.com/?mid=download&package_id=22753681
https://xe1.xpressengine.com/index.php?mid=download&package_id=22753695