Skip to content

Commit 4d2bedb

Browse files
authored
Merge pull request #74 from fasteiner/feature/37-new--function-for-every-block-class
"New-*" function for every block class #37:
2 parents d0ad098 + c71b758 commit 4d2bedb

11 files changed

Lines changed: 267 additions & 71 deletions

File tree

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Added
9+
10+
- **`source/Public/Block/Pdf/New-NotionPdfBlock.ps1`**
11+
- Implemented `New-NotionPdfBlock` to generate a Notion PDF block from provided caption and URL.
12+
13+
- **`source/Public/Block/Video/New-NotionVideoBlock.ps1`**
14+
- Implemented `New-NotionVideoBlock` to create a Notion video block with specified input.
15+
16+
- **`tests/Unit/Public/Block/New-NotionPdfBlock.Tests.ps1`**
17+
- Added unit tests for `New-NotionPdfBlock`, validating block construction from caption and URL.
18+
19+
- **`tests/Unit/Public/Block/New-NotionVideoBlock.Tests.ps1`**
20+
- Added unit tests for `New-NotionVideoBlock`, covering basic functionality and input validation.
21+
22+
### Fixed
23+
24+
- **`/workspaces/Notion/source/Classes/Block/27.99_Table.ps1`**
25+
- Fixed `[Table_structure]::ConvertFromObject()`
26+
27+
### Removed
28+
29+
- Removed unimplemented placeholder versions of these functions from the `Cmds` folder after relocating and implementing them.
30+
- **`source/Public/Block/Cmds/Pdf/New-NotionPdfBlock.ps1`**
31+
- **`source/Public/Block/Cmds/Video/New-NotionVideoBlock.ps1`**
32+
833
## [0.10.0] - 2025-06-27
934

1035
### Added

source/Classes/03_File/01_file.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ class notion_file : notion_icon
3333

