Skip to content

Commit

Permalink
pre4: fix baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
softlion committed Jan 11, 2024
1 parent fdec91b commit a5abeb6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>

<UserSecretsId>a04bcd1f-7c68-4874-ac98-81af967e3eb3</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NotionSharp.ApiClient.Tests/TestNotionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public async Task TestGetSyndicationFeed()
Assert.IsNotNull(pages);
Assert.AreEqual(1, pages.Count);

var feed = await session.GetSyndicationFeed(pages[0]);
var feed = await session.GetSyndicationFeed(pages[0], new ("https://"));
Assert.IsNotNull(feed?.Items);
Assert.AreNotEqual(0, feed.Items.Count());
}
Expand Down
2 changes: 1 addition & 1 deletion NotionSharp.ApiClient/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class Constants
{
public const string BaseUrl = "https://www.notion.so/";
//public const string BaseUrl = "https://www.notion.so/";
public const string ApiBaseUrl = "https://api.notion.com/v1/";
public const string NotionApiVersion = "2022-06-28";
//public const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36";
Expand Down
12 changes: 8 additions & 4 deletions NotionSharp.ApiClient/Lib/Helpers/NotionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ namespace NotionSharp
public static class NotionUtils
{
//RegexOptions options = RegexOptions.None;
static readonly Regex TrimSpaces = new Regex(@"[\s-:()/\\\W]+", RegexOptions.None);
static readonly Regex TrimSpaces = new (@"[\s-:()/\\\W]+", RegexOptions.None);
//sentence = regex.Replace(sentence, " ");

public static Uri GetPageUri(string pageId, string? title)
=> new Uri(new Uri(Constants.BaseUrl), $"{Uri.EscapeUriString(TrimSpaces.Replace(title ?? "", "-"))}-{pageId}");
public static Uri GetPageUri(string pageId, string? title, Uri baseUrl)
{
var pageTitle = Uri.EscapeDataString(TrimSpaces.Replace(title ?? "", "-"));
pageId = pageId.Trim('-');
return new(baseUrl, $"{pageTitle}-{pageId}");
}

/// <summary>
/// Extract the block/page ID from a Notion.so URL
Expand All @@ -25,7 +29,7 @@ public static Uri GetPageUri(string pageId, string? title)
/// </example>
public static Guid? ExtractId(string urlOrId)
{
if (urlOrId.StartsWith(Constants.BaseUrl))
if (urlOrId.StartsWith("http"))
urlOrId = new Uri(urlOrId).GetLeftPart(UriPartial.Path).Split('-').Last().Split('#').Last();

if (Guid.TryParse(urlOrId, out var guid))
Expand Down
12 changes: 7 additions & 5 deletions NotionSharp.ApiClient/NotionSessionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public static async Task LoadChildBlocks(this NotionSession session, List<Block>
/// </summary>
/// <param name="session">a session</param>
/// <param name="page">blocks of type ChildPage only</param>
/// <param name="baseUrl">URL of the Notion domain for published pages</param>
/// <param name="maxItems">max number of child pages to return</param>
/// <param name="maxBlocks">limit the parsing of each page to the first maxBlocks blocks</param>
/// <param name="stopBeforeFirstSubHeader">when true, stop parsing a page when a line containing a sub_header is found</param>
Expand All @@ -309,10 +310,10 @@ public static async Task LoadChildBlocks(this NotionSession session, List<Block>
/// All child pages will go into this feed's items.
///
/// The created feed has the title and id of the page.
///
///
/// Are included only child pages which are not restricted with the "integration".
/// </remarks>
public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession session, Page page, int maxItems = 50, int maxBlocks = 20, bool stopBeforeFirstSubHeader = true, CancellationToken cancel = default)
public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession session, Page page, Uri baseUrl, int maxItems = 50, int maxBlocks = 20, bool stopBeforeFirstSubHeader = true, CancellationToken cancel = default)
{
var childPages = await session.GetBlockChildren(page.Id, cancel: cancel)
.Where(b => b is { Type: BlockTypes.ChildPage, ChildPage: { } }) //2 checks are redundant. We could keep only one.
Expand All @@ -322,7 +323,7 @@ public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession
if (childPages.Count == 0)
return new() { LastUpdatedTime = DateTimeOffset.Now };

var feed = await session.GetSyndicationFeed(childPages, maxBlocks, stopBeforeFirstSubHeader, cancel).ConfigureAwait(false);
var feed = await session.GetSyndicationFeed(childPages, baseUrl, maxBlocks, stopBeforeFirstSubHeader, cancel).ConfigureAwait(false);

var title = page.Title();

Expand All @@ -341,14 +342,15 @@ public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession
/// </summary>
/// <param name="session">a session</param>
/// <param name="childPages">blocks of type ChildPage only</param>
/// <param name="baseUrl">URL of the Notion domain for published pages</param>
/// <param name="maxBlocks">limits the parsing of each page to the first maxBlocks blocks</param>
/// <param name="stopBeforeFirstSubHeader">when true, stop parsing a page when a line containing a sub_header is found</param>
/// <param name="cancel"></param>
/// <returns>A SyndicationFeed containing one SyndicationItem per page</returns>
/// <remarks>
/// The created feed has no title/description
/// </remarks>
public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession session, List<Block> childPages, int maxBlocks = 20, bool stopBeforeFirstSubHeader = true, CancellationToken cancel = default)
public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession session, List<Block> childPages, Uri baseUrl, int maxBlocks = 20, bool stopBeforeFirstSubHeader = true, CancellationToken cancel = default)
{
var feedItems = new List<SyndicationItem>();
var htmlRenderer = new HtmlRenderer();
Expand All @@ -368,7 +370,7 @@ public static async Task<SyndicationFeed> GetSyndicationFeed(this NotionSession

var content = htmlRenderer.GetHtml(page.Children, stopBeforeFirstSubHeader);
var title = page.ChildPage!.Title;
var pageUri = NotionUtils.GetPageUri(page.Id, title);
var pageUri = NotionUtils.GetPageUri(page.Id, title, baseUrl);

var item = new SyndicationItem(title, content, pageUri)
{
Expand Down
2 changes: 1 addition & 1 deletion NotionSharp.ApiClient/NotionSharp.ApiClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PropertyGroup>
<!-- nuget configurable properties -->
<Version>2.0.0</Version>
<VersionSuffix>-pre3</VersionSuffix>
<VersionSuffix>-pre4</VersionSuffix>
<DefineConstants>$(DefineConstants);</DefineConstants>
</PropertyGroup>

Expand Down

0 comments on commit a5abeb6

Please sign in to comment.