Skip to content

Commit

Permalink
Merge pull request #1 from steven-kraft/Fix-issues
Browse files Browse the repository at this point in the history
Fix Efficiency and Security Issues (1.1.0)
  • Loading branch information
steven-kraft committed May 10, 2021
2 parents aa1eb42 + ae20248 commit 6412b56
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
50 changes: 27 additions & 23 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
import { Plugin, MarkdownPostProcessor, MarkdownPostProcessorContext, MarkdownPreviewRenderer } from 'obsidian'
import { Plugin, MarkdownPostProcessor, MarkdownPostProcessorContext } from 'obsidian'

// Regular Expression for {{kanji|kana|kana|...}} format
const REGEXP = /{((?:[一-龯]|[ぁ-んァ-ン])+)((?:\|[ぁ-んァ-ン]*)+)}/gm

// Main Tags to search for Furigana Syntax
const TAGS = 'p, h1, h2, h3, h4, h5, h6, ol, ul, table'

const convertFurigana = (element:Node): Node => {
const text = element.textContent
const matches = Array.from(text.matchAll(REGEXP))
const convertFurigana = (element:Text): Node => {
const matches = Array.from(element.textContent.matchAll(REGEXP))
if (matches.length === 0) return element

let newText = text

let lastNode = element
for (const match of matches) {
const kanji = match[1].split('')
const furi = match[2].split('|').slice(1) // First Element will be empty
if (kanji.length === furi.length || furi.length === 1) {
// Number of Characters in first section must be equal to number of furigana sections (unless only one furigana section)
newText = newText.replace(match[0], function () {
// Create a stringified version of the ruby HTMLElement
if (furi.length === 1) return `<ruby>${kanji.join('')}<rt>${furi[0]}</rt></ruby>`
const innerHTML = kanji.map((k, i) => { return `${k}<rt>${furi[i]}</rt>` }).join('')
return `<ruby>${innerHTML}</ruby>`
})
const rubyNode = document.createElement('ruby')
rubyNode.addClass('furi')
let rt
if (furi.length === 1) {
rubyNode.appendChild(document.createTextNode(kanji.join('')))
rt = document.createElement('rt')
rt.innerText = furi[0]
rubyNode.appendChild(rt)
} else {
kanji.forEach((k, i) => {
rubyNode.appendChild(document.createTextNode(k))
rt = document.createElement('rt')
rt.innerText = furi[i]
rubyNode.appendChild(rt)
})
}
const nodeToReplace = lastNode.splitText(lastNode.textContent.indexOf(match[0]))
lastNode = nodeToReplace.splitText(match[0].length)
nodeToReplace.replaceWith(rubyNode)
}
}

// Replace TextElement with new Span containing Ruby Element(s)
const newElement = document.createElement('span')
newElement.addClass('furi')
newElement.innerHTML = newText
return newElement
return element
}

export default class MarkdownFurigana extends Plugin {
public postprocessor: MarkdownPostProcessor = (el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const blockToReplace = el.querySelector(TAGS)
if (!blockToReplace || !blockToReplace.innerHTML.match(REGEXP)) return
if (!blockToReplace) return

function replace (node:Node) {
node.childNodes.forEach(child => {
if (child.nodeType === 3) {
// Nodes of Type 3 are TextElements
child.replaceWith(convertFurigana(child))
child.replaceWith(convertFurigana(child as Text))
} else if (child.hasChildNodes() && child.nodeName !== 'CODE') {
// Ignore content in Code Blocks
replace(child)
Expand All @@ -55,11 +60,10 @@ export default class MarkdownFurigana extends Plugin {

async onload () {
console.log('loading Markdown Furigana plugin')
MarkdownPreviewRenderer.registerPostProcessor(this.postprocessor)
this.registerMarkdownPostProcessor(this.postprocessor)
}

onunload () {
console.log('unloading Markdown Furigana plugin')
MarkdownPreviewRenderer.unregisterPostProcessor(this.postprocessor)
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-markdown-furigana",
"name": "Markdown Furigana",
"version": "1.0.0",
"version": "1.1.0",
"minAppVersion": "0.9.12",
"description": "Simple Markdown to Furigana Rendering Plugin for Obsidian.",
"author": "Steven Kraft",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-markdown-furigana",
"version": "1.0.0",
"version": "1.1.0",
"description": "Simple Markdown to Furigana Rendering Plugin for Obsidian.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"1.0.0": "0.9.12"
"1.1.0": "0.9.12"
}

0 comments on commit 6412b56

Please sign in to comment.