3434
static [notion_file] Create([notion_filetype] $type, [string] $name, $caption, [string] $url, $expiry_time = $null )
3535
{
36-
$processedCaption = [rich_text]::ConvertFromObjects($caption)
37-
3836
switch ($type)
3937
{
4038
"file"
4139
{
42-
return [notion_hosted_file]::new($name, $processedCaption, $url, $expiry_time)
40+
return [notion_hosted_file]::new($name, $caption, $url, $expiry_time)
4341
}
4442
"external"
4543
{
46-
return [notion_external_file]::new($name, $processedCaption, $url)
44+
return [notion_external_file]::new($name, $caption, $url)
4745
}
4846
"file_upload "
4947
{
@@ -61,7 +59,7 @@ class notion_file : notion_icon
6159

6260
static [notion_file] ConvertFromObject($Value)
6361
{
64-
Write-Verbose "[notion_file]::ConvertFromObject($($Value | ConvertTo-Json))"
62+
Write-Verbose "[notion_file]::ConvertFromObject($($Value | ConvertTo-Json -Depth 10))"
6563
if ($null -eq $Value)
6664
{
6765
return $null

source/Classes/Block/24_PDF.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class notion_PDF_block : notion_block
1414
$this.pdf = [notion_file]::ConvertFromObject($file)
1515
}
1616

17+
notion_PDF_block($caption, $url)
18+
{
19+
$this.pdf = [notion_file]::Create("external", $null, $caption, $url, $null)
20+
}
21+
1722
notion_PDF_block($caption, $url, $name)
1823
{
1924
$this.pdf = [notion_file]::Create("external", $name, $caption, $url, $null)

source/Classes/Block/27.99_Table.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Table_structure
3838
static [Table_structure] ConvertFromObject($Value)
3939
{
4040
$Table_structure_Obj = [Table_structure]::new()
41-
$Table_structure_Obj.table_width = $Value.table.table_width
42-
$Table_structure_Obj.has_column_header = $Value.table.has_column_header
43-
$Table_structure_Obj.has_row_header = $Value.table.has_row_header
41+
$Table_structure_Obj.table_width = $Value.table_width
42+
$Table_structure_Obj.has_column_header = $Value.has_column_header
43+
$Table_structure_Obj.has_row_header = $Value.has_row_header
4444
return $Table_structure_Obj
4545
}
4646
}

source/Public/Block/Cmds/Pdf/New-NotionPdfBlock.ps1

Lines changed: 0 additions & 11 deletions
This file was deleted.

source/Public/Block/Cmds/Video/New-NotionVideoBlock.ps1

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function New-NotionPdfBlock {
2+
<#
3+
.SYNOPSIS
4+
Creates a new Notion PDF block.
5+
6+
.DESCRIPTION
7+
The New-NotionPdfBlock function creates a new instance of a Notion PDF block.
8+
Users can either provide an InputObject or specify the caption, name, and URL to create the block.
9+
10+
.PARAMETER InputObject
11+
The file object to be used for creating the PDF block. This should be an object that can be converted
12+
into a notion_PDF_block using the ConvertFromObject method.
13+
14+
.PARAMETER caption
15+
The caption for the PDF block.
16+
17+
.PARAMETER url
18+
The URL of the PDF file.
19+
20+
.OUTPUTS
21+
[notion_PDF_block]
22+
Returns an instance of the notion_PDF_block class.
23+
24+
.EXAMPLE
25+
$pdfBlock = New-NotionPdfBlock -InputObject $fileObject
26+
27+
Creates a new Notion PDF block using the specified file object.
28+
29+
.EXAMPLE
30+
$pdfBlock = New-NotionPdfBlock -caption "My PDF" -url "https://example.com/example.pdf"
31+
32+
Creates a new Notion PDF block using the specified caption and URL.
33+
34+
.NOTES
35+
This function requires the notion_PDF_block class to be defined in the project.
36+
#>
37+
[CmdletBinding(DefaultParameterSetName = "InputObjectSet")]
38+
[OutputType([notion_PDF_block])]
39+
param(
40+
[Parameter(Mandatory = $True, ParameterSetName = "InputObjectSet")]
41+
[Alias("File")]
42+
[object] $InputObject,
43+
44+
[Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
45+
[object] $caption,
46+
47+
# Not supported at the moment
48+
# [Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
49+
# [string] $name,
50+
51+
[Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
52+
[string] $url
53+
)
54+
55+
if ($PSCmdlet.ParameterSetName -eq "InputObjectSet") {
56+
# Create a new instance of notion_PDF_block using the provided InputObject
57+
return [notion_PDF_block]::ConvertFromObject($InputObject)
58+
} elseif ($PSCmdlet.ParameterSetName -eq "CaptionSet") {
59+
# Create a new instance of notion_PDF_block using the caption, name, and URL
60+
return [notion_PDF_block]::new($caption, $url<#, $name#>)
61+
}
62+
return $null
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function New-NotionVideoBlock {
2+
<#
3+
.SYNOPSIS
4+
Creates a new Notion Video block.
5+
6+
.DESCRIPTION
7+
The New-NotionVideoBlock function creates a new instance of a Notion Video block.
8+
Users can either provide an InputObject or specify the caption, name, and URL to create the block.
9+
10+
.PARAMETER InputObject
11+
The file object to be used for creating the Video block. This should be an object that can be converted
12+
into a notion_video_block using the ConvertFromObject method.
13+
14+
.PARAMETER caption
15+
The caption for the Video block.
16+
17+
.PARAMETER url
18+
The URL of the Video file.
19+
20+
.OUTPUTS
21+
[notion_video_block]
22+
Returns an instance of the notion_video_block class.
23+
24+
.EXAMPLE
25+
$videoBlock = New-NotionVideoBlock -InputObject $fileObject
26+
27+
Creates a new Notion Video block using the specified file object.
28+
29+
.EXAMPLE
30+
$videoBlock = New-NotionVideoBlock -caption "My Video" -url "https://example.com/example.mp4"
31+
32+
Creates a new Notion Video block using the specified caption and URL.
33+
34+
.NOTES
35+
This function requires the notion_video_block class to be defined in the project.
36+
#>
37+
[CmdletBinding(DefaultParameterSetName = "InputObjectSet")]
38+
[OutputType([notion_video_block])]
39+
param(
40+
[Parameter(Mandatory = $True, ParameterSetName = "InputObjectSet")]
41+
[Alias("File")]
42+
[object] $InputObject,
43+
44+
[Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
45+
[object] $caption,
46+
47+
# Not supported at the moment
48+
# [Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
49+
# [string] $name,
50+
51+
[Parameter(Mandatory = $True, ParameterSetName = "CaptionSet")]
52+
[string] $url
53+
)
54+
55+
if ($PSCmdlet.ParameterSetName -eq "InputObjectSet") {
56+
# Create a new instance of notion_video_block using the provided InputObject
57+
return [notion_video_block]::ConvertFromObject($InputObject)
58+
} elseif ($PSCmdlet.ParameterSetName -eq "CaptionSet") {
59+
# Create a new instance of notion_video_block using the caption, name, and URL
60+
return [notion_video_block]::new("external", $null ,$caption, $url, $null)
61+
}
62+
return $null
63+
}

tests/Unit/Classes/Block/27.99_Table.tests.ps1

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,11 @@ Describe "notion_table_block Tests" {
1515
$mut = Import-Module -Name "$script:projectPath/output/module/$ProjectName/$script:version/$ProjectName.psd1" -Force -ErrorAction Stop -PassThru
1616
}
1717

18-
Context "Table_structure Constructors" {
19-
It "should create default Table_structure" {
20-
$table = [Table_structure]::new()
21-
$table.table_width | Should -Be 0
22-
$table.has_column_header | Should -BeFalse
23-
$table.has_row_header | Should -BeFalse
24-
$table.children | Should -BeNullOrEmpty
25-
}
26-
27-
It "should create Table_structure with width" {
28-
$table = [Table_structure]::new(3)
29-
$table.table_width | Should -Be 3
30-
}
31-
32-
It "should create Table_structure with width and headers" {
33-
$table = [Table_structure]::new(4, $true, $true)
34-
$table.table_width | Should -Be 4
35-
$table.has_column_header | Should -BeTrue
36-
$table.has_row_header | Should -BeTrue
37-
}
38-
}
39-
40-
Context "Table_structure Methods" {
41-
It "should add a single row" {
42-
$table = [Table_structure]::new()
43-
$row = [notion_table_row_block]::new()
44-
$table.addRow($row)
45-
$table.children.Count | Should -Be 1
46-
}
47-
48-
It "should add multiple rows" {
49-
$table = [Table_structure]::new()
50-
$rows = @([notion_table_row_block]::new(), [notion_table_row_block]::new())
51-
$table.addRows($rows)
52-
$table.children.Count | Should -Be 2
53-
}
54-
}
55-
5618
Context "notion_table_block Constructors" {
5719
It "should create default notion_table_block" {
5820
$block = [notion_table_block]::new()
5921
$block.type | Should -Be "table"
60-
$block.table | Should -BeOfType "Table_structure"
22+
$block.table.gettype().Name | Should -Be "Table_structure"
6123
}
6224

6325
It "should create notion_table_block with rows" {
@@ -86,14 +48,14 @@ Describe "notion_table_block Tests" {
8648

8749
Context "ConvertFromObject Tests" {
8850
It "should convert from object correctly" {
89-
$mock = [PSCustomObject]@{
51+
$object = [PSCustomObject]@{
9052
table = [PSCustomObject]@{
9153
table_width = 3
9254
has_column_header = $true
9355
has_row_header = $false
9456
}
9557
}
96-
$block = [notion_table_block]::ConvertFromObject($mock)
58+
$block = [notion_table_block]::ConvertFromObject($object)
9759
$block | Should -BeOfType "notion_table_block"
9860
$block.table.table_width | Should -Be 3
9961
$block.table.has_column_header | Should -BeTrue
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# FILE: New-NotionPdfBlock.Tests.ps1
2+
Import-Module Pester
3+
4+
BeforeDiscovery {
5+
$script:projectPath = "$($PSScriptRoot)/../../../.." | Convert-Path
6+
7+
if (-not $ProjectName) {
8+
$ProjectName = Get-SamplerProjectName -BuildRoot $script:projectPath
9+
}
10+
Write-Debug "ProjectName: $ProjectName"
11+
$global:moduleName = $ProjectName
12+
Set-Alias -Name gitversion -Value dotnet-gitversion
13+
$script:version = (gitversion /showvariable MajorMinorPatch)
14+
15+
Remove-Module -Name $global:moduleName -Force -ErrorAction SilentlyContinue
16+
17+
$mut = Import-Module -Name "$script:projectPath/output/module/$ProjectName/$script:version/$ProjectName.psd1" -Force -ErrorAction Stop -PassThru
18+
}
19+
20+
Describe "New-NotionPdfBlock" {
21+
InModuleScope $moduleName {
22+
# Test for InputObject parameter set
23+
It "Should create a Notion PDF block with InputObject" {
24+
$fileObject = @{ pdf = @{ type = "external"; external = @{ url = "https://example.com/example.pdf" }; <#name = "example.pdf";#> caption = "Example Caption" } }
25+
$result = New-NotionPdfBlock -InputObject $fileObject
26+
$result | Should -Not -BeNullOrEmpty
27+
$result | Should -BeOfType "notion_PDF_block"
28+
$result.pdf.type | Should -Be "external"
29+
$result.pdf.external.url | Should -Be "https://example.com/example.pdf"
30+
# $result.pdf.name | Should -Be "example.pdf"
31+
$result.pdf.caption.plain_text | Should -Be "Example Caption"
32+
}
33+
34+
# Test for caption, name, and URL parameter set
35+
It "Should create a Notion PDF block with caption, name, and URL" {
36+
$result = New-NotionPdfBlock -caption "My PDF Caption" <#-name "example.pdf"#> -url "https://example.com/example.pdf"
37+
$result | Should -Not -BeNullOrEmpty
38+
$result | Should -BeOfType "notion_PDF_block"
39+
$result.pdf.type | Should -Be "external"
40+
$result.pdf.external.url | Should -Be "https://example.com/example.pdf"
41+
# $result.pdf.external.name | Should -Be "example.pdf"
42+
$result.pdf.caption.plain_text | Should -Be "My PDF Caption"
43+
}
44+
45+
# Test for invalid parameter combinations
46+
It "Should throw an error when both InputObject and caption are specified" {
47+
{ New-NotionPdfBlock -InputObject @{ pdf = @{ type = "external"; external = @{ url = "https://example.com/example.pdf"; name = "example.pdf"; caption = "Example Caption" } } } -caption "My PDF" -name "example.pdf" -url "https://example.com/example.pdf" } | Should -Throw
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)