Last active
December 10, 2024 12:27
-
-
Save eoureo/9235da337baf1e2848c350527a804a95 to your computer and use it in GitHub Desktop.
티스토리 에디터에서 텍스트 노드에 포함된 불필요한 공백 문자( 비공백 문자, \u00A0)를 제거하는 JavaScript 북마클릿입니다. 텍스트 콘텐츠의 보이지 않는 공백 문제를 간단히 해결할 수 있습니다. 티스토리, 북마클릿, JavaScript, 공백 제거, 비공백 문자
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 티스토리 불필요한 공백 제거 북마클릿 | |
* ----------------------------------- | |
* 이 스크립트는 티스토리 에디터에서 텍스트 노드에 포함된 | |
* 비공백 문자( , \u00A0)를 일반 공백으로 대체하여 불필요한 | |
* 공백 문제를 해결합니다. | |
* | |
* 사용 방법: | |
* 1. 아래 코드를 복사하여 브라우저 북마크에 저장하세요. | |
* 2. 티스토리 에디터 기본모드에서 북마크를 클릭하면 실행됩니다. | |
* | |
* 작성자: eoureo | |
* | |
* 이 코드는 누구나 자유롭게 사용할 수 있습니다. | |
* 사용으로 인한 문제나 손해에 대해서는 책임을 지지 않습니다. | |
*/ | |
javascript: (function () { | |
/* 에디터 iframe의 문서에서 모든 하위 요소를 배열로 가져옴 */ | |
const elements = Array.from( | |
document.getElementById("editor-tistory_ifr").contentWindow.document.body.children | |
); | |
elements.forEach((el) => { | |
/* 지정된 태그만 처리 (H1-H6, P, UL, OL, TABLE) */ | |
if (el.tagName.match(/^H[1-6]$|^P$|^UL$|^OL$|^TABLE$/i)) { | |
/* 해당 요소와 모든 하위 요소를 포함하는 배열 생성 */ | |
const allElements = Array.from(el.querySelectorAll("*")).concat(el); | |
allElements.forEach((el2) => { | |
/* 요소가 figure나 pre 태그인지 확인 */ | |
if (el2.tagName && ["figure", "pre"].includes(el2.tagName.toLowerCase())) { | |
return; /* figure나 pre 태그는 처리 하지 않음 */ | |
} | |
/* 요소의 자식 노드 순회 */ | |
Array.from(el2.childNodes).forEach((node) => { | |
/* 텍스트 노드만 처리 */ | |
if (node.nodeType === Node.TEXT_NODE) { | |
/* 텍스트 노드에 \u00A0 ( , non-breaking space) 포함 여부 확인 */ | |
if (node.nodeValue.includes("\u00A0")) { | |
/* \u00A0를 일반 공백으로 대체 */ | |
node.nodeValue = node.nodeValue.replace(/\u00A0/g, " "); | |
} | |
} | |
}); | |
}); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
아래 제 블로그 글에서 자세한 설명을 볼 수 있습니다.
https://eoureo.tistory.com/entry/티스토리-글에서-불필요한-nbsp-공백-제거하는-방법-Bookmarklet