-
Notifications
You must be signed in to change notification settings - Fork 2
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
Zaydek Michels-Gualtieri
committed
Aug 1, 2020
1 parent
47bc37a
commit 3964196
Showing
10 changed files
with
110 additions
and
118 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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
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 was deleted.
Oops, something went wrong.
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 @@ | ||
export { default } from "./usePreferences" |
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,95 @@ | ||
import escape from "lodash/escape" | ||
import toArray from "lib/x/toArray" | ||
import toTree from "Editor/components/toReactTree/toTree" | ||
|
||
// Reads text content from tree-shaped text nodes. | ||
function textContent(treeTextNodes) { | ||
let str = "" | ||
for (const each of toArray(treeTextNodes)) { | ||
if (typeof each === "string") { | ||
str += each | ||
continue | ||
} | ||
str += each.props.children && | ||
textContent(each.props.children) | ||
} | ||
return str | ||
} | ||
|
||
// Resolves tree-shaped children to a resolver-type. | ||
function toInnerText(children, resolver) { | ||
let str = "" | ||
for (const each of toArray(children)) { | ||
if (typeof each === "string") { | ||
str += resolver !== markup ? each : escape(each) | ||
continue | ||
} | ||
str += resolver[each.type](each) | ||
} | ||
return str | ||
} | ||
|
||
// Converts to tree-shaped text nodes. | ||
function toText(children, resolver) { | ||
return toInnerText(toTree(children), resolver) | ||
} | ||
|
||
const markdown = { | ||
em: el => `_${toInnerText(el.props.children, markdown)}_`, | ||
strong: el => `**${toInnerText(el.props.children, markdown)}**`, | ||
code: el => `\`${textContent(el.props.children)}\``, | ||
strike: el => `~~${toInnerText(el.props.children, markdown)}~~`, | ||
|
||
// TODO | ||
a: el => `[${toInnerText(el.props.children, markdown)}](${el.props.href || "TODO"})`, | ||
|
||
"h1": el => `# ${toText(el.props.children, markdown)}`, | ||
"h2": el => `## ${toText(el.props.children, markdown)}`, | ||
"h3": el => `### ${toText(el.props.children, markdown)}`, | ||
"h4": el => `#### ${toText(el.props.children, markdown)}`, | ||
"h5": el => `##### ${toText(el.props.children, markdown)}`, | ||
"h6": el => `###### ${toText(el.props.children, markdown)}`, | ||
"p": el => toText(el.props.children, markdown), | ||
} | ||
|
||
const markup = { | ||
em: el => `<em>${toInnerText(el.props.children, markup)}</em>`, | ||
strong: el => `<strong>${toInnerText(el.props.children, markup)}</strong>`, | ||
code: el => `<code>${toInnerText(el.props.children, markup)}</code>`, | ||
strike: el => `<strike>${toInnerText(el.props.children, markup)}</strike>`, | ||
|
||
// TODO | ||
a: el => `<a href="${el.props.href}">${toInnerText(el.props.children, markup)}</a>`, | ||
|
||
"h1": el => `<h1>\n\t${toText(el.props.children, markup) || "<br />"}\n</h1>`, | ||
"h2": el => `<h2>\n\t${toText(el.props.children, markup) || "<br />"}\n</h2>`, | ||
"h3": el => `<h3>\n\t${toText(el.props.children, markup) || "<br />"}\n</h3>`, | ||
"h4": el => `<h4>\n\t${toText(el.props.children, markup) || "<br />"}\n</h4>`, | ||
"h5": el => `<h5>\n\t${toText(el.props.children, markup) || "<br />"}\n</h5>`, | ||
"h6": el => `<h6>\n\t${toText(el.props.children, markup) || "<br />"}\n</h6>`, | ||
"p": el => `<p>\n\t${toText(el.props.children, markup) || "<br />"}\n</p>`, | ||
} | ||
|
||
// Converts to GFM. | ||
export function toMarkdown(elements) { | ||
let str = "" | ||
for (const each of elements) { | ||
str += markdown[each.type](each) | ||
if (each !== elements[elements.length - 1]) { | ||
str += "\n" | ||
} | ||
} | ||
return str | ||
} | ||
|
||
// Converts to HTML. | ||
export function toMarkup(elements) { | ||
let str = "" | ||
for (const each of elements) { | ||
str += markup[each.type](each) | ||
if (each !== elements[elements.length - 1]) { | ||
str += "\n" | ||
} | ||
} | ||
return str | ||
} |
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 @@ | ||
// TODO |
6 changes: 3 additions & 3 deletions
6
src/EditorApp/usePreferences.js → ...ditorApp/usePreferences/usePreferences.js
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