From 2e57346c871fc3a693747281e1f41ce787976bf6 Mon Sep 17 00:00:00 2001 From: Nils Ree Date: Mon, 30 Jan 2023 16:59:45 +0100 Subject: [PATCH 1/4] Include image asset in sanity block (for alternative text) --- .../BlockContent/SanityHtmlSerializers.cs | 17 ++++++++--------- src/Sanity.Linq/CommonTypes/SanityBlock.cs | 3 ++- src/Sanity.Linq/CommonTypes/SanityImageAsset.cs | 5 ++++- .../SanityReferenceTypeConverter.cs | 14 ++++++++++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs b/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs index 0eef9a2..655d1dc 100644 --- a/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs +++ b/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs @@ -19,7 +19,7 @@ public Task SerializeDefaultBlockAsync(JToken input, SanityOptions sanit var listItemEnd = new StringBuilder(); var text2 = new StringBuilder(); - + // get style var tag = ""; @@ -27,7 +27,7 @@ public Task SerializeDefaultBlockAsync(JToken input, SanityOptions sanit { tag = "p"; } - else + else { //default to span tag = input["style"]?.ToString() ?? "span"; @@ -99,7 +99,7 @@ public Task SerializeDefaultBlockAsync(JToken input, SanityOptions sanit foreach (var mark in child["marks"]) { var sMark = mark?.ToString(); - var markDef = markDefs?.FirstOrDefault(m => m["_key"]?.ToString() == sMark); + var markDef = markDefs?.FirstOrDefault(m => m["_key"]?.ToString() == sMark); if (markDef != null) { if (TrySerializeMarkDef(markDef, context, ref start, ref end)) @@ -144,6 +144,8 @@ public Task SerializeImageAsync(JToken input, SanityOptions options) { var asset = input["asset"]; var imageRef = asset?["_ref"]?.ToString(); + var assetValue = asset?["value"]; + var imageAltText = assetValue?["altText"]; if (asset == null || imageRef == null) { @@ -168,18 +170,15 @@ public Task SerializeImageAsync(JToken input, SanityOptions options) url.Append(imageParts[2] + "."); // dimensions. url.Append(imageParts[3]); // file extension url.Append(parameters.ToString()); // ?crop etc.. - - return Task.FromResult($"
"); + + return Task.FromResult($"
\"{imageAltText}\"/
"); } public Task SerializeTableAsync(JToken input, SanityOptions options) { var html = ""; - - - return Task.FromResult(html); } protected virtual bool TrySerializeMarkDef(JToken markDef, object context, ref StringBuilder start, ref StringBuilder end) => false; } -} +} \ No newline at end of file diff --git a/src/Sanity.Linq/CommonTypes/SanityBlock.cs b/src/Sanity.Linq/CommonTypes/SanityBlock.cs index 4cc9ce4..8c7940b 100644 --- a/src/Sanity.Linq/CommonTypes/SanityBlock.cs +++ b/src/Sanity.Linq/CommonTypes/SanityBlock.cs @@ -16,10 +16,11 @@ public SanityBlock() : base() public object[] Children { get; set; } = new object[] { }; + [Include] public SanityReference Asset { get; set; } = new SanityReference { }; public int? Level { get; set; } public string ListItem { get; set; } } -} +} \ No newline at end of file diff --git a/src/Sanity.Linq/CommonTypes/SanityImageAsset.cs b/src/Sanity.Linq/CommonTypes/SanityImageAsset.cs index 59e2077..4a4bdb4 100644 --- a/src/Sanity.Linq/CommonTypes/SanityImageAsset.cs +++ b/src/Sanity.Linq/CommonTypes/SanityImageAsset.cs @@ -26,6 +26,9 @@ public SanityImageAsset() : base() { Type = "sanity.imageAsset"; } + + [JsonProperty("altText")] + public string AltText { get; set; } } public class SanityImageAssetReference : SanityImageAsset @@ -33,4 +36,4 @@ public class SanityImageAssetReference : SanityImageAsset [JsonProperty("_ref")] public string Ref { get; set; } } -} +} \ No newline at end of file diff --git a/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs b/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs index 7d6bedf..4383d07 100644 --- a/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs +++ b/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs @@ -66,6 +66,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s //Get reference from object var valRef = type.GetProperty("Ref").GetValue(value) as string; + object Value = null; // Alternatively, get reference from Id on nested Value if (string.IsNullOrEmpty(valRef)) @@ -82,6 +83,14 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s } } } + else + { + var propValue = type.GetProperty("Value"); + if (propValue != null) + { + Value = propValue.GetValue(value); + } + } // Get _key property (required for arrays in sanity editor) var keyProp = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_key" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_key")).GetValueOrDefault()); @@ -89,13 +98,14 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s var valKey = keyProp?.GetValue(value) as string ?? Guid.NewGuid().ToString(); var valWeak = weakProp?.GetValue(value) as bool? ?? null; + if (!string.IsNullOrEmpty(valRef)) { - serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak }); + serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak, value = Value }); return; } } serializer.Serialize(writer, null); } } -} +} \ No newline at end of file From 802b897ebaf3fae0a599774f882913c32caf4a46 Mon Sep 17 00:00:00 2001 From: Nils Ree Date: Fri, 3 Mar 2023 17:10:35 +0100 Subject: [PATCH 2/4] SanityImage: Include on Asset --- src/Sanity.Linq/CommonTypes/SanityImage.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Sanity.Linq/CommonTypes/SanityImage.cs b/src/Sanity.Linq/CommonTypes/SanityImage.cs index 46a9df9..0404b9d 100644 --- a/src/Sanity.Linq/CommonTypes/SanityImage.cs +++ b/src/Sanity.Linq/CommonTypes/SanityImage.cs @@ -28,10 +28,11 @@ public SanityImage() : base() SanityType = "image"; } + [Include] public SanityReference Asset { get; set; } public SanityImageCrop Crop { get; set; } public SanityImageHotspot Hotspot { get; set; } } -} +} \ No newline at end of file From b0701935887229afd3e961100b035f80d88925fd Mon Sep 17 00:00:00 2001 From: Eivind Dagsland Halderaker Date: Tue, 1 Aug 2023 14:27:44 +0200 Subject: [PATCH 3/4] Handle null in altText --- src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs b/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs index 655d1dc..b8643af 100644 --- a/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs +++ b/src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs @@ -145,7 +145,7 @@ public Task SerializeImageAsync(JToken input, SanityOptions options) var asset = input["asset"]; var imageRef = asset?["_ref"]?.ToString(); var assetValue = asset?["value"]; - var imageAltText = assetValue?["altText"]; + var imageAltText = assetValue?["altText"] ?? ""; if (asset == null || imageRef == null) { From fde75f94569d52c2d6abc9ae04e6a9f4ecf72696 Mon Sep 17 00:00:00 2001 From: Eivind Dagsland Halderaker Date: Tue, 1 Aug 2023 14:29:09 +0200 Subject: [PATCH 4/4] Refactor WriteToJson function --- .../SanityReferenceTypeConverter.cs | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs b/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs index 4383d07..1188977 100644 --- a/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs +++ b/src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs @@ -58,22 +58,22 @@ public override object ReadJson(JsonReader reader, Type type, object existingVal return null; } - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, object objectToSerialize, JsonSerializer serializer) { - if (value != null) + if (objectToSerialize != null) { - var type = value.GetType(); + var objectType = objectToSerialize.GetType(); //Get reference from object - var valRef = type.GetProperty("Ref").GetValue(value) as string; - object Value = null; + var valRef = objectType.GetProperty("Ref").GetValue(objectToSerialize) as string; + object objectToSerializePropValue = null; + var propValue = objectType.GetProperty("Value"); // Alternatively, get reference from Id on nested Value - if (string.IsNullOrEmpty(valRef)) + if (string.IsNullOrEmpty(valRef) && propValue != null) { - var propValue = type.GetProperty("Value"); - var valValue = propValue.GetValue(value); - if (propValue != null && valValue != null) + var valValue = propValue.GetValue(objectToSerialize); + if (valValue != null) { var valType = propValue.PropertyType; var idProp = valType.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_id" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_id")).GetValueOrDefault()); @@ -83,25 +83,22 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s } } } - else + + if (!string.IsNullOrEmpty(valRef) && propValue != null) { - var propValue = type.GetProperty("Value"); - if (propValue != null) - { - Value = propValue.GetValue(value); - } + objectToSerializePropValue = propValue.GetValue(objectToSerialize); } // Get _key property (required for arrays in sanity editor) - var keyProp = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_key" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_key")).GetValueOrDefault()); - var weakProp = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_weak" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_weak")).GetValueOrDefault()); - var valKey = keyProp?.GetValue(value) as string ?? Guid.NewGuid().ToString(); - var valWeak = weakProp?.GetValue(value) as bool? ?? null; + var keyProp = objectType.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_key" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_key")).GetValueOrDefault()); + var weakProp = objectType.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_weak" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_weak")).GetValueOrDefault()); + var valKey = keyProp?.GetValue(objectToSerialize) as string ?? Guid.NewGuid().ToString(); + var valWeak = weakProp?.GetValue(objectToSerialize) as bool? ?? null; if (!string.IsNullOrEmpty(valRef)) { - serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak, value = Value }); + serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak, value = objectToSerializePropValue }); return; } }