Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SanityImageAsset: Alternative text + include in SanityImageAsset reference object #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public Task<string> SerializeDefaultBlockAsync(JToken input, SanityOptions sanit
var listItemEnd = new StringBuilder();

var text2 = new StringBuilder();


// get style
var tag = "";
if (input["style"]?.ToString() == "normal")
{
tag = "p";
}
else
else
{
//default to span
tag = input["style"]?.ToString() ?? "span";
Expand Down Expand Up @@ -99,7 +99,7 @@ public Task<string> 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))
Expand Down Expand Up @@ -144,6 +144,8 @@ public Task<string> 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)
{
Expand All @@ -168,18 +170,15 @@ public Task<string> 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($"<figure><img src=\"{url}\"/></figure>");

return Task.FromResult($"<figure><img src=\"{url}\" alt=\"{imageAltText}\"/></figure>");
}
public Task<string> 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;
}
}
}
3 changes: 2 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public SanityBlock() : base()

public object[] Children { get; set; } = new object[] { };

[Include]
public SanityReference<SanityImageAsset> Asset { get; set; } = new SanityReference<SanityImageAsset> { };

public int? Level { get; set; }

public string ListItem { get; set; }
}
}
}
3 changes: 2 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public SanityImage() : base()
SanityType = "image";
}

[Include]
public SanityReference<SanityImageAsset> Asset { get; set; }

public SanityImageCrop Crop { get; set; }

public SanityImageHotspot Hotspot { get; set; }
}
}
}
5 changes: 4 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityImageAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public SanityImageAsset() : base()
{
Type = "sanity.imageAsset";
}

[JsonProperty("altText")]
public string AltText { get; set; }
}

public class SanityImageAssetReference : SanityImageAsset
{
[JsonProperty("_ref")]
public string Ref { get; set; }
}
}
}
35 changes: 21 additions & 14 deletions src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +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;
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());
Expand All @@ -83,19 +84,25 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
}
}

if (!string.IsNullOrEmpty(valRef) && propValue != null)
{
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 });
serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak, value = objectToSerializePropValue });
return;
}
}
serializer.Serialize(writer, null);
}
}
}
}