-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4726d40
commit 8c91b6e
Showing
3 changed files
with
62 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const SPAN_CLOSE = '</span>'; | ||
|
||
import {escapeHTML} from './utils'; | ||
|
||
export default class HTMLRenderer { | ||
constructor(tree, options) { | ||
this.buffer = ""; | ||
this.classPrefix = options.classPrefix; | ||
tree.walk(this); | ||
} | ||
|
||
// renderer API | ||
|
||
addText(text) { | ||
this.buffer += escapeHTML(text) | ||
} | ||
|
||
openNode(node) { | ||
let className = node.kind; | ||
if (!node.kind) return; | ||
|
||
if (!node.sublanguage) | ||
className = `${this.classPrefix}${className}`; | ||
this.span(className); | ||
} | ||
|
||
closeNode(node) { | ||
if (!node.kind) return; | ||
|
||
this.buffer += SPAN_CLOSE; | ||
} | ||
|
||
// helpers | ||
|
||
span(className) { | ||
this.buffer += `<span class="${className}">` | ||
} | ||
|
||
value() { | ||
return this.buffer; | ||
} | ||
} |
This file contains 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