Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.1.0 ready for your review #69

Merged
merged 13 commits into from
Sep 13, 2022
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Most of the Notion blocks can generate their pandoc AST from _only_ their own da

## Built-in Plugins

N2y provides a few builtin plugins. Brief descriptions are provided below, but see [the code](https://github.com/innolitics/n2y/tree/rich-text-extensions/n2y/plugins) for details.
N2y provides a few builtin plugins. These plugins are all turned off by default. Brief descriptions are provided below, but see [the code](https://github.com/innolitics/n2y/tree/rich-text-extensions/n2y/plugins) for details.

### Deep Headers

Expand Down Expand Up @@ -167,6 +167,12 @@ Replace headers with links back to the originating notion block.

Adds support for Pandoc-style footnotes. Any `text` rich texts that contain footnote references in the format `[^NUMBER]` (eg: `...some claim [^2].`) will be linked to the corresponding footnote paragraph block starting with `[NUMBER]:` (eg: `[2]: This is a footnote.`).

### Expand Link To Page Blocks

When this plugin is enabled, any "link to page" block (which can be created using the `/link` command in the Notion UI), will be replaced with the content of the page that is linked to. This makes it possible to use the "link to page" block to include repeated content in multiple locations. It is like a "synced content block" in this way, but unlike "synced content blocks" which don't play well when duplicating child pages, the "link to page" blocks can be duplicated more easily.

Note that any link to a page that the integration doesn't have access to will be skipped entirely (Notion returns an "Unsupported Block" in this case).

## Architecture

N2y's architecture is divided into four main steps:
Expand Down Expand Up @@ -228,6 +234,7 @@ Here are some features we're planning to add in the future:
- Add `n2y.plugins.footnotes` plugin
- Add support for exporting HTML files (useful for generating jekyll pages or if you need pandoc features that aren't supported in github flavored markdown).
- Added the `n2yaudit` tool.
- Add the "link to page block"
- Added support for the column block types.
- Added basic support the link to page block type.

Expand Down
2 changes: 1 addition & 1 deletion n2y/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, client, notion_data, page, get_children=True):
page.plugin_data[plugin_key] = []
page.plugin_data[plugin_key].append({
"block_url": self.notion_url,
"linked_page_id": self.link_notion_id,
"linked_page_id": self.linked_page_id,
"type": "link to page",
})

Expand Down
21 changes: 12 additions & 9 deletions n2y/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,24 @@ def to_pandoc(self):
class LinkToPageBlock(Block):
def __init__(self, client, notion_data, page, get_children=True):
super().__init__(client, notion_data, page, get_children)

self.link_type = self.notion_data["type"]
self.link_notion_id = self.notion_data[self.link_type]
# The key for the object id may be either "page_id"
# or "database_id".
self.linked_page_id = self.notion_data[self.link_type]

def to_pandoc(self):
# TODO: in the future, if we are exporting the linked page too, then add
# a link to the page. For now, we just display the text of the page.
if self.link_type == "page_id":
page = self.client.get_page(self.link_notion_id)
title = page.title.to_pandoc()
elif self.link_type == "database_id":
database = self.client.get_database(self.link_notion_id)
title = database.title.to_pandoc()

page = self.client.get_page_or_database(self.linked_page_id)
if page is None:
msg = "Permission denied when attempting to access linked page [%s]"
logger.warning(msg, self.notion_url)
return None
else:
raise Exception(f"Unknown link type '{self.link_type}'")
return Para(title)
title = page.title.to_pandoc()
return Para(title)


def render_with_caption(content_ast, caption_ast):
Expand Down
5 changes: 4 additions & 1 deletion n2y/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def main(raw_args, access_token):
"--media-root", help="Filesystem path to directory where images and media are saved"
)
parser.add_argument("--media-url", help="URL for media root; must end in slash if non-empty")
parser.add_argument("--plugin", '-p', action='append', help="Plugin module")
parser.add_argument(
"--plugin", '-p', action='append',
help="Plugin module location, e.g. ('n2y.plugins.deepheaders')",
)
parser.add_argument(
"--output", '-o', default='./',
help="Relative path to output directory",
Expand Down
37 changes: 37 additions & 0 deletions n2y/plugins/expandlinktopages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging

from n2y.blocks import LinkToPageBlock

logger = logging.getLogger(__name__)


class ExpandingLinkToPageBlock(LinkToPageBlock):
"""
Replace page link with the content of the linked-to page.

"""

def to_pandoc(self):
assert self.linked_page_id is not None

if self.link_type == "page_id":
page = self.client.get_page(self.linked_page_id)
# The `page.block` refers to the ChildPageBlock in the page; we don't
# want to call `to_pandoc` on it directly, since we don't want a
# full pandoc document, but just the content that would have been in
# that document.
return page.block.children_to_pandoc()
else:
# TODO: Might be expanded to handle links to databases as well.
logger.warning(
'Links to databases (to:%s from:%s) not supported at this time.',
self.linked_page_id, self.page.notion_id
)
return None


notion_classes = {
"blocks": {
"link_to_page": ExpandingLinkToPageBlock,
}
}
Loading