Skip to main content

typub_adapter_wordpress/
config.rs

1use anyhow::Result;
2use typub_adapters_core::{
3    AdapterCapability, AdapterRegistrar, NodePolicy, NodePolicyAction, PlatformAdapter,
4    TaxonomyCapability, register_adapter, resolve_asset_strategy_from_config,
5    resolve_math_rendering_from_config,
6};
7use typub_config::{Config, PlatformConfig};
8use typub_core::{AssetStrategy, MathDelimiters, MathRendering};
9
10use crate::adapter::WordPressAdapter;
11use crate::model::ID;
12
13pub const CAPABILITY: AdapterCapability = AdapterCapability {
14    id: ID,
15    name: "WordPress",
16    short_code: "wp",
17    local_output: false,
18    requires_config: true,
19    taxonomy: TaxonomyCapability::full(),
20    asset_strategies: &[
21        AssetStrategy::Upload,
22        AssetStrategy::Embed,
23        AssetStrategy::External,
24    ],
25    math_renderings: &[MathRendering::Svg, MathRendering::Png], // Inline SVG or media upload
26    math_delimiters: &[MathDelimiters::Dollar],
27    code_highlight: true,
28    notes: "Tags/categories sync to WP taxonomies; internal links resolved from local status DB.",
29    node_policy: NodePolicy {
30        raw: NodePolicyAction::Sanitize,
31        unknown: NodePolicyAction::Drop,
32    },
33};
34
35pub fn create(config: &Config) -> Result<Box<dyn PlatformAdapter>> {
36    Ok(Box::new(WordPressAdapter::new(config)?))
37}
38
39pub fn register(registrar: &mut AdapterRegistrar) -> Result<()> {
40    register_adapter(registrar, &CAPABILITY, create)
41}
42
43pub fn resolve_asset_strategy(platform_config: Option<&PlatformConfig>) -> Result<AssetStrategy> {
44    resolve_asset_strategy_from_config(platform_config, &CAPABILITY)
45}
46
47/// Resolve math rendering strategy from platform configuration.
48pub fn resolve_math_rendering(platform_config: Option<&PlatformConfig>) -> Result<MathRendering> {
49    resolve_math_rendering_from_config(platform_config, &CAPABILITY)
50}
51
52#[cfg(test)]
53#[allow(clippy::expect_used)]
54mod tests {
55    use super::*;
56    use std::collections::HashMap;
57
58    #[test]
59    fn test_resolve_strategy_invalid_value() {
60        let cfg = PlatformConfig {
61            enabled: true,
62            asset_strategy: Some("invalid".to_string()),
63            published: None,
64            theme: None,
65            internal_link_target: None,
66            math_rendering: None,
67            math_delimiters: None,
68            extra: HashMap::new(),
69        };
70        let err =
71            resolve_asset_strategy(Some(&cfg)).expect_err("already configured invalid strategy");
72        assert!(err.to_string().contains("Invalid asset strategy"));
73    }
74
75    #[test]
76    fn test_resolve_strategy_disabled_platform() {
77        let cfg = PlatformConfig {
78            enabled: false,
79            asset_strategy: Some("embed".to_string()),
80            published: None,
81            theme: None,
82            internal_link_target: None,
83            math_rendering: None,
84            math_delimiters: None,
85            extra: HashMap::new(),
86        };
87        let resolved = resolve_asset_strategy(Some(&cfg)).expect("resolve");
88        assert_eq!(resolved, CAPABILITY.default_asset_strategy());
89    }
90}