forked from qumuchegi/notion-docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Chege Qumu
committed
Apr 5, 2022
1 parent
d483c07
commit 283d940
Showing
5 changed files
with
80 additions
and
55 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
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,15 @@ | ||
import { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } from 'node-html-markdown' | ||
|
||
const nhm = new NodeHtmlMarkdown( | ||
/* options (optional) */ | ||
{ | ||
preferNativeParser: true, | ||
|
||
}, | ||
/* customTransformers (optional) */ undefined, | ||
/* customCodeBlockTranslators (optional) */ undefined | ||
); | ||
|
||
export default function convertHtmlToMd(htmlStr) { | ||
return nhm.translate(htmlStr) | ||
} |
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 |
---|---|---|
@@ -1,14 +1,8 @@ | ||
import React from 'react'; | ||
import { renderToString } from 'react-dom/server' | ||
import { NotionRenderer, Equation, Collection, CollectionRow, Code } from 'react-notion-x' | ||
// import { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } from 'node-html-markdown' | ||
// import '../style/notion-render.css' | ||
import convertHtmlToMd from './convertHtmlToMd' | ||
|
||
// const nhm = new NodeHtmlMarkdown( | ||
// /* options (optional) */ {}, | ||
// /* customTransformers (optional) */ undefined, | ||
// /* customCodeBlockTranslators (optional) */ undefined | ||
// ); | ||
const CHILD_BLOCK_TYPE = [ | ||
'page' | ||
] | ||
|
@@ -21,12 +15,11 @@ function findPageBlockId(parentId, blockValue, blocks = [], collectionView = {}) | |
// isSubPage 直接子页面 | ||
const isSubPage = blockValue.content.includes(block.value.id) && | ||
CHILD_BLOCK_TYPE.includes(block.value.type) | ||
// && block.value.id !== generatePageId(parentId) | ||
if (isSubPage) { | ||
childIds.push(block.value.id) | ||
} | ||
|
||
// todo 间接子页面,比如复制的其他 notion 页面的链接(非直接子页面) | ||
|
||
// collection page | ||
let collectionSubPageIds = [] | ||
if (block.value.type === 'collection_view') { | ||
|
@@ -47,26 +40,38 @@ function findPageBlockId(parentId, blockValue, blocks = [], collectionView = {}) | |
} | ||
|
||
const PageLink = ( | ||
redirectBaseUrl | ||
fileType, // html/md | ||
childPagesIdArr = [] | ||
) => (props) => { | ||
// console.log({props}) | ||
// console.log({props, childPagesIdArr}) | ||
const id = props.href.match(/\/(.*)/)[1] | ||
const isSubPage = childPagesIdArr.includes(generatePageId(id)) | ||
return <a | ||
{...props} | ||
className={props.className + ' notion-link-rewrite-base-path'} | ||
className={props.className + (isSubPage ? ' notion-link-rewrite-base-path' : '')} | ||
href={ | ||
// relativePath + | ||
'/childPages' + | ||
props.href + | ||
'/index.html' | ||
isSubPage | ||
? | ||
('/childPages' + | ||
props.href + | ||
`/index.${fileType}`) | ||
: ('https://www.notion.so' + props.href) | ||
} | ||
target='_blank' | ||
> | ||
{props.children} | ||
</a> | ||
} | ||
|
||
export function renderNotionPage(parentDir, recordMap, blockId) { | ||
const htmlStr = renderToString( | ||
export function renderNotionPage(parentDir, recordMap, blockId, fileType) { // fileType html/md | ||
const childPages = findPageBlockId( | ||
blockId, | ||
recordMap.block[generatePageId(blockId)].value, | ||
Object.values(recordMap.block), | ||
recordMap['collection_view'] | ||
).filter(id => id !== blockId) | ||
|
||
let htmlStr = renderToString( | ||
<NotionRenderer | ||
recordMap={recordMap} | ||
previewImages | ||
|
@@ -79,39 +84,37 @@ export function renderNotionPage(parentDir, recordMap, blockId) { | |
code: Code, | ||
collection: Collection, | ||
collectionRow: CollectionRow, | ||
pageLink: PageLink(parentDir) | ||
pageLink: PageLink(fileType, childPages) | ||
}} | ||
/> | ||
) | ||
const notionRenderCSSCDN = 'https://cdn.jsdelivr.net/npm/[email protected]/src/styles.css' | ||
htmlStr = `<html> | ||
<head> | ||
<meta charset='utf-8'/> | ||
<meta name="viewport" content="width=device-width,user-scalable=0,initial-scale=1,maximum-scale=1, minimum-scale=1"> | ||
<link rel='stylesheet' href='${notionRenderCSSCDN}'/> | ||
<script type='text/javascript'> | ||
window.onload = () => { | ||
const pageLinks = document.getElementsByClassName('notion-link-rewrite-base-path') | ||
console.log({pageLinks}) | ||
for(let i = 0; i < pageLinks.length; i++) { | ||
const linkNode = pageLinks[i] | ||
linkNode.href = window.location.href.replace('/index.html', '') + linkNode.href.replace('file:///', '/') | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
${htmlStr} | ||
</body> | ||
</html>` | ||
return { | ||
htmlStr: `<html> | ||
<head> | ||
<link rel='stylesheet' href='${notionRenderCSSCDN}'/> | ||
<script type='text/javascript'> | ||
window.onload = () => { | ||
const pageLinks = document.getElementsByClassName('notion-link-rewrite-base-path') | ||
console.log({pageLinks}) | ||
for(let i = 0; i < pageLinks.length; i++) { | ||
const linkNode = pageLinks[i] | ||
linkNode.href = window.location.href.replace('/index.html', '') + linkNode.href.replace('file:///', '/') | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
${htmlStr} | ||
</body> | ||
</html>`, | ||
|
||
childPages: findPageBlockId( | ||
blockId, | ||
recordMap.block[generatePageId(blockId)].value, | ||
Object.values(recordMap.block), | ||
recordMap['collection_view'] | ||
).filter(id => id !== blockId) | ||
str: fileType === 'html' ? htmlStr : convertHtmlToMd(htmlStr), | ||
childPages | ||
} | ||
} | ||
|
||
function generatePageId(rawPageId) { | ||
if (/[^-]{8}-[^-]{4}-[^-]{4}-[^-]{4}-[^-]{12}/.test(rawPageId)) { | ||
return rawPageId | ||
|