Utils Layers¶
This section is about the underlying utility functions that are used in the plugin, will be called by plugin core and CLI directly.
And specifically, the common module of CLI will call plugin core to get the plugin configuration instance, and scanner module will call meta module to validate the note file's frontmatter.
Notion sync lives under mkdocs_note.utils.notion and is invoked only from the CLI (notion-sync / ns), not from MkDocs build hooks.
Developer note: Cursor MCP token (optional)¶
notion_sync.allow_cursor_mcp_token (default false) may allow reading a Notion token from ~/.cursor/mcp.json for local developer convenience. When a token is loaded this way, a warning is emitted unless silence_mcp_token_warning: true. Prefer NOTION_TOKEN / project .env for normal use. This option is intentionally omitted from the user handbook.
Metadata helpers for note frontmatter.
Supports both MkDocs File objects (plugin build path) and plain text / path APIs used by CLI tools such as Notion sync.
extract_date(f) ¶
Extract date from docs file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f | File | The file to extract date from. | required |
Returns:
| Type | Description |
|---|---|
datetime | None | Optional[datetime]: The date if successful, None otherwise. |
Source code in src/mkdocs_note/utils/meta.py
147 148 149 150 151 152 153 154 155 156 157 158 159 | |
extract_tags(meta_dict) ¶
Normalize frontmatter tags / tag into a list of non-empty strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta_dict | dict[str, Any] | Parsed frontmatter mapping. | required |
Returns:
| Type | Description |
|---|---|
list[str] | List of tag strings. |
Source code in src/mkdocs_note/utils/meta.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
extract_title(f) ¶
Extract title from docs file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f | File | The file to extract title from. | required |
Returns:
| Type | Description |
|---|---|
str | None | Optional[str]: The title if successful, None otherwise. |
Source code in src/mkdocs_note/utils/meta.py
162 163 164 165 166 167 168 169 170 171 172 173 174 | |
parse_frontmatter(text) ¶
Split markdown text into frontmatter dict and body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Full file contents. | required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any], str] | Tuple of (metadata dict, body without frontmatter). |
Source code in src/mkdocs_note/utils/meta.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
parse_frontmatter_file(path) ¶
Read a file and parse its frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | Path | Path to a markdown (or text) file. | required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any], str] | Tuple of (metadata dict, body). |
Source code in src/mkdocs_note/utils/meta.py
58 59 60 61 62 63 64 65 66 67 68 | |
validate_frontmatter(f) ¶
Validate the frontmatter of the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f | File | The file to validate. | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if the frontmatter is valid, False otherwise. |
Source code in src/mkdocs_note/utils/meta.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
scan_notes(files, config) ¶
Scan notes directory, return all supported note files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files | Files | The list of files to scan | required |
config | Plugin configuration | required |
Returns:
| Type | Description |
|---|---|
tuple[list[File], list[File]] | tuple[list[File], list[File]]: (valid notes, invalid files) |
Source code in src/mkdocs_note/utils/scanner.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
Hierarchical page-tree helpers shared by CLI tools and Notion sync.
Produces a uniform TreeNode shape from either .nav.yml (awesome-nav) or a filesystem walk under notes_root (directory hierarchy preserved).
TreeNode dataclass ¶
A navigation / filesystem tree node.
Leaf content pages set file_rel; intermediate sections leave it None.
Source code in src/mkdocs_note/utils/tree.py
21 22 23 24 25 26 27 28 29 30 31 32 | |
build_directory_tree(root, *, parent_key='', rel_prefix='') ¶
Build a TreeNode forest from filesystem hierarchy under root.
Directories become sections; .md / .ipynb leaves become content pages. index.md files are omitted from the tree (callers still skip them on sync).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root | Path | Absolute or relative notes root directory. | required |
parent_key | str | Parent nav key for this level. | '' |
rel_prefix | str | Docs-relative path prefix for children. | '' |
Returns:
| Type | Description |
|---|---|
list[TreeNode] | Ordered list of tree nodes for this directory level. |
Source code in src/mkdocs_note/utils/tree.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
build_nav_tree(nodes, parent_key='', *, docs_prefix='docs/') ¶
Build a TreeNode forest from awesome-nav YAML nodes.
Source code in src/mkdocs_note/utils/tree.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
build_page_tree(*, nav_file, notes_root, docs_dir) ¶
Resolve page tree: prefer .nav.yml, else notes_root directory scan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nav_file | Path | None | Path to | required |
notes_root | Path | Notes directory for filesystem fallback. | required |
docs_dir | Path | Docs root used when stripping prefixes in nav paths. | required |
Returns:
| Type | Description |
|---|---|
list[TreeNode] | Tuple of (tree, source_label) where source_label is |
str |
|
Source code in src/mkdocs_note/utils/tree.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
docs_rel(path) ¶
Normalize path separators to forward slashes.
Source code in src/mkdocs_note/utils/tree.py
35 36 37 | |
index_tree(tree) ¶
Map node key → node for the entire tree.
Source code in src/mkdocs_note/utils/tree.py
71 72 73 | |
is_index_doc(rel) ¶
True for any docs-relative path whose basename is index.md.
Source code in src/mkdocs_note/utils/tree.py
45 46 47 | |
load_nav_yaml(nav_path) ¶
Load the nav list from an awesome-nav .nav.yml file.
Source code in src/mkdocs_note/utils/tree.py
76 77 78 79 80 81 82 83 84 | |
resolve_doc_path(raw, docs_prefix='docs/') ¶
Strip optional docs prefix and quotes from a nav path entry.
Source code in src/mkdocs_note/utils/tree.py
50 51 52 53 54 55 56 57 58 59 | |
title_from_path(path) ¶
Default title from a file stem.
Source code in src/mkdocs_note/utils/tree.py
40 41 42 | |
walk_tree(items) ¶
Depth-first flatten of a tree.
Source code in src/mkdocs_note/utils/tree.py
62 63 64 65 66 67 68 | |
Notion sync utilities (convert / client / sync).
SyncOptions dataclass ¶
Configuration for a Notion sync run (assembled by CLI / mkdocs.yml).
Source code in src/mkdocs_note/utils/notion/sync.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
run_sync(options) ¶
Main sync entry used by the CLI. Returns a process exit code.
Source code in src/mkdocs_note/utils/notion/sync.py
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
client ¶
Notion HTTP API helpers for page sync.
Handles page create/update, tags schema, file uploads, and block operations. Wiki IDs are always passed by the caller — never hardcoded here.
TagsSchemaCache dataclass ¶
Cached multi_select options for a wiki tags property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_source_id | str | Notion data source id (caller-supplied). | required |
property_name | str | Multi-select property name (default | '标签' |
Source code in src/mkdocs_note/utils/notion/client.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
ensure_options(token, tags) ¶
Merge missing tag names into the data source schema (preserving existing).
Source code in src/mkdocs_note/utils/notion/client.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
refresh(token) ¶
Load current multi_select option names from the data source schema.
Source code in src/mkdocs_note/utils/notion/client.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
attach_placeholder_images(token, page_id, image_paths) ¶
Replace ⟦LOCALIMG:N⟧ placeholders with uploaded image blocks.
Returns:
| Type | Description |
|---|---|
int | Number of images successfully attached. |
Source code in src/mkdocs_note/utils/notion/client.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
create_page(token, parent_id, title, *, title_property, parent_kind) ¶
Create a Notion page under a wiki data source or parent page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token | str | Notion bearer token. | required |
parent_id | str | Parent data-source, database, or page id. | required |
title | str | Page title (truncated to 2000 chars). | required |
title_property | str | Title property name on the parent schema. | required |
parent_kind | str | One of | required |
Returns:
| Type | Description |
|---|---|
dict[str, str] | Dict with |
Source code in src/mkdocs_note/utils/notion/client.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
delete_block(token, block_id) ¶
Delete (archive) a Notion block.
Source code in src/mkdocs_note/utils/notion/client.py
377 378 379 | |
insert_image_after(token, parent_id, after_block_id, upload_id, caption='') ¶
Insert an uploaded image block after after_block_id.
Source code in src/mkdocs_note/utils/notion/client.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
iter_blocks(token, block_id) ¶
Recursively yield child blocks under block_id (depth-first).
Source code in src/mkdocs_note/utils/notion/client.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
list_wiki_pages(token, data_source_id) ¶
Query all pages in a wiki data source (paginated).
Source code in src/mkdocs_note/utils/notion/client.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
notion_request(token, method, path, payload=None, notion_version=NOTION_VERSION_PAGES, retries=6) ¶
Perform a Notion REST request with retries on transient failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token | str | Notion integration bearer token. | required |
method | str | HTTP method (GET, POST, PATCH, DELETE, …). | required |
path | str | API path under | required |
payload | dict | None | Optional JSON body. | None |
notion_version | str | Notion-Version header value. | NOTION_VERSION_PAGES |
retries | int | Max attempts for rate-limit / network errors. | 6 |
Returns:
| Type | Description |
|---|---|
dict | Parsed JSON response dict (empty if body is empty). |
Source code in src/mkdocs_note/utils/notion/client.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
page_title(page, title_property='页面') ¶
Extract the title plain text from a Notion page object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page | dict | Notion page JSON. | required |
title_property | str | Preferred title property name to try first. | '页面' |
Source code in src/mkdocs_note/utils/notion/client.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
rich_text_plain(block) ¶
Concatenate plain_text from a block's rich_text array.
Source code in src/mkdocs_note/utils/notion/client.py
369 370 371 372 373 374 | |
update_page_markdown(token, page_id, markdown) ¶
Replace page content via the Notion markdown PATCH endpoint.
Source code in src/mkdocs_note/utils/notion/client.py
157 158 159 160 161 162 163 164 165 166 167 168 | |
update_page_tags(token, page_id, tags, *, property_name='标签', tags_cache=None) ¶
Write frontmatter tags into a Notion multi_select property; return applied names.
Wiki quirk: assigning a multi_select name that is not yet in the data-source schema returns HTTP 200 but leaves the property empty. Ensure options first.
Source code in src/mkdocs_note/utils/notion/client.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
upload_local_file(token, path, cache) ¶
Upload a local file to Notion and return a file-upload:// source URI.
Source code in src/mkdocs_note/utils/notion/client.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
convert ¶
Markdown / notebook → Notion Enhanced Markdown conversion.
Pure conversion helpers with no network I/O. Callers pass docs_root for path resolution instead of relying on a global docs directory.
collect_indented_block(lines, start, indent) ¶
Collect consecutive lines indented at least indent spaces.
Source code in src/mkdocs_note/utils/notion/convert.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
convert_admonitions_and_tabs(text) ¶
Convert Material admonitions and content tabs to Notion markup.
Source code in src/mkdocs_note/utils/notion/convert.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
convert_html_blocks(text) ¶
Convert known HTML embeds and strip HTML comments.
Source code in src/mkdocs_note/utils/notion/convert.py
207 208 209 210 211 212 213 214 215 216 217 | |
convert_images(text, source_file, site_url, docs_root, upload_local=None) ¶
Rewrite image markdown; optionally upload local files via upload_local.
Source code in src/mkdocs_note/utils/notion/convert.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
convert_inline_math(text) ¶
Adapt KaTeX-style math delimiters for Notion.
Source code in src/mkdocs_note/utils/notion/convert.py
196 197 198 199 200 201 202 203 204 | |
convert_links(text, source_file, page_map, docs_root) ¶
Rewrite internal links to Notion mention-page when mapped.
Source code in src/mkdocs_note/utils/notion/convert.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
convert_markdown_file(file_path, site_url, page_map, docs_root, upload_local=None) ¶
Convert a markdown or notebook file to Notion-ready markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path | Path | Source | required |
site_url | str | Public site base URL for absolute image links. | required |
page_map | dict[str, dict[str, str]] | Docs-relative path → | required |
docs_root | Path | Documentation root used for path resolution. | required |
upload_local | Callable[[Path], Any] | None | Optional callback that accepts a local | None |
Returns:
| Type | Description |
|---|---|
tuple[str, str, dict[str, Any]] | Tuple of |
Source code in src/mkdocs_note/utils/notion/convert.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
extract_tags(meta_dict) ¶
Normalize frontmatter tags / tag into a list of non-empty strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta_dict | dict[str, Any] | Parsed frontmatter mapping. | required |
Returns:
| Type | Description |
|---|---|
list[str] | List of tag strings. |
Source code in src/mkdocs_note/utils/meta.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
ipynb_to_markdown(path) ¶
Convert a Jupyter notebook to markdown (cells only; no raw JSON).
Source code in src/mkdocs_note/utils/notion/convert.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
is_gfm_table_separator_row(line) ¶
True for GFM alignment rows like |:-:|, |:-|, |-:|, | --- | :---: |.
Source code in src/mkdocs_note/utils/notion/convert.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
md_references_asset(source, asset_rel, docs_root) ¶
Cheap check: whether markdown likely references a docs-relative asset.
Source code in src/mkdocs_note/utils/notion/convert.py
573 574 575 576 577 578 579 580 581 582 583 584 585 | |
normalize_blockquotes_for_notion(text) ¶
Drop MkDocs blank quote markers (> alone) and collapse quote runs for Notion.
In MkDocs/Material, a lone > between quote lines acts as a paragraph/line break and does not render as an empty citation. Notion treats that line as an empty quote block. Convert each contiguous > run into a single Notion multi-line quote using <br>, omitting blank quote lines.
Source code in src/mkdocs_note/utils/notion/convert.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
notion_indent_block(text, tabs=1) ¶
Indent block children for Notion; keep blank lines indented so nesting holds.
Source code in src/mkdocs_note/utils/notion/convert.py
105 106 107 108 109 110 111 112 113 114 | |
page_has_local_images(source) ¶
Return True if the file contains any non-http(s) image references.
Source code in src/mkdocs_note/utils/notion/convert.py
563 564 565 566 567 568 569 570 | |
resolve_image_url(raw, source_file, site_url, docs_root) ¶
Build an absolute site URL for an image reference.
Source code in src/mkdocs_note/utils/notion/convert.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
resolve_internal_target(raw, source_file, docs_root) ¶
Resolve an internal markdown link to a docs-relative path.
Source code in src/mkdocs_note/utils/notion/convert.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
resolve_local_asset_path(raw, source_file, docs_root) ¶
Resolve a relative or site-absolute asset path under docs_root.
Source code in src/mkdocs_note/utils/notion/convert.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
strip_duplicate_h1(title, body) ¶
Remove a leading H1 that duplicates the page title.
Source code in src/mkdocs_note/utils/notion/convert.py
63 64 65 66 67 68 69 70 71 72 73 | |
strip_gfm_table_separators(text) ¶
Drop GFM table alignment rows so Notion does not insert them as data rows.
Markdown renderers ignore |:-:| / |:-| / |-:| separator lines; Notion's markdown ingest treats them as ordinary table rows.
Source code in src/mkdocs_note/utils/notion/convert.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
sync ¶
Notion wiki sync orchestration.
Token resolution, git incremental diff, page-map state, section ensure, and run_sync — the CLI entrypoint. Wiki IDs and site URL come from SyncOptions (or env fallbacks); nothing is hardcoded here.
DiffSet dataclass ¶
Incremental change set relative to docs_dir.
Source code in src/mkdocs_note/utils/notion/sync.py
95 96 97 98 99 100 101 102 | |
MigrationState dataclass ¶
Local map of docs-relative keys → Notion page id/url.
Source code in src/mkdocs_note/utils/notion/sync.py
85 86 87 88 89 90 91 92 | |
SyncOptions dataclass ¶
Configuration for a Notion sync run (assembled by CLI / mkdocs.yml).
Source code in src/mkdocs_note/utils/notion/sync.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
collect_targets(*, full, diff, nav_index, docs_dir, sections) ¶
Return (pages to sync, deleted rel paths).
Source code in src/mkdocs_note/utils/notion/sync.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
ensure_section(token, state, state_path, nav_index, key, delay, dry_run) ¶
Ensure a section (or Notebook root) exists; return Notion page id.
Source code in src/mkdocs_note/utils/notion/sync.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | |
expand_asset_dependents(assets, nav_index, docs_dir, sections) ¶
Find nav-listed markdown files that reference changed assets (scoped).
Source code in src/mkdocs_note/utils/notion/sync.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
filter_sections(rel, sections) ¶
True if rel is under any of the section prefixes (or no filter).
Source code in src/mkdocs_note/utils/notion/sync.py
562 563 564 565 566 | |
git_diff(base, project_root, docs_dir) ¶
Collect docs changes between base...HEAD. If base is None → full.
Source code in src/mkdocs_note/utils/notion/sync.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
load_state(path, default_title='页面') ¶
Load migration state JSON, or return an empty state.
Source code in src/mkdocs_note/utils/notion/sync.py
287 288 289 290 291 292 293 294 295 296 297 | |
rebuild_state_from_wiki(token, *, data_source_id, database_id, title_property='页面', tree=None, nav_file=None, notes_root=None, docs_dir=None, sections=None) ¶
Match existing wiki pages to nav / directory tree keys (recovery).
Pass tree when already built; otherwise call build_page_tree using nav_file / notes_root / docs_dir.
Source code in src/mkdocs_note/utils/notion/sync.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
resolve_git_base(project_root, explicit) ¶
Pick a base ref for incremental sync.
Priority: explicit → GITHUB_EVENT_BEFORE → NOTION_SYNC_BASE → HEAD~1. Returns None when a full sync is required.
Source code in src/mkdocs_note/utils/notion/sync.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
resolve_parent_id(token, state, state_path, nav_index, parent_key, delay, dry_run) ¶
Resolve parent Notion id and kind for a content page.
Source code in src/mkdocs_note/utils/notion/sync.py
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
resolve_token(explicit, project_root, allow_cursor_mcp_token=False, silence_mcp_token_warning=False) ¶
Resolve Notion token without requiring --token every run.
Order: explicit → load .env from project_root (no override) → NOTION_TOKEN / NOTION_API_KEY → project_root/.notion_token → ~/.config/notion/token → (only if allow_cursor_mcp_token) Cursor mcp.json.
Source code in src/mkdocs_note/utils/notion/sync.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
run_sync(options) ¶
Main sync entry used by the CLI. Returns a process exit code.
Source code in src/mkdocs_note/utils/notion/sync.py
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
save_state(path, state) ¶
Persist migration state JSON.
Source code in src/mkdocs_note/utils/notion/sync.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
setup_logging(verbose=False) ¶
Configure root logging for CLI runs (stdout, no log files).
Source code in src/mkdocs_note/utils/notion/sync.py
110 111 112 113 114 115 116 117 118 119 | |
sync_one_page(token, state, state_path, nav_index, item, *, docs_dir, site_url, delay, dry_run, upload_images, tags_property='标签', tags_cache=None) ¶
Create or update one Notion page from a nav tree leaf.
Source code in src/mkdocs_note/utils/notion/sync.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | |
commands ¶
CleanCommand ¶
Command to clean up orphaned asset directories.
Source code in src/mkdocs_note/utils/cli/commands.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
execute(dry_run=False) ¶
Execute the clean command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run | bool | If True, only report what would be removed without actually removing | False |
Source code in src/mkdocs_note/utils/cli/commands.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
MoveCommand ¶
Command to move a note(s) and its(their) corresponding asset directory(ies) like mv.
Source code in src/mkdocs_note/utils/cli/commands.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
execute(source, destination=None, permalink=None) ¶
Execute the move command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source | Path | The path to the source note file(s) to move, or file to rename permalink | required |
destination | Path | None | The path to the destination note file(s) to move (ignored if permalink is provided) | None |
permalink | str | None | If provided, rename permalink instead of moving file | None |
Source code in src/mkdocs_note/utils/cli/commands.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
NewCommand ¶
Command to create a new note.
Source code in src/mkdocs_note/utils/cli/commands.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
execute(permalink, file_path) ¶
Execute the new command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
permalink | str | The permalink value to use for frontmatter and asset directory | required |
file_path | Path | The path to the new note file | required |
Source code in src/mkdocs_note/utils/cli/commands.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
RemoveCommand ¶
Command to remove a note(s) and its(their) corresponding asset directory(ies) like rm -rf.
Source code in src/mkdocs_note/utils/cli/commands.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
execute(path, remove_assets=True) ¶
Execute the remove command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | Path | The path to the note file to remove | required |
Source code in src/mkdocs_note/utils/cli/commands.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
common ¶
Common utilities and data structures for CLI operations.
cleanup_empty_directories(start_dir, stop_at) ¶
Recursively remove empty parent directories up to a stop point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_dir | Path | Directory to start cleanup from | required |
stop_at | Path | Directory to stop at (won't be removed) | required |
Source code in src/mkdocs_note/utils/cli/common.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
ensure_parent_directory(path) ¶
Ensure the parent directory of a path exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | Path | File path whose parent should be created | required |
Raises:
| Type | Description |
|---|---|
OSError | If directory creation fails |
Source code in src/mkdocs_note/utils/cli/common.py
194 195 196 197 198 199 200 201 202 203 | |
get_asset_directory(note_path) ¶
Get the asset directory path for a note file based on filename.
Uses co-located asset structure: note_file.parent / "assets" / note_file.stem This is the legacy method based on filename stem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note_path | Path | Path to the note file | required |
Returns:
| Name | Type | Description |
|---|---|---|
Path | Path | The asset directory path |
Examples:
>>> get_asset_directory(Path("docs/usage/contributing.md"))
PosixPath('docs/usage/assets/contributing')
>>> get_asset_directory(Path("docs/notes/python/intro.md"))
PosixPath('docs/notes/python/assets/intro')
Source code in src/mkdocs_note/utils/cli/common.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
get_asset_directory_by_permalink(note_path, permalink) ¶
Get the asset directory path for a note file based on permalink.
Uses co-located asset structure: note_file.parent / "assets" / permalink
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note_path | Path | Path to the note file | required |
permalink | str | The permalink value from frontmatter | required |
Returns:
| Name | Type | Description |
|---|---|---|
Path | Path | The asset directory path |
Examples:
>>> get_asset_directory_by_permalink(Path("docs/notes/my-note.md"), "my-permalink")
PosixPath('docs/notes/assets/my-permalink')
Source code in src/mkdocs_note/utils/cli/common.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
get_permalink_from_file(note_path) ¶
Extract permalink value from note file's frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note_path | Path | Path to the note file | required |
Returns:
| Type | Description |
|---|---|
str | None | Optional[str]: The permalink value if found, None otherwise |
Examples:
>>> get_permalink_from_file(Path("docs/notes/my-note.md"))
'my-permalink'
Source code in src/mkdocs_note/utils/cli/common.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
get_plugin_config() ¶
Get the plugin configuration.
Returns:
| Name | Type | Description |
|---|---|---|
MkdocsNoteConfig | MkDocsConfig | The plugin configuration |
Source code in src/mkdocs_note/utils/cli/common.py
16 17 18 19 20 21 22 | |
is_excluded_name(name, exclude_patterns) ¶
Check if a filename matches any exclude pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Filename to check | required |
exclude_patterns | list[str] | List of patterns to exclude (e.g., ["index.md", "README.md"]) | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if name should be excluded |
Source code in src/mkdocs_note/utils/cli/common.py
181 182 183 184 185 186 187 188 189 190 191 | |
update_permalink_in_file(note_path, new_permalink) ¶
Update permalink value in note file's frontmatter.
This function preserves the original frontmatter format as much as possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note_path | Path | Path to the note file | required |
new_permalink | str | New permalink value to set | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if update was successful, False otherwise |
Examples:
>>> update_permalink_in_file(Path("docs/notes/my-note.md"), "new-permalink")
True
Source code in src/mkdocs_note/utils/cli/common.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